473,396 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Updating a form control from another thread

I have a form with a socket which is handled by the beginReceive function.
When the data is received it is in turn returned to the window for display.
I have done this several times before and the call back is straight forward
enough it just seems the way I am doing it is excessive. I was wondering if
there was a easier way of coding this.

private delegate void SetMessageDelegate(string Message);
private void SetMessage(string Message)
{
this.rtfMessages.AppendText(Message);
}
private void SetMessageHelper(string Message)
{
if(!this.IsDisposed)
this.BeginInvoke(new SetMessageDelegate(this.SetMessage),
new object[] { Message });
}

Regards,
John
Oct 23 '06 #1
2 2062
John J. Hughes II wrote:
I have a form with a socket which is handled by the beginReceive function.
When the data is received it is in turn returned to the window for display.
I have done this several times before and the call back is straight forward
enough it just seems the way I am doing it is excessive. I was wondering if
there was a easier way of coding this.

private delegate void SetMessageDelegate(string Message);
private void SetMessage(string Message)
{
this.rtfMessages.AppendText(Message);
}
private void SetMessageHelper(string Message)
{
if(!this.IsDisposed)
this.BeginInvoke(new SetMessageDelegate(this.SetMessage),
new object[] { Message });
}

Regards,
John

As long as you are calling your UI from outside the UI thread,
(Begin)Invoke is the only way. However I got around fostering a second
mehod just to handle the "Cross thread case".

using System.Windows.Forms;

namespace TheClient
{
// set of standard delegates, declared in a
// central spot.
public delegate void NullaryFunction();
public delegate Ret NullaryFunction<Ret>();

public delegate void UnaryFunction<Arg>(Arg a);
public delegate Ret UnaryFunction<Arg, Ret>(Arg a);

public delegate void BinaryFunction<Arg1, Arg2>(Arg1 a1, Arg2 a2);
public delegate Ret UnaryFunction<Arg1, Arg2, Ret>(Arg1 a1, Arg2 a2);

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public void DoSomethingOnUi(string s)
{
if(InvokeRequired)
{// if cross thread, recursive invokation
Invoke(new UnaryFunction<string>(DoSomethingOnUi), s);
return;
}

// ... Do normal stuff. Always will be in the GUI thread.
}
}
}

HTH,
Andy
--
You can email me directly by removing the NOSPAm below
xm**********@gmxNOSPAm.netNOSPAm
Oct 23 '06 #2
Neat, did not know that InvokeRequired existed, that makes this a little
easier. The code was kind of getting clucky with two methods doing almost
the exact same function.

Thanks,
John

"Andreas Mueller" <me@privacy.netwrote in message
news:4q************@individual.net...
John J. Hughes II wrote:
>I have a form with a socket which is handled by the beginReceive
function. When the data is received it is in turn returned to the window
for display. I have done this several times before and the call back is
straight forward enough it just seems the way I am doing it is excessive.
I was wondering if there was a easier way of coding this.

private delegate void SetMessageDelegate(string Message);
private void SetMessage(string Message)
{
this.rtfMessages.AppendText(Message);
}
private void SetMessageHelper(string Message)
{
if(!this.IsDisposed)
this.BeginInvoke(new SetMessageDelegate(this.SetMessage),
new object[] { Message });
}

Regards,
John

As long as you are calling your UI from outside the UI thread,
(Begin)Invoke is the only way. However I got around fostering a second
mehod just to handle the "Cross thread case".

using System.Windows.Forms;

namespace TheClient
{
// set of standard delegates, declared in a
// central spot.
public delegate void NullaryFunction();
public delegate Ret NullaryFunction<Ret>();

public delegate void UnaryFunction<Arg>(Arg a);
public delegate Ret UnaryFunction<Arg, Ret>(Arg a);

public delegate void BinaryFunction<Arg1, Arg2>(Arg1 a1, Arg2 a2);
public delegate Ret UnaryFunction<Arg1, Arg2, Ret>(Arg1 a1, Arg2 a2);

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public void DoSomethingOnUi(string s)
{
if(InvokeRequired)
{// if cross thread, recursive invokation
Invoke(new UnaryFunction<string>(DoSomethingOnUi), s);
return;
}

// ... Do normal stuff. Always will be in the GUI thread.
}
}
}

HTH,
Andy
--
You can email me directly by removing the NOSPAm below
xm**********@gmxNOSPAm.netNOSPAm

Oct 23 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ron James | last post by:
I have a Form based dialog which kicks off a worker thread. The form has a progress bar, and a Cancel button. The Cancel button Aborts the thread, and when exiting, the thread attempts to Show a...
1
by: Joshua Russell | last post by:
Firstly my main method is like this: static void Main(string args) { // New Form Thread FormHandler myFormHandler = new FormHandler(); ThreadStart myThreadStart = new...
5
by: Claire | last post by:
My progress window is created by a secondary thread and then updated by it while a file is uploaded. There's an avi animation control on there that should show the move file avi. Plus a progress...
2
by: BG | last post by:
We're having trouble writing the code to update a UI control (label.Text) from a secondary thread. We're using C# with Windows Forms. We have a main form named MainForm, a splash screen form...
11
by: DW | last post by:
I've gotten this question a couple of times in interviews and I don't know what they are looking for: How do you update a control's property, such as a textbox.text property, from a thread, in...
9
by: Jervin Justin | last post by:
Hi, I've been having this problem for some time with Web Forms: I have a web app that sends data to a service when the user presses a button. Based on the data, the server will send several...
1
by: sklett | last post by:
I have been doing research on updating the UI while doing time intensive processing. Basically, I have a click event in my Form that will call a member function on one of my business objects, that...
5
by: Mark R. Dawson | last post by:
Hi all, I may be missing something with how databinding works but I have bound a datasource to a control and everything is great, the control updates to reflect the state of my datasource when I...
0
by: Code Monkey | last post by:
Suppose I have a windows form (.exe) that has a load of labels and text boxes on it. I enter a number into one of the text boxes and hit the search button. This then launches another thread,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.