C# 如何同时运行两个方法?
我尝试过使用system.threading.task.task.factor.startnew,但它仍然会暂停整个过程C# 如何同时运行两个方法?,c#,coroutine,C#,Coroutine,我尝试过使用system.threading.task.task.factor.startnew,但它仍然会暂停整个过程 using System; namespace test { class MainClass { public static void test2 () { System.Threading.Thread.Sleep(500); Console.WriteLine("Test"); } public stat
using System;
namespace test
{
class MainClass
{
public static void test2 ()
{
System.Threading.Thread.Sleep(500);
Console.WriteLine("Test");
}
public static void Update ()
{
System.Threading.Thread.Sleep(1);
Console.WriteLine("testt");
}
public static void Main ()
{
while (true) {
System.Threading.Tasks.Task.Factory.StartNew (() => test2 ());
System.Threading.Tasks.Task.Factory.StartNew (() => Update ());
}
}
}
}
我做错了什么?您需要使用两个线程
class MainClass
{
public static void Main()
{
Task.Factory.StartNew(MainClass.test2);
Task.Factory.StartNew(MainClass.Update);
Console.ReadLine();
}
public static void test2()
{
while (true)
{
System.Threading.Thread.Sleep(500);
Console.WriteLine("Test");
}
}
public static void Update()
{
while (true)
{
Console.WriteLine("Hello");
System.Threading.Thread.Sleep(250);
}
}
}
你想干什么?更新是否需要调用(调用)test2?他们需要同时启动吗?您研究过使用线程吗?Task2需要由update调用,并且它们不需要同时启动。我试过使用线程,但是关于它们的信息很少。相反,关于启动线程的信息很多。google/bing c#线程、任务、tpl、委托。顺便说一句,我觉得有趣的是,这是因为引用了一个被搁置的问题而被关闭的重复项…go figure:/Get此错误:名称“task”在当前上下文中不存在通过将task.Factory.StartNew更改为System.Threading.Tasks.task.Factory.StartNew修复了它。