My Vizai

Hi

Can anybody help me to change the text property of label control from a thread.(when a thread is running I need to change the Text property of Label)

Thanks

vizai




Re: Windows Forms General How to change Text property of label control from a thread

Karthik Simha

Hi,

Check the below code.. this would solve the problem...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim objThread As New System.Threading.Thread(AddressOf changeLabelText)

objthread.Start()

End Sub

Private Sub changeLabelText()

Label1.Text = "New Text"

End Sub






Re: Windows Forms General How to change Text property of label control from a thread

Mark Dawson

Hi,

that code will not work since you are trying to access the property of a control from a thread that did not create the control, cross thread actions like that are not allowed. In the 1.1 version of the framework this would have possibly worked sometimes but could cause errors, in the 2.0 version of the framework this will throw an exception.

Mark.






Re: Windows Forms General How to change Text property of label control from a thread

Karthik Simha

Hi,

Check this one.. may be this should help you...

Dim ChangeLabeTextInvoker As New MethodInvoker(AddressOf changeLabelText)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim objThread As New System.Threading.Thread(AddressOf differentThread)

objThread.Start()

End Sub

Private Sub differentThread()

Me.Invoke(ChangeLabeTextInvoker) 'Synchronous

'Me.BeginInvoke(ChangeLabeTextInvoker) 'Asynchronous

End Sub

Private Sub changeLabelText()

Label1.Text = "New Text"

End Sub






Re: Windows Forms General How to change Text property of label control from a thread

Mark Dawson

Hi,

in order to change a control that was created in a different thread from the one currently executing you will have to use Invoke or BeginInvoke to first put control back to the thread that created the control, since only the thread that created a control is allowed to update its properties, an example of how you can do this is shown below, however what are you trying to do, you may be able to use a System.Windows.Forms.Timer or System.Timers.Timer instead

//For demonstration purposes:

using System;

using System.Threading;

using System.Windows.Forms;

namespace ConsoleApplication2

{

class Program

{

private static Label label = new Label();

static void Main(string[] args)

{

Thread t = new Thread(new ThreadStart(UpdateLabel));

t.Start();

t.Join();

}

public static void UpdateLabel()

{

//Called by the thread we just created

ChangeLabel("Hi There");

}

private delegate void LabelUpdateDelegate(string value);

public static void ChangeLabel(string value)

{

if (label.InvokeRequired)

{

//This thread did not create the label, need to marshal control

//to the correct thread.

label.Invoke(new LabelUpdateDelegate(ChangeLabel), new object[] { value });

}

else

{

//In the context of the thread that created the label

label.Text = value;

}

}

}

}

Mark.






Re: Windows Forms General How to change Text property of label control from a thread

Karthik Simha

Hi Mark,

I am new to .net framework 2.0.. i couldnt find "InvokeRequired" property in .net framework 1.0....

can you pls brief on the "label.InvokeRequired" property...






Re: Windows Forms General How to change Text property of label control from a thread

Mark Dawson

Hi Karthik,

this property has been available in all versions of the framework, see: http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.invokerequired(VS.80).aspx

It allows you to check if you are on the correct thread before updating a control.

Mark.






Re: Windows Forms General How to change Text property of label control from a thread

My vizai

Thank u mark






Re: Windows Forms General How to change Text property of label control from a thread

Karthik Simha

Thanks Mark.