Click to See Complete Forum and Search --> : Passing data from a worker thread to a form
markyoung1984
November 4th, 2008, 09:18 AM
I have my main program, which loads a form. On loading of the form a thread is started that basically works through a list of IP addresses, pinging each IP address using the Ping class. I can output results to a file without a problem. However, I now want to output results to the form that has already been loaded by the Main thread. How can this be done?
dannystommen
November 4th, 2008, 10:45 AM
private SynchronizationContext _uiSyncContext = null;
private void frmUser_Load(object sender, EventArgs e) {
_uiSyncContext = SynchronizationContext.Current;
Thread t = new Thread(new ThreadStart(MyThreadMethod));
t.Start();
}
private void AddIPadressesToRichTextBox(object list) {
List<string> adresses = (list as List<string>);
foreach(string s in adresses){
richTextBox1.AppendText(s);
}
}
private void MyThreadMethod() {
//do something with the IPadress and add them to the list
List<string> adresses = new List<string>();
adresses.Add("ipadress");
//and more adresses
SendOrPostCallback callback = delegate(object state) {
AddIPadressesToRichTextBox(state);
};
_uiSyncContext.Post(callback, adresses);
}
In this example I use a List<string> to pass to the main thread, you can use anything you want
TheCPUWizard
November 4th, 2008, 11:22 AM
You need to pass the information back to the main thread. The easiest way is to use "Control.Invoke".
DeepT
November 4th, 2008, 02:17 PM
Here is sample from one of my programs:
private void UpdateStatus(object sender, AutoSSHEventArgs e)
{
if (true == InvokeRequired)
{
Invoke(new EventHandler<AutoSSHEventArgs>(UpdateStatus), new object[] { sender, e });
}
else
{
TermBox.AppendText(e.Message);
}
}
You can use it as a template, this is an event callback, but you can do the same thing for a normal method.
Thread1
November 4th, 2008, 11:20 PM
another solution would be to use the BackgroundWorker
TheCPUWizard
November 4th, 2008, 11:28 PM
another solution would be to use the BackgroundWorker
While BackgroundWorker vs. "raw" thread management is a very good idea, I dont see how it changes anything regarding the need to perform the control updates from the UI thread... :confused: :confused:
Thread1
November 5th, 2008, 12:57 AM
While BackgroundWorker vs. "raw" thread management is a very good idea, I dont see how it changes anything regarding the need to perform the control updates from the UI thread... :confused: :confused:
the convenience is that it will not require additional codes to update the UI. a value can be passed to the main thread on some of the BackgroundWorker events and then update any control directly without checking for InvokeRequired. :)
cjard
November 5th, 2008, 07:52 AM
You need to pass the information back to the main thread. The easiest way is to use "Control.Invoke".
BackgroundWorker handles the delegation in a model that is already familiar to users.. It raises events that are handled on the UI thread, so no invoking in user code is required. The user chooses when to raise an event and can provide arguments that the UI thread can use in drawing.. It's definitely the easiest way :)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.