I have a windows form where in I open new threads for server communication.
I need to write in the form (in a textbox) how the server has responded to the query.
At the moment I do the server communication as follows:
ServerClass SC = new ServerClass(param);
new Thread(new ThreadStart(SC.serverAction)).Start()
The serverAction as it is now, is a void method within the ServerClass class, but I can ofcourse make that return a value if needed. However Im leaning more to delegates, but im not quite sure how to call back from the other thread…
The ServerClass is a simple class using WCF as follows:
public class ServerClass
{
private string var1;
private string var2;
public ServerClass(string var1, string var2)
{
this.var1 = var1; this.var2 = var2;
}
public void serverAction()
{
//WCF here
}
}
Any suggestions how I can get a value from the “serverAction()” method?
Well, you can’t exactly return a value from the thread’s entry point.
What I would suggest is some kind of async communication between the Server class.
You can define an event in your Server class, something like:
Then, when serverAction is ready, you do something like:
Next, you need to bind to this event from somewhere from the UI (probably the Window).
There you need to just call your code in the UI thread’s context, by first checking Dispatcher.CheckAccess and then invoking your UI code.
Something very similar with what is described here.