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;
}
}
Stop a thread in CTRL
- adaneau
- Posts:310
- Joined: Tue Feb 21, 2012 9:49 am
Re: Stop a thread in CTRL
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
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
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.
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
Hi,
Strange, which version are you using? I tried it with 3,16 P012.
Find below my code:
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
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;
}
}
BR
Alexandre
- ange.ogawin
- Posts:18
- Joined: Thu Mar 14, 2019 10:51 am
Re: Stop a thread in CTRL
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
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
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
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
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.
me to stop the thread simulateC200.
Thank you.