UI invoke controls

firehawk

Newcomer
Joined
Sep 11, 2005
Messages
4
Hi,

I need some help and i've been googling all night long and cannot find any decent examples or help on this matter

im using C#

I have a form (Main form) and several class files.
One of these class/method file are executed on a seperate thread.

Now, on the main form there is a label, which shows the user whats happening (like a status label)

From the thread created, it will update the status text on the form.
I would like to know how to make use of eventhandler/delegates (control.invoke) so that the label status can be updated safely from the thread method that wishes to update it.

please, any help is most welcome but do give clear examples so I can grasp the idea of this.


more about the classes:

the Main Form has a method called "UIUpdateStatus(string theMessage)" which updates the form's label with the message in the params.

That method is public so anyone can call it.

Thanks!
 
Last edited:
here's a quick example of modifying a public control on a form via a seperate thread, you need to make a cast of the ActiveForm.

eg:
PHP:
 private void button1_Click(object sender, System.EventArgs e)
{
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(myThread));
thread.Start();
}
static void myThread()
{
/// cast a Form1 , by getting the ActiveForm...
Form1 frm1 = ( Form1 )Form.ActiveForm;
frm1.label1.Text = "some fandaby dozy text here";
MessageBox.Show("your label should now say...\r" + frm1.label1.Text);
}
 
dynamic_sysop, your code will have the nasty habit of not working as expected (depending on the GUI controls and the methods called), as you dont check that the started thread is the same thread as the thread that created the GUI controls.

Search the forum for InvokeRequired to see some examples regarding updating the GUI from any thread, e.g. this thread:
http://www.xtremedotnettalk.com/showthread.php?t=86818&highlight=invokerequired
 
Thanks wile...

I found a way of doing delegates but I seem to always get an Arguement out of range exception (im using .NET CF)

Main form:

Code:
public delegete void UpdateStatusHandler()
..
..


public void UpdateStatus()
{
   this.lblUpdate.Text = this.theStatus;
}


//ClassA

(somemethod...)
theMainForm.theStatus = "Something";
theMainForm.Invoke(new UpdateStatusHandler(theMainForm.UpdateStatus));

I always get the exception :( Don't know why!
 
Back
Top