Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

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!

Edited by firehawk
  • Leaders
Posted

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:

 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);
}

Posted

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

Nothing is as illusive as 'the last bug'.
Posted

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:

 

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!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...