如何通过单击另一个按钮来停止运行事件的按钮. [英] How to Stop Button running Event by Clicking another button.

查看:140
本文介绍了如何通过单击另一个按钮来停止运行事件的按钮.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,

朋友们,我正在一个名为Computer Scanner的项目中,我在应用程序中添加了两个按钮,第一个按钮用于扫描计算机驱动器,另一个按钮用于停止扫描过程.例如,当我们扫描计算机时使用防病毒软件,一次我们也可以通过js单击停止"按钮来停止扫描,我也想对我的项目实施相同的行为.

请帮助,

在此先感谢:)

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 :)

推荐答案

线程是可能的,但不是必需的. 线程 [ ^ ]由于其他情况可能是必要的.但是简单地杀死线程以停止后台操作可能会导致在此处中描述的问题. [ ^ ].

开始按钮启动一个方法.停止按钮不能直接停止此方法.但是可以在考虑可能需要将其停止的可能性的情况下创建该方法.

为了实现这一点,该方法必须反复检查它是否应该继续.循环或循环方法可以很容易地以这种方式构建.
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);
}


您好,
您可以通过线程概念来实现.
如果假设我们要运行一个永远运行的方法,而另一个按钮想要停止
由button1启动的此运行方法,您可以通过button2杀死由button1运行的线程[例如onclick代码].
您也可以这样解决.
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.


如果您不想使用最明显的Threading概念.

这是另一种方式:::>

使用键盘挂钩概念并使用API​​非托管调用来实现功能.
现在创建一个委托来处理keypress事件.
最后一步是在每次迭代之前检查是否有按键按下.

实施起来有点繁琐,但会毫无保留地完成工作.

需要的东西> Windows API调用
>代表们
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


这篇关于如何通过单击另一个按钮来停止运行事件的按钮.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆