Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to execute a function again and again. Means when first time execution finished,then second time execution will be started. Clicking on start button I can start. But how to stop execution clicking on stop button.
Help I'm getting is to apply condition. But here no condition can be applied. Just a button click will stop it.
Please give a small example how to do it.
Posted
Comments
Salman622 8-Dec-15 6:24am    
In this case you use a backgroung worker in c# to execute the function on button click.
and to stop it on button click you can stop the background worker thats it.
souvikcode 9-Dec-15 2:26am    
Background worker is most fine. Its working. My problem solved.

Just tell me when I start background worker from button click by run_async() method,its started and do a looping function. The thing is that the function is here performing in different thread than UI. But is the function is creating own threads when it is being called by for loop every time?
For example----
for(int i=0;i<100;i++)
{
longTask();
}

longTask() is the method is called over and over in different thread than UI,say it is called in thread2.
Now when one longTask() is performing for say 5 sec,then is there any chance to another thread be created or the for loop waits until the function finished?
Salman622 9-Dec-15 3:23am    
take a boolean variable at global level in form assign value true to it
and in your background worker do work event
apply a check like below
if(boolVariable)
{
boolVariable=false;
for(int i=0;i<100;i++)
{
longTask();
}
}

and in your RunWorkerComplete event just do that boolVariable to true
thats it
souvikcode 9-Dec-15 3:40am    
Its fine to prevent calling before its finished. Thanks. But is there any chance of being called of the function until its finished?
Salman622 9-Dec-15 4:03am    
no it would not happen
until you click the start process button again and if you click it again there would be a error i.e.
This BackgroundWorker is currently busy and cannot run multiple tasks concurrently
to prevent this your can apply a check in your start process click event

1 solution

Hi,

You can try to do two things:
1. Set a boolean global variable and, between a call and the other of your function, if that variable equals to true, stop the execution. The button will make that variable equal to true.

C#
public bool ShouldStop = false;

public void MyFunction(){
    if (!ShouldStop)
        //Execute
    else
        return;
}


2. As proposed by Salman622 in the comments, use a BackgroundWorker or a new thread for that function and call Abort when the button is clicked.

Hope it helps.

LG
 
Share this answer
 
Comments
souvikcode 8-Dec-15 22:54pm    
Your solution is imperfect as it will hang the main ui. And as it is not in different thread,I can't change variable value.
LLLLGGGG 9-Dec-15 10:32am    
Put the function MyFunction in another thread (or use the async/await) pattern) and you're done! (If you put it in another thread, make sure to set the bool variable to volatile by prepending to bool the keyword "volatile".

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