Click here to Skip to main content
15,868,128 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to change enum value to all threads in my app at run time here is my code :

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();
    }
  }
}


now when i click Break button after i click RunWork all Threads keep do as
C#
DataWorkState.RunWork


nothing change in all the threads how i can fix this

What I have tried:

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();
    }
  }
}
Posted
Updated 28-Feb-18 6:09am

Mark the field as volatile[^]:
C#
public volatile DataWorkState WorkerState;
 
Share this answer
 
Comments
Member 7912784 1-Mar-18 10:39am    
thnx a lot volatile field works fine
Um. Nothing will happen: you create a list of your class, but you don't populate it with anything. As a result the list is always empty, and your loops never enter the body - the runWorkers.Count property is always zero.
 
Share this answer
 
Comments
Member 7912784 28-Feb-18 11:39am    
Um. i manged the code i didn't type every thing. there is a method that add the list when the form load I'm sure that at less there is 5 object in the list before i click the button.

the problem is not at list objects the problem is after is start any one of states (ex. DataWorkState.RunWork,...) ican't change it again even if i Suspend the thread and resume it. like the thread is looked in the first state it start with
OriginalGriff 28-Feb-18 11:53am    
Don't "be sure" - use the debugger and check it! And then check exactly what is happening as you go round the loop.
We can't do that for you - you clearly haven't given us all the code, so we have no way to run it under the same condition you are.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900