firehawk Posted September 11, 2005 Posted September 11, 2005 (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 September 11, 2005 by firehawk Quote
Leaders dynamic_sysop Posted September 11, 2005 Leaders Posted September 11, 2005 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); } Quote
firehawk Posted September 11, 2005 Author Posted September 11, 2005 thanks for that but there is nothing about delegates in there :( (im using C# btw) Quote
Wile Posted September 12, 2005 Posted September 12, 2005 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 Quote Nothing is as illusive as 'the last bug'.
firehawk Posted September 12, 2005 Author Posted September 12, 2005 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! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.