Здрасте, я уже в отчаяни, трети сутки сижу с этой проблемой... Может кто по опытней меня и сталкивался с чем-то подобным? Есть такой простенький код: | Код | using System; using System.Collections.Generic; using System.Windows.Forms; using System.Threading;
namespace WindowsApplication2 { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Server server = new Server("Server"); Thread serverThread = new Thread(new ThreadStart(Server.startServer)); serverThread.Start(); Application.Run(new Form1()); } } }
using System; using System.Collections.Generic; using System.Text; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using System.Collections; using System.Runtime.Serialization.Formatters;
namespace WindowsApplication2 { class Server { private static string name; public Server() { } public Server(string name) { Server.name = name; } public static void startServer() { System.Console.WriteLine("Server"); BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider(); BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider(); serverProvider.TypeFilterLevel = TypeFilterLevel.Full; IDictionary p = new Hashtable(); p["port"] = 4000; p["name"] = "RemoteServer"; ChannelServices.RegisterChannel(new TcpChannel(p, clientProvider, serverProvider)); RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServerObject), Server.name, WellKnownObjectMode.Singleton); while (true) { } } } }
using System; using System.Collections.Generic; using System.Text;
namespace WindowsApplication2 { public class ServerObject : MarshalByRefObject, IServer { public event NotifyEventHandler NotifyEvent;
public void Notify(string message) { if (NotifyEvent != null) NotifyEvent(this, new NotifyEventArgs(message)); } } }
using System; using System.Collections.Generic; using System.Text;
namespace WindowsApplication2 { public delegate void NotifyEventHandler(object sender, NotifyEventArgs e);
public interface IServer { event NotifyEventHandler NotifyEvent; void Notify(string message); } }
using System; using System.Collections.Generic; using System.Text;
namespace WindowsApplication2 { [Serializable] public class NotifyEventArgs : EventArgs { private string _message;
public string Message { get { return _message; } }
public NotifyEventArgs(string message) { _message = message; } } }
using System; using System.Collections.Generic; using System.Text;
namespace WindowsApplication2 { public interface IForm { void setStatus(string messedge); } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using System.Collections; using System.Runtime.Serialization.Formatters;
namespace WindowsApplication2 { public partial class Form1 : Form,IForm { private IServer server; private IClient client; public Form1() { InitializeComponent(); BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider(); BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider(); serverProvider.TypeFilterLevel = TypeFilterLevel.Full; IDictionary p = new Hashtable(); p["port"] = 0; p["name"] = "RemoteClient"; ChannelServices.RegisterChannel(new TcpChannel(p, clientProvider, serverProvider)); server = (IServer)Activator.GetObject(typeof(IServer), "tcp://localhost:4000/Server"); client = new ClientObject(server, this); }
private void button1_Click(object sender, EventArgs e) { this.server.Notify(this.label1.Text + " a"); } public void setStatus(string messege) { this.label1.Text = messege; }
} }
using System; using System.Collections.Generic; using System.Text;
namespace WindowsApplication2 { public interface IClient { void NotifyEventMethod(object sender, NotifyEventArgs e); } }
using System; using System.Collections.Generic; using System.Text;
namespace WindowsApplication2 { public class ClientObject : MarshalByRefObject, IClient { private IServer _server; private IForm _f;
public ClientObject(IServer server, Form1 form) //public ClientObject(IServer server) { _server = server; _f = form; _server.NotifyEvent += new NotifyEventHandler(NotifyEventMethod); }
~ClientObject() { _server.NotifyEvent -= new NotifyEventHandler(NotifyEventMethod); }
public void NotifyEventMethod(object sender, NotifyEventArgs e) { Console.WriteLine("##########" + e.Message); this._f.setStatus(e.Message); Console.WriteLine("##########" + e.Message); } } }
|
значит проблема в следующем, когда я пытаюсь выполнить this._f.setStatus(e.Message); программа перестает реагировать на внешние раздрожители, т.е. при обновлении label1 если же this._f.setStatus(e.Message); вобще убрть/закоментировать, то прога работает! Так что думаю проблемма лежит и менно в Форме... Зарание спасибо...
|