我正在尝试将 test1 源代码更改为 test2 源代码。 两个源代码有什么区别吗? 哪种代码效率更高
public class Test1
{
public static Test1 Instance { get; } = new Test1();
public event EventHandler EventSomething;
public Test1()
{
Thread thread = new Thread(new ThreadStart(Run));
thread.IsBackground = true;
thread.Start();
}
void Run()
{
while (true)
{
Thread.Sleep(5 * 1000);
EventSomething?.Invoke(this, new EventArgs());
}
}
}
public class Test2
{
public static Test2 Instance { get; } = new Test2();
public event EventHandler EventSomething;
public Test2()
{
Run();
}
async void Run()
{
await Task.Delay(5 * 1000);
EventSomething?.Invoke(this, new EventArgs());
Run();
}
}
我觉得没有什么区别