Returning a value from thread?

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

How do I return a value from a thread?

17条回答
  •  抹茶落季
    2020-11-27 10:26

    Simply use the delegate approach.

    int val;
    Thread thread = new Thread(() => { val = Multiply(1, 2); });
    thread.Start();
    

    Now make Multiply function that will work on another thread:

    int Multiply(int x, int y)
    {
        return x * y;
    }
    

提交回复
热议问题