There are two streams, the first displays a table of processes, while the second considers them to count and displays. Initially launched for the first, then the second:
Thread t1 = new Thread(tmh1.FillTable);
Thread t2 = new Thread(tmh1.GetGeneralInfo);
t1.Start();
t2.Start();
Here are the methods that run in threads:
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);
}
}
and the result:
0
-----------------------------------------------------------------------------
| Name | ID | threads quantity| Start time |
-----------------------------------------------------------------------------
but should be the follow:
-----------------------------------------------------------------------------
| Name | ID | threads quantity| Start time |
-----------------------------------------------------------------------------
**68**
How to make the treads run in the correct order?
Threads are supposed to run in parallel. If you want the task the second thread performs to be executed when the first thread is done, simply make the first thread execute the second task as well.
You could also use the means of the Task Parallel Library to run Tasks one after the other.