Updating a form control from another thread

  • Thread starter Thread starter John J. Hughes II
  • Start date Start date
J

John J. Hughes II

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
 
John said:
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
 
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 said:
John said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top