HelloWorld Multithreaded C#app,具有双输入和输出

时间:2018-02-03 13:20:25

标签: c#

考虑以下来自HelloWorld Multithreaded C# app

的示例
using System;
using System.Threading;
class ThreadTest
{
  static void Main()
  {
    Thread t = new Thread (WriteY);          // Kick off a new thread
    t.Start();                               // running WriteY()

    // Simultaneously, do something on the main thread.
    for (int i = 0; i < 1000; i++) Console.Write ("x");
  }

  static void WriteY()
  {
    for (int i = 0; i < 1000; i++) Console.Write ("y");
  }
}

有没有办法让“static double WriteY(double a)”这样的函数代替“static void WriteY()”?

1 个答案:

答案 0 :(得分:0)

我不确定你想用Write方法的返回值做什么,但我认为我得到了你的意图并修改了你的代码片段来解决你所寻找的问题:

class ThreadTest
{
  static void Main()
  {
    Task t = Task.Run(() => Write(5));

    // Simultaneously, do something on the main thread.
    for (int i = 0; i < 1000; i++) Console.Write ("x");
    t.wait();

  }

  static double Write(double a)
    {
        for (int i = 0; i < 1000; i++) Console.Write("y");
        return a;
    }
}