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