سلام بر تمامی برنامه نویسان سی شارپ 😉 در این پست کد متوقف کردن یک thread در سی شارپ #C را آماده کردم. انشاالله که این کد سی شارپ براتون مفید باشد.
خروجی پروژه:
My Thread starting.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Stopping thread.
Main thread terminating.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Stopping thread.
Main thread terminating.
کد متوقف کردن یک thread در سی شارپ #C :
using System;
using System.Threading;
class MyThread {
public Thread thrd;
public MyThread(string name) {
thrd = new Thread(this.run);
thrd.Name = name;
thrd.Start();
}
void run() {
Console.WriteLine(thrd.Name + " starting.");
for(int i = 1; i <= 100; i++) {
Console.Write(i + " ");
Thread.Sleep(50);
}
Console.WriteLine(thrd.Name + " exiting.");
}
}
class MainClass {
using System.Threading;
class MyThread {
public Thread thrd;
public MyThread(string name) {
thrd = new Thread(this.run);
thrd.Name = name;
thrd.Start();
}
void run() {
Console.WriteLine(thrd.Name + " starting.");
for(int i = 1; i <= 100; i++) {
Console.Write(i + " ");
Thread.Sleep(50);
}
Console.WriteLine(thrd.Name + " exiting.");
}
}
class MainClass {