c# - Infinite while loop issues -


i barely started programming in c# , running small issue background threads running infinite while loops inside of them.

a little background: trying make physical notification light outlook. right making in wpf application test before throw in outlook add-in.

i have button opens new background thread , runs new instance of class:

public class lightcontrol {     private byte light;      public byte light     {         { return light; }         set { light = value; }     }      private string color;      public string color     {         { return color; }         set { color = value; }     }      private int delay;      public int delay     {         { return delay; }         set { delay = value; }     }     private bool status;      public bool status     {         { return status; }         set { status = value; }     }      public void notification()     {         action<byte, string, int, bool> lightnot =                                     delegate(byte i, string c, int d, bool s)                                     { lightloop(i, c, d, s); };         lightnot(light, color, delay, status);     }       private void lightloop(byte index, string color, int delay, bool state)     {         blinkstick device = blinkstick.findfirst();         if (device != null && device.opendevice())         {             while (state)             {                 device.setcolor(0, index, color);                 thread.sleep(delay);                 device.setcolor(0, index, "black");                 thread.sleep(delay);             }           }     }  } 

i have button feeds false status member of class should trickle down private method lightloop , kill infinite loop. killing entire thread in process.

it isn't doing though. loop continues run. cause of that?

any on appreciated.

here how start thread:

 private void lightstart(byte index)     {         random random = new random();         int randnumber = random.next(0, 5);         lightcontrol light = new lightcontrol();         light.light = index;         light.color = colors[randnumber]; //random color generator fun.         light.delay = 500;         light.status = status; //global bool on mainwindow class         thread lightthread = new thread(new threadstart(light.notification));         lightthread.isbackground = true;          lightthread.start();       } 

i apologize in advance inaccuracy in terminology still wrapping head around of this.

another option using ref achieve want. essentially, allows pass reference bool instead of actual value @ time of evaluation. allows changes value affect method passed to, if value type.

here's basic example, can take concepts , apply them situation.

static bool state = true;  public static void _main(string[] args) {     console.writeline("1. main thread: {0}", datetime.now.tostring("hh:mm:ss.fffffff"));     thread t = new thread(new threadstart(startloop));     t.start();     console.writeline("2. main thread: {0}", datetime.now.tostring("hh:mm:ss.fffffff"));     thread.sleep(5000);     state = false;     console.writeline("3. main thread: {0}", datetime.now.tostring("hh:mm:ss.fffffff")); }  delegate void mydelegate(ref bool s);  public static void startloop() {     mydelegate mydelegate = loopmethod;     mydelegate(ref state); }  public static void loopmethod(ref bool state) {     while (state)     {         console.writeline("loopmethod running: {0}", datetime.now.tostring("hh:mm:ss.fffffff"));         thread.sleep(1000);     } } 

output:

1. main thread: 20:00:28.3693277 2. main thread: 20:00:28.3753284 loopmethod running: 20:00:28.3793309 loopmethod running: 20:00:29.3813856 loopmethod running: 20:00:30.3816387 loopmethod running: 20:00:31.3818790 loopmethod running: 20:00:32.3829344 3. main thread: 20:00:33.3760889 

nothing else printed.

this requires changing delegate anonymous delegate strongly-typed delegate.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -