Click to See Complete Forum and Search --> : Change label on form from thread


FrankZA
April 9th, 2008, 07:55 AM
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:


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...

zdavis
April 9th, 2008, 08:28 AM
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



//Thread event
public static event EventHandler yourEventName;


subscribe to the event on your main form


classname.yourEventName += mainform_yourEventName;



after you have that done you should be able to use the following class to invoke the event across threads.



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



invoker.invoke(myHandler, this, eventargs.empty);



Hopefully that helps

-zd

zdavis
April 9th, 2008, 08:29 AM
Sorry....I just realized that this is the VB forum....Sorry for the confusion.

FrankZA
April 9th, 2008, 08:35 AM
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.

zdavis
April 9th, 2008, 08:54 AM
Yeah....That was in C#. I will try to convert that over to vb today and repost.

FrankZA
April 9th, 2008, 09:34 AM
Thanks zd, I would appreciate that a lot!