Jul 20, 2018 04:41 AM
(This post was last modified: Jul 20, 2018 07:46 AM by michalmonday.)
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.
2.
Where "UpdateRichEditCallback" is a delegate function defined as:
"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?
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?


