WorkerThreads updating WindowsForm field.

  • Thread starter Thread starter Joshua Russell
  • Start date Start date
J

Joshua Russell

Firstly my main method is like this:

static void Main(string[] args)
{
// New Form Thread
FormHandler myFormHandler = new FormHandler();
ThreadStart myThreadStart = new ThreadStart(myFormHandler.ShowForm);
Thread formWorkerThread = new Thread(myThreadStart);
formWorkerThread.Start();

// Main Thread Handler
MainThreadHandler threadHandler = new MainThreadHandler();
}

This opens an instance of a form that I use as a main interface with a
notify icon. it then makes a new instance of MainThreadHandler causing my
thread handles to start. This is a TCP listener. It creates a new worker
thred for every incoming connection (server). I would like each server
thread created to update or cause the updating of a field in the main Form.
In effect I would like a field on the main form that tells me how manny
connections I have currently open.
Any one know how I can do this?
 
Hi Joshua,

I guess you will need to pass to the MainThreadHandler
either the FormHandler instance or an object that holds a reference to
the form that you would like to update.

MainThreadHandler threadHandler = new MainThreadHandler(myFormHandler);

Then, MainThreadHandler's listener thread would do something like the
following to update the form:

_myFormHandler.MyUpdateFormMethod();

The FormHandler.MyUpdateFormMethod() method updates the form.

Also, since you are updating the form's controls from another thread,
make sure you use Control.Invoke() in the method that updates the
form. (MyUpdateFormMethod in the above example)

Regards,
Aravind C
 
Back
Top