How to implement data exchange between the main loop and threads? Suppose there is such a pseudocode Example:
namespace First { public class Test { public static string Outout1 = string.Empty; private static System.Threading.Thread SThread; public static string Func1() { pass; } public static void GetTimeNow() { for(;;){ Outout1 += Func1(); System.Threading.Thread.Sleep(4000); } } public static string GetTimeNow2() { pass; } private static void RunThread() { if (SThread == null || SThread.ThreadState != System.Threading.ThreadState.Running) { SThread = new System.Threading.Thread(GetTimeNow); SThread.Start(); } } public static void MainFunc() { for(;;){ Outout1 += GetTimeNow2(); RunThread() ; System.Threading.Thread.Sleep(5000); } } } } The main function MainFunc is MainFunc into an infinite loop in this loop and we call the function GetTimeNow2 . Which returns us data to the variable Outout1 . Next, it runs the RunThread function RunThread which runs the GetTimeNow function in a separate thread.
The GetTimeNow function GetTimeNow infinite loop, and it calls Func1 , which writes data to Outout1 .
The question is whether the correct data will always be in the variable Outout1 , namely whether the data from GetTimeNow2 and GetTimeNow will be visible and if you reset it at the end of the loop, it will also be reset in GetTimeNow which is running in a separate thread? Example:
for(;;){ Outout1 += GetTimeNow2(); RunThread(); System.Threading.Thread.Sleep(5000); Outout1 = ""; } Please tell me how to implement this method correctly, so that you can work with the Outout1 variable both in the main loop and in the loop that is running in a separate thread? and how to do it if you start not 1 stream but 2-5 Example:
for(;;){ Outout1 += GetTimeNow2(); RunThread(); RunThread1(); RunThread2(); System.Threading.Thread.Sleep(5000); Outout1 = ""; }