Как изменить значение в потоке при его запуске C#
Я хочу изменить значение enum для всех потоков в моем приложении во время выполнения вот мой код :
public class UsingData { //do staff public enum DataWorkState { RunWork, StopWork, Break, None } } public class Worker { //do Staff public Thread RunWorker; public DataWorkState WorkerState; public Worker() { RunWorker = new Thread(StartWork); WorkerState = DataWorkState.None; } Privet void StartWork() { lock(RunWorker) { while(WorkerState == DataWorkState.RunWork) { //do staff } while(WorkerState == DataWorkState.StopWork) { //do staff } while(WorkerState == DataWorkState.Break) { //do staff } } } } public class form1:Form { List<Worker> runWorkers; public form1() { ...... runWorkers = new List<Worker>(); } privet void btonRunWork_Click(object sender,EventArgs e) { for(int i = 0; i < runWorkers.Count; i++) { runWorkers[i].WorkerState = DataWorkState.RunWork; if(!RunWorker.IsAlive) runWorkers[i].RunWorker.Start(); } } privet void btonBreak_Click(object sender,EventArgs e) { for(int i = 0; i < runWorkers.Count; i++) { runWorkers[i].WorkerState = DataWorkState.Break; if(!RunWorker.IsAlive) runWorkers[i].RunWorker.Start(); } } }
теперь, когда я нажмите кнопку перерыв после того, как я нажмите кнопку RunWork все темы, продолжайте делать как
DataWorkState.RunWork
ничего не меняется во всех потоках как я могу это исправить
Что я уже пробовал:
public class UsingData { //do staff public enum DataWorkState { RunWork, StopWork, Break, None } } public class Worker { //do Staff public Thread RunWorker; public DataWorkState WorkerState; public Worker() { RunWorker = new Thread(StartWork); WorkerState = DataWorkState.None; } Privet void StartWork() { lock(RunWorker) { while(WorkerState == DataWorkState.RunWork) { //do staff } while(WorkerState == DataWorkState.StopWork) { //do staff } while(WorkerState == DataWorkState.Break) { //do staff } } } } public class form1:Form { List<Worker> runWorkers; public form1() { ...... runWorkers = new List<Worker>(); } privet void btonRunWork_Click(object sender,EventArgs e) { for(int i = 0; i < runWorkers.Count; i++) { runWorkers[i].WorkerState = DataWorkState.RunWork; if(!RunWorker.IsAlive) runWorkers[i].RunWorker.Start(); } } privet void btonBreak_Click(object sender,EventArgs e) { for(int i = 0; i < runWorkers.Count; i++) { runWorkers[i].WorkerState = DataWorkState.Break; if(!RunWorker.IsAlive) runWorkers[i].RunWorker.Start(); } } }