Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

Hi Friends, I am working on a project called Computer Scanner, I have added two buttons in my application, The first button is for scanning the computer drive and the another button is for stop the scanning process. For example Antivirus when we scan our computer and at a time we can stop the scan as well by js clicking the stop button, i wants to implement the same behaviour to my project as well.

Plz Help,

Thanks in Advance :)
Posted

Threads are possible, but not necessary. Threading[^] may be necessary because of other circumstances. But simply killing a thread to stop a background operation may result in issues described here[^].

The start button starts a method. The stop button cannot directly stop this method. But the method can be created with the possibility in mind that there may be a need to stop it.

To implement this, the method has to repeatedly check if it should continue. Looping or recoursive methods can easily be built that way.
C#
private bool continue = true;


private void StopButton_Click(object sender, EventArgs e)
{
    continue = false;
}


private void StartButton_Click(object sender, EventArgs e)
{
    continue = true;
    LoopThroughNumbers(); // or RecourseThroughNumbers(0);
}


private void LoopThroughNumbers()
{
    for(int i = 0; i < 10000; i++)
    {
        if(!continue)
        {
            break;
        }
        
        DoSomethingWith(i);
    }
}


private void RecourseThroughNumbers(int i)
{
    if(!continue)
    {
        return;
    }

    if(i >= 10000)
    {
        return;
    }

    DoSomethingWith(i);
    RecourseThroughNumbers(i + 1);
}
 
Share this answer
 
v2
hi there
you can do this by thread concept .
if supposing that we want run a method that run for ever and another button want to stop
this running method that started by button1 you can kill the thread that running by button1 by button2[for example onclick code].
you solution same be this.
 
Share this answer
 
Comments
Nikhil@123 22-Nov-12 7:10am    
Thanks for Replying,

I did not use the treading concept in my application, is there any other way to do so,,, plz replly,,,thanks..
If you don't want to use the most obvious Threading concept.

Here is one other way ::>

Use keyboard hooking concept and use API unmanaged calls to implement the functions.
Now create a delegate to handle the keypress event.
Last step is to check for keypress before every iteration.

Its a bit tedious to implement but will defenitly do the JOB.

Things needed > Windows API calls
> Delegates
 
Share this answer
 

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