I am working on a C# windows application. This application is mainly related to sockets.
I have a class called tcpsocket, which handles all socket level functionality of sending, receiving data, etc. A controller class calls this tcpsocket. In addition, it does all other work of logging data to a file and updating GUI. I thought it will be good to have this controller in the backgroundworker, to ensure that gui is responsive at all times. First of all, is it a good idea to do this?
As i am trying to do this, i am facing a problem regarding the progresschanged handler for the backgroundworker. I want to be able to display connection status (type: String) in a text field and data being sent/received by the application (type: Byte[]) in richTextBox. Since the controller can only send data to gui through the progressChanged event, how do i pass different types of data (String/byte[]) to the gui?
If you decide to run the controller on a different thread other than the UI thread you will need to hook the progressChanged event from the controller to a method handler on the UI thread. Once you have done this you will have to marshal the data returned from the processChanged event to the UI thread. Below is an example of how to do the marshaling.
Additionally you might need to modify your controller and add different events which might return something other than a type:byte[]. You can do this by simply be leveraging the generic delegate class which is part of the .net framework or you can leverage the delegate class directly. Here is the Microsoft documentation on delegates.
Enjoy!