C# Run two methods at the same time | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

C# Run two methods at the same time

I want to execute this "string" at the same time one underneath the other? what's wrong? using System; using System.Threading; namespace ConsoleApplication1 { class MainClass { public static void Main() { func(); } public static void func() { // This code is on thread 1 Thread ta = new Thread(new ThreadStart(() => LoopA("MMMMMMMM MMMMMMMM"))); // create thread 2 for LoopA Thread tb = new Thread(new ThreadStart(() => LoopB("MMMMMMMMM MMMMMMMMM"))); // create thread 3 for LoopB ta.Start(); // Run LoopA on thread 2 tb.Start(); // Run LoopB on thread 3 // Join makes this thread wait until the thread being joined to has finished ta.Join(); // wait for thread 2 to finish tb.Join(); // now wait for thread 3 to finish // From this point on we know that both loops have completed } public static void LoopA(string print) { foreach (char l in print) { Console.Write(l); Thread.Sleep(25); } } public static void LoopB(string print) { Console.Write("\n"); foreach (char l in print) { Console.Write(l); Thread.Sleep(25); } } } }

25th Dec 2018, 9:37 PM
Matheus Campos
Matheus Campos - avatar
3 Answers
0
Can you send the code separately (By using link)?
25th Dec 2018, 11:09 PM
ShortCode
0
how i do this?
26th Dec 2018, 1:04 AM
Matheus Campos
Matheus Campos - avatar
26th Dec 2018, 1:10 AM
Matheus Campos
Matheus Campos - avatar