[ad_1]
実行時に列挙値をアプリ内のすべてのスレッドに変更したいのは、ここに私のコードです:
C#
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をクリックした後にBreakボタンをクリックすると、すべてのスレッドがそのまま維持されます
C#
DataWorkState.RunWork
すべてのスレッドで何も変更されていません。これを修正するにはどうすればよいですか
私が試したこと:
C#
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(); } } }
解決策 1
うーん。 クラスのリストを作成しますが、何も入力しません。 その結果、リストは常に空になり、ループが本体に入ることはありません – runWorkers.Count プロパティは常にゼロです。
解決策 2
[ad_2]
コメント