Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Asynchronous communication between threads

Hi, I found a tutorial about asynchronous socket server, it's great but 1 thing troubles me about it...

It has separate threads for each client to receive data, these separate clients communicate with the main (GUI) thread to display received data. And it's the way of this communication that seems to be a little bit too complex to me and I wonder if it could be done is slightly different way. To transfer received text to the GUI thread it uses the following:

1.
Code
private void AppendToRichEditControl(string msg) // (the purpose of it is to allow calling the same function from client threads and main GUI thread too, it's just a wrapper for the function that actually does append the text)

2.
Code
richTextBoxReceivedMsg.BeginInvoke(new UpdateRichEditCallback(OnUpdateRichEdit), pList);


Where "UpdateRichEditCallback" is a delegate function defined as:
Code
public delegate void UpdateRichEditCallback(string text);

"OnUpdateRichEdit" is the function which actually appends the text


Summarizing, it's a complex process just because it is multithreaded, in order to create some functionality usually I would:
-create a desired function
-call a function

In this case it's:
-create a function wrapper (to detect which thread is calling the function and act accordingly)
-define and use delegate function which calls the desired function
-create desired function
-call a function wrapper

E.g.:
-private void AppendToRichEditControl(string msg)
-public delegate void UpdateRichEditCallback(string text);
-private void OnUpdateRichEdit(string msg)
-the function wrapper contains: "richTextBoxReceivedMsg.BeginInvoke(new UpdateRichEditCallback(OnUpdateRichEdit), pList);"

Another example from that code:
-private void UpdateClientListControl()
-public delegate void UpdateClientListCallback();
-void UpdateClientList()
-the function wrapper contains: "listBoxClientList.BeginInvoke(new UpdateClientListCallback(UpdateClientList), null);"

So for each communication feature between client threads and GUI thread there's a "4-step" procedure. It isn't messy in the example but if I wanted to expand the code and add more such "4-step" features I fear that the code will become a mess very quickly...

Would it be a good way to replace "AppendToRichEditControl(msg)" procedure with something like: "ParseClientMsgAndTakeAction_from_GUI_thread(client_number_id, msg)"? So it would be decided on the GUI thread what to do with it and there would be only 1 or 2 of these "4-step" communicational procedures instead. Would that be a proper way of implementing it? Or would it be just too much work for the GUI thread to parse all messages from all the other threads?
This can be easily done with a single function :P

The Control.InvokeRequired property lets you know if you're on a thread other than the one used to create the form or control
https://msdn.microsoft.com/en-us/library....110).aspx

Code
private void UpdateRichTextBox(string text) {
   if (InvokeRequired)
       BeginInvoke(new Action<string>(UpdateRichTextBox), text);
   else {
       // On GUI Thread
   }
}

This way the method can be called from any thread
If its on the GUI thread, it just updates the text
If its on any other thread, it will invoke the GUI thread to update the text
Use the async/await Task pattern.

The Task is the wrapper for your async method calls.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by SigmaApr 28, 2021 06:50 PM 0 87 Apr 28, 2021 06:50 PM
Last Post: Sigma
[Python] by SympthDec 4, 2020 02:04 AM 15 328 Dec 4, 2020 02:04 AM
Last Post: Sympth
[C#] by PaskowskyMay 9, 2020 11:33 AM 6 428 May 9, 2020 11:33 AM
Last Post: SaketSaket