Returning a value from thread?

前端 未结 17 2423
不思量自难忘°
不思量自难忘° 2020-11-27 09:45

How do I return a value from a thread?

17条回答
  •  攒了一身酷
    2020-11-27 10:17

    With the latest .NET Framework, it is possible to return a value from a separate thread using a Task, where the Result property blocks the calling thread until the task finishes:

      Task task = Task.Factory.StartNew(() =>
      {
          string s = "my message";
          double d = 3.14159;
          return new MyClass { Name = s, Number = d };
      });
      MyClass test = task.Result;
    

    For details, please see http://msdn.microsoft.com/en-us/library/dd537613(v=vs.110).aspx

提交回复
热议问题