A
ajk
I am confused, I come from Win32 and am now trying to learn C#
threading
I have a simple winform with a button, in the button's eventhandler I
create a thread, I want the thread to report progress to the form i.e.
writing a line in a list box, but the app throws an exception
complaining about the listbox being in another thread. Fine, but how
do solve this in the simplest manner?
Here is some of the code:
in the button event handler I start the thread
...
Thread workerThread = new Thread(SimulatedClient);
workerThread.IsBackground = true;
workerThread.Name = "My Thread";
workerThread.Start(this); // the form instance passed
as argument
...
in the form I have a listbox say 'lbList'
the thread function is declared as (not sure if using static is needed
here)
private static void SimulatedClient(Object objParent)
{
MyFormClass mfc = (MyFormClass)objParent; // getting the
form instance
... do some stuff ...
mfc.Items.Add("Some Msg"); <-- gives the exception
}
All of the above is in the same form class.
I have looked at delegates and events a bit but the coin hasn't fallen
down yet In Win32 I would for instance send a message from the
thread to the main window, how is this done in C#/.NET?
Thanks in advance.
Anders.
threading
I have a simple winform with a button, in the button's eventhandler I
create a thread, I want the thread to report progress to the form i.e.
writing a line in a list box, but the app throws an exception
complaining about the listbox being in another thread. Fine, but how
do solve this in the simplest manner?
Here is some of the code:
in the button event handler I start the thread
...
Thread workerThread = new Thread(SimulatedClient);
workerThread.IsBackground = true;
workerThread.Name = "My Thread";
workerThread.Start(this); // the form instance passed
as argument
...
in the form I have a listbox say 'lbList'
the thread function is declared as (not sure if using static is needed
here)
private static void SimulatedClient(Object objParent)
{
MyFormClass mfc = (MyFormClass)objParent; // getting the
form instance
... do some stuff ...
mfc.Items.Add("Some Msg"); <-- gives the exception
}
All of the above is in the same form class.
I have looked at delegates and events a bit but the coin hasn't fallen
down yet In Win32 I would for instance send a message from the
thread to the main window, how is this done in C#/.NET?
Thanks in advance.
Anders.