2

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?

Kasper Wittrup
  • 481
  • 1
  • 5
  • 14

5 Answers5

1

You may want to look at the new .NET Framework 4.0 Async libraries. Specifically this may be of interest to you: System.Threading.Tasks.Task<T>.

This class would be used in palce of System.Threading.Thread.

Chris Kerekes
  • 1,116
  • 8
  • 27
  • Why the down votes? This answer was posted before the author indicated that they were using the .NET Framework 3.5 and may still be helpful for somebody else visiting this page. – Chris Kerekes Jun 15 '12 at 18:33
0

Consider using events instead. Fire an event when your thread operation has completed.

Søren Lorentzen
  • 868
  • 1
  • 11
  • 26
0

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:

public event EventHandler<YourEventInformation> OnServerConnected = (s, e) => {};

Then, when serverAction is ready, you do something like:

YourEventInformation evtInfo [= new YourEventInformation();
evtInfo.CustomReturnValue = "something here";

//Async call in the UI thread
OnServerConnected.BeginInvoke(this, evtInfo, null, null);

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.

Marcel N.
  • 13,726
  • 5
  • 47
  • 72
0

Thread is not method and thread in .net designed in a way that runs asynchronously and can not return value.

But you can return value by using BackgroundWorker. You can get the result in BackgroundWorker in e.Result.

Additionally you can check this for different technique to return value from thread.

Community
  • 1
  • 1
Md Kamruzzaman Sarker
  • 2,387
  • 3
  • 22
  • 38
0

You can use this code

class Program
 {
 static void Main(string[] args)
{
    string returnValue = null;
   new Thread(
      () =>
      {
          returnValue =test() ; 
      }).Start();
    Console.WriteLine(returnValue);
    Console.ReadKey();
  }
    public static string test()
   {
     return "Returning From Thread called method";
   }
}
Shyam sundar shah
  • 2,473
  • 1
  • 25
  • 40