This is a technique I've used for years to receive network data and use it in my GUI (dialog, form etc).
public delegate void mydelegate(Byte[] message);
public ReceiveEngineCS(String LocalIpIn, String ReceiveFromIp, mydelegate d)
{
this.m_LocalIpIn = LocalIpIn;
this.m_ReceiveFromIp = ReceiveFromIp;
m_MainCallback = d;
SetupReceive();
m_Running = true;
//Create the Track receive thread and pass the parent (this)
m_RtdirReceiveThread = new Thread(new ParameterizedThreadStart(MessageRecieveThread));
m_RtdirReceiveThread.Start(this);
}
private void MessageRecieveThread(System.Object obj)
{
ReceiveEngineCS parent = (ReceiveEngineCS)obj;
while(parent.m_Running)
{
Byte[] receiveBytes = new Byte[1500];
try
{
receiveBytes = parent.m_ClientReceiver.Receive(ref parent.ipEndPoint);
parent.ThreadOutput(receiveBytes);
}
catch ( Exception e )
{
parent.StatusUpdate(e.ToString());
}
}
}
public void ThreadOutput(Byte[] message)
{
m_MainCallback(message);
}
public partial class SystemMain : Form
{
//Local Class Variables
Network.ReceiveEngineCS SystemMessageReceiver;
private void Form1_Load(object sender, EventArgs e)
{
//Load up the message receiver
SystemMessageReceiver = new Network.ReceiveEngineCS(localAddy, fromAddy, new mydelegate(LocalDelegate));
}
public void LocalDelegate(Byte[] message)
{
if (Form.ListView.InvokeRequired)
{
//External call: invoke delegate
Form.ListView.Invoke(new mydelegate(this.LocalDelegate), message);
}
else
{
//Get the Packet Header
Formats.PacketHeaderObject ph = new Formats.PacketHeaderObject(message);
//Update or Add item to Specific ListView
... update views
}
}
}
The Receiver takes in anywhere between 10 and 100 real-time messages a second and often more.
I've been doing research lately into .Net 4.0 and C# and noticed many other similar ways to do this data processing ,such as Worker Threads, and other ways of using the Delegate and Invoke.
My question... Is there a more efficient way in the newer .Net Libraries (3.5, 4.0 etc) to do this data receive / GUI updating?
I think this method isn't working as well with C#.
Any help would be appreciated.