Click here to Skip to main content
11,432,427 members (66,978 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 Smile | :)
Posted 22-Nov-12 1:58am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
 
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);
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Nikhil@123 at 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..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.150428.2 | Last Updated 22 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100