Stop a thread in CTRL

Find and share HowTos to various installations / configurations!
7 posts • Page 1 of 1
ange.ogawin
Posts:18
Joined: Thu Mar 14, 2019 10:51 am

Stop a thread in CTRL

Post by ange.ogawin »

Hi,
I'm trying to stop a thread with the following code, in the clicked event of a push button.
I get the error "Invalid argument in function" for the line : stopThread(id);
i checked and id is 1.
Some ideas to solve this?

PS: the startThread() works.

#uses "simulate_c200.ctl"
bool simuEnCours=false;
int id;
main(mapping event)
{
if(simuEnCours==false)
{
id=startThread("simulateC200");//function in simulate_c200.ctl file.
simuEnCours=true;
this.text("Stop simulator");
}
else{
stopThread(id);
this.text("Start simulator");
simuEnCours=false;
}

}

adaneau
Posts:310
Joined: Tue Feb 21, 2012 9:49 am

Re: Stop a thread in CTRL

Post by adaneau »

Hi,

You should put
bool simuEnCours=false;
int id;

inside scope lib, otherwise this will not be shared to the next click of your button as it is redeclared at each click.

I tried it and it works great ;)

BR
Alexandre Daneau

ange.ogawin
Posts:18
Joined: Thu Mar 14, 2019 10:51 am

Re: Stop a thread in CTRL

Post by ange.ogawin »

I just did like you said. But it's stil not working. It's strange because i don't need to put bool simuEnCours in scope lib to get the correct value.
I say it because the button text is changing right at each click.

adaneau
Posts:310
Joined: Tue Feb 21, 2012 9:49 am

Re: Stop a thread in CTRL

Post by adaneau »

Hi,

Strange, which version are you using? I tried it with 3,16 P012.

Find below my code:

Code: Select all

─// [(Panel)] [0] - [ScopeLib]
bool simuEnCours=false;
int id;

void simulateC200()
{
  while(1)
  {
    DebugN("hello world");
    delay(1);
  }
}
════════════════════════════════════════════════════════════════════════════════════════════════════
─// [PUSH_BUTTON1] [1] - [Clicked]
main(mapping event)
{
if(simuEnCours==false)
{
id=startThread("simulateC200");//function in simulate_c200.ctl file.
simuEnCours=true;
this.text("Stop simulator");
}
else{
stopThread(id);
this.text("Start simulator");
simuEnCours=false;
}
}
It is inside a very classic panel. I replaced your function by an infinite loop. If you try it you'll see the debug stopping as soon as you press button second time, meaning that the thread has been stopped.

BR
Alexandre

ange.ogawin
Posts:18
Joined: Thu Mar 14, 2019 10:51 am

Re: Stop a thread in CTRL

Post by ange.ogawin »

Thank you very much for your answer. Your code is working when i test it. The problem with my code is maybe that the function simulateC200 is in a ctl file.
In addition, this function starts others threads.I also test to copy paste the simulateC200 function in scope lib, the same error appears when i call stopThread.
I'm using a 3.16 version

adaneau
Posts:310
Joined: Tue Feb 21, 2012 9:49 am

Re: Stop a thread in CTRL

Post by adaneau »

Hi,

I pushed the test further, putting function in a ctl lib and having #uses "myLib" in my scope lib. Still working like a charm.

Are you sure that your thread still runs when you try to stop it? If you run subthreads I would highly recommend you to be clean and have consequent amount of waitThread() to ensure that the program is really doing what you want. Classic rule from programming, dont let your threads run wild ;)

BR
Alexandre Daneau

ange.ogawin
Posts:18
Joined: Thu Mar 14, 2019 10:51 am

Re: Stop a thread in CTRL

Post by ange.ogawin »

It's now solved. You were right,my main thread simulateC200 was ending before the others started threads. Using waitThread() allowed
me to stop the thread simulateC200.

Thank you.

7 posts • Page 1 of 1