有两个流,第一个显示进程表,而第二个将它们视为计数并显示。最初是为第一个启动,然后是第二个:
Thread t1 = new Thread(tmh1.FillTable);
Thread t2 = new Thread(tmh1.GetGeneralInfo);
t1.Start();
t2.Start();
以下是在线程中运行的方法:
public void FillTable()
{
while (true)
{
lock (lockObj)
{
arr.Clear();
arr.AddRange(Process.GetProcesses());
TableFormatter.Line();
TableFormatter.Row("Name", "ID", "threads quantity", "Start time");
TableFormatter.Line();
}
Thread.Sleep(interval);
}
}
public void GetGeneralInfo()
{
while (true)
{
lock (lockObj)
{
Console.WriteLine(arr.Count.ToString());
}
Thread.Sleep(interval);
}
}
结果:
0
-----------------------------------------------------------------------------
| Name | ID | threads quantity| Start time |
-----------------------------------------------------------------------------
但应该如下:
-----------------------------------------------------------------------------
| Name | ID | threads quantity| Start time |
-----------------------------------------------------------------------------
**68**
如何使踏板以正确的顺序运行?