public class Form1 : Form { //new instance of Main public Main myMain; public Server myServer; public Form1() { InitializeComponent(); myMain = new Main(this); myServer = new Server(this); myServer.LogEvent += new EventHandler(Form1_LogEvent); } //Handle log events public void Form1_LogEvent(object sender, LogEventArgs e) { //Make sure we're invoking on the right thread if (InvokeRequired) { //Invoke the other-thread method on the Form thread MessageBox.Show("Invoking..."); Invoke(new EventHandler(Form1_LogEvent)); //return; } MessageBox.Show("Callign update log"); this.updateLog(e.EntryString); } } public class Main { //form1 ref public Form1 myForm1; public Main(Form1 form1ref) { myForm1 = form1ref; } } public class Server { // The "listener" for incoming connections private TcpListener _myListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 11001); // The incoming client string private static string _data = null; // Thread signal public static ManualResetEvent allDone = new ManualResetEvent(false); // Form1 reference private static Form1 _myForm1Ref; //Threading and Event public event EventHandler LogEvent; Thread myServerThread; private void OnLogEvent(string entryString) { EventHandler myHandler = LogEvent; if (myHandler != null) { //Throw the event myHandler(this, new LogEventArgs(entryString)); } } //Start the thread public void startThread() { myServerThread = new Thread(new ThreadStart(StartListening)); myServerThread.IsBackground = true; myServerThread.Start(); } public Server(Form1 myForm1Ref) { _myForm1Ref = myForm1Ref; } public void StartListening() { // Incoming buffer byte[] myBuffer = new Byte[1024]; // Need socket endpoint // Get the host IP info IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); // Grab me an IP... IPAddress ipAddress = ipHostInfo.AddressList[0]; // Set the endpoint IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000); // Make a new TCP/IP socket... Socket myListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); EventHandler myEventHandler = LogEvent; // Bind socket to endpoint... try { myListener.Bind(localEndPoint); myListener.Listen(100); while (true) { //set event to nonsignaled allDone.Reset(); System.Windows.Forms.MessageBox.Show("Bla"); if (myEventHandler != null) { //Throw the event myEventHandler(this, new LogEventArgs(String.Format("Listening for connections on {0}:{1}...", ipAddress.ToString(), "11000"))); } //Start socket to listen myListener.BeginAccept(new AsyncCallback(AcceptCallback), myListener); //Wait for a connection... allDone.WaitOne(); } } catch (Exception e) { // handle the exception } }