Change label on form from thread
Hi
I am busy writing a program that is supposed to evaluate some algorithms (by time or result). When the program loads, a form appears. Once some selections in comboboxes have been made, the "Start" button begins the process.
I have used threads so that I can change the priority. Result evaluation can be run on a "belownormal" priority, while the time evaluation would be better when running on "highest" priority so that anything else that Windows is running has a lesser effect (or am I wrong here).
My code has the following structure:
Code:
Public Class Form1
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button.Click
Dim C1 as New Class1
Call C1.NotCompare()
C1 = Nothing
End Sub
End Class
Public Class Class1
Public Sub NotCompare()
Dim t As Threading.Thread
t = New Threading.Thread(AddressOf Me.Compare)
t.Priority = ThreadPriority.AboveNormal
t.Start()
Thread.Sleep(0)
End Sub
Public Sub Compare()
Dim i as Integer
For i = 0 to AlgorithmList.GetUpperBound(0)
Form1.AlgorithmLabel.Text = AlgorithmList(i).Name 'This is where the problem lies I think
Next
i = Nothing
End Sub
End Class
I am attempting to to write the name of the algorithm to the label AlgorithmLabel on the original form Form1. I also want to write the name of the dataset in use to the form so that the user knows how far along the calculations the thread is (I assume this would be done in the same way).
How would I approach this problem of changing the label's text from a thread? Or is there a better approach?
Also, is it useful to use the Sleep(0) option at all?
Thanks...
Re: Change label on form from thread
If I have understood the question correctly
I would declare an event in your thread and then subscribe to the event from your main form.
Depending on what you want to whether you need your own event args class you could do something like
Code:
//Thread event
public static event EventHandler yourEventName;
subscribe to the event on your main form
Code:
classname.yourEventName += mainform_yourEventName;
after you have that done you should be able to use the following class to invoke the event across threads.
Code:
public static class Invoker
{
public static void Invoke(Delegate delegateToCall, params object[] argsOfDelegateToCall)
{
foreach (Delegate del in delegateToCall.GetInvocationList())
{
ISynchronizeInvoke si = del.Target as ISynchronizeInvoke;
if (si != null && si.InvokeRequired)
si.Invoke(del, argsOfDelegateToCall);
else
del.DynamicInvoke(argsOfDelegateToCall);
}
}
}
Invoke the event
Code:
invoker.invoke(myHandler, this, eventargs.empty);
Hopefully that helps
-zd
Re: Change label on form from thread
Sorry....I just realized that this is the VB forum....Sorry for the confusion.
Re: Change label on form from thread
Quote:
Originally Posted by zdavis
Sorry....I just realized that this is the VB forum....Sorry for the confusion.
Would something like this work in VB? I have no experience in other languages, but I would guess that was some form of C.
Re: Change label on form from thread
Yeah....That was in C#. I will try to convert that over to vb today and repost.
Re: Change label on form from thread
Thanks zd, I would appreciate that a lot!