Windows Develop Bookmark and Share   
 index > Windows Forms General > question about updating GUI components from a seperate thread.
 

question about updating GUI components from a seperate thread.

Hi,

I am coming from a Java background, where if we want to update a GUI component from a seperate thread, we would do something like this:

Code Snippet

SwingUtilities.invokeLater(new Runnable() {
public void run() {
outputArea.append("New Text");
}
});



rather than just doing: outputArea.append("New Text"), because of threading violations. I was wondering if similar considerations need to be taken into account in C#? thanks!
anikkar  Saturday, February 16, 2008 12:47 AM

Hi,

same applies for C#, only the thread that created the control is allowed to access it. You call call the Invoke method on a control which marshals control back to the main UI thread (of BeginInvoke if you want to do it Asyncronously):

delegate void UpdateTextBoxDelegate(string s);

private void Form1_Load(object sender, EventArgs e)

{

Thread t = new Thread(delegate()

{

//This code is called inside the non UI thread

textBox1.Invoke((UpdateTextBoxDelegate)delegate(string s)

{

textBox1.Text = s;

}, new object[]{"hi there"});

});

t.Start();

}

MArk.
Mark Dawson  Saturday, February 16, 2008 1:20 AM

Hi,

same applies for C#, only the thread that created the control is allowed to access it. You call call the Invoke method on a control which marshals control back to the main UI thread (of BeginInvoke if you want to do it Asyncronously):

delegate void UpdateTextBoxDelegate(string s);

private void Form1_Load(object sender, EventArgs e)

{

Thread t = new Thread(delegate()

{

//This code is called inside the non UI thread

textBox1.Invoke((UpdateTextBoxDelegate)delegate(string s)

{

textBox1.Text = s;

}, new object[]{"hi there"});

});

t.Start();

}

MArk.
Mark Dawson  Saturday, February 16, 2008 1:20 AM
Hi Mark,

I'm having a bit of trouble following. Lets assume that the thread does not have direct access to the text box, but rather has access to the user control, where the text box is located, and in turn, the user control has a function like:

Code Snippet

public void updateText(String t)
{
textBox.Text = t;
}



how exactly would the thread then access the text box?
anikkar  Saturday, February 16, 2008 1:57 AM

Hi,

so if the non UI thread has access to the user control then it would do something like:

delegate UpdateTextDelegate(string s);

public void Foo()

{

//This is the non UI thread in the function

myUserControl.Invoke(new UpdateTextDelegate(myUserControl.updateText), new object[]{" hello"});

}

by calling myUserControl.Invoke this causes the method specified in the first parameter to be called on the same thread that created the control. The array of objects are the parameters to the method. A delegate is just a strongly typed function pointer.

Does that help answer your question?

Mark.

Mark Dawson  Saturday, February 16, 2008 5:47 AM

Or you could do something more like this in case you may not know if the thread isnt the UI thread or not:

Code Snippet

private delegate UpdateTextDelegate(string s);

public void UpdateText(string text)

{

if (textbox1.InvokeRequired)

{

textBox1.Invoke(new UpdateTextDelegate(UpdateText), text);

}

else

{

textbox1.Text = text;

}

}

Dan Rigsby  Saturday, February 16, 2008 3:04 PM
got it working. Thanks!
anikkar  Tuesday, February 19, 2008 9:42 PM

You can use google to search for other answers

Custom Search

More Threads

• Multithreading problem
• TabControl
• Determining if a form is the "active" form?
• Problem Treeview control using DrawMode (OwnerDrawAll)
• Change font of RichtextBox
• Numeric Up Down Question
• Avoiding two button_clicks in inherited form
• Pass LocalReport objects to ReportViewer control
• ComboBox Column AutoCompleteMode = AutoCompleteMode.SuggestAppend Width Problem
• How to capture double Click event on combobox