简体   繁体   English

如何检查按钮是否在C#中的Windows窗体应用程序中的另一个按钮单击事件内被单击

[英]how to check whether a button is clicked, inside another button click event in windows form application in C#

well i am having two buttons on a form and I want to start data transfer with the first button and stop on the press of a second button. 好吧,我在表单上有两个按钮,我想从第一个按钮开始数据传输,而在按下第二个按钮时停止。

code is: 代码是:

private void stdaq_Click(object sender, EventArgs e)
{
    stopped = false;

    //while (stopped == false)

    if (sender == spdaq)
    {
        stopped = true;
        ///break;
        Process();
    }
    else if (sender == stdaq)
    {
        Process();
    }
}

here stdaq is the start button and spdaq is the stop button, the process function is a function which i am implementing and in that with the stopped variable of bool type i am implementing two different functions inside process method, but i want to continually check whether the stop button is pressed or not but here with this code i got no success. 在这里,stdaq是开始按钮,而spdaq是停止按钮,过程函数是我正在实现的函数,在bool类型的stoped变量中,我正在过程方法内部实现两个不同的函数,但我想不断检查是否停止按钮是否按下,但是在此代码下我没有成功。

so please help me with how to pass the value true to the stopped variable inside the event click function of start button itself on the press of stop button. 所以请帮助我如何在按下“停止”按钮时将“ true”值传递给“开始”按钮本身的事件单击功能内的“停止”变量。

Use two separate Handlers for the start and the stop button. 使用两个单独的处理程序作为开始和停止按钮。 This makes your logic much simpler to follow. 这使您的逻辑更容易遵循。 Then do soemthing like this: 然后做这样的事情:

private void stdaq_Click(object sender, EventArgs e) // Start
{
    Process(true);
}

private void spdaq_Click(object sender, EventArgs e) // Stop
{
    Process(false);
}

Or even better: Create two seperate Methods StartProcess() and StopProcess() . 甚至更好:创建两个单独的方法StartProcess()StopProcess()

Create cancellation token, start asynchronous Task in button start event handler put your method in this Task, pass reference to this cancellation token and use it to stop this task in Stop button event handler when you'll need it later. 创建取消令牌,在按钮启动事件处理程序中启动异步Task,将您的方法放在此Task中,传递对该取消令牌的引用,并在以后需要时使用它在Stop按钮事件处理程序中停止该任务。

More information : https://msdn.microsoft.com/en-us/library/jj155759.aspx 更多信息: https : //msdn.microsoft.com/zh-cn/library/jj155759.aspx

Example of how you can use it: 如何使用它的示例:

    static CancellationTokenSource cts;
    static Task t;

    private void Method()
    {
        while (!cts.IsCancellationRequested) 
        {
             // your logic here
        }
        t = null;
    }

    private void stdaq_click (object sender, EventArgs e)
    {           
       if(t != null) return;
       cts = new CancellationTokenSource();
       t = new Task(Method, cts.Token, TaskCreationOptions.None);
       t.Start();
    }

    private void spdaq_Click(object sender, EventArgs e) 
    {
       if(t != null) cts.Cancel(); 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查表单上的按钮单击事件-C# - Check for Button Click Event on Form - C# 从onother子窗体通过按钮单击事件Windows应用程序c#打开新的子窗体 - Open new child form from onother child Form By Button Click Event Windows application c# 如何在Windows窗体应用程序中单击按钮时运行另一个Windows窗体 - How to run another windows form on button click in windows form application C#Windows Form应用程序中每次单击时按钮文本的更改 - Button Text Change on Every Click in C# Windows Form Application 单击Windows窗体应用程序C#按钮,关闭特定的窗体 - Windows Forms Application C# button on click close specific form 在按钮上单击以其他窗口形式显示面板c# - Displaying a panel in another windows form on button click c# C# 表单应用程序 - 单击另一个按钮即可停止正在进行的进程 - C# Form Application - Stop an ongoing process with a click of another button 如何获取,在C#Winform应用程序中单击表单上的哪个按钮 - how to get, which button is clicked on form in c# winform application C#Windows窗体-如何通过不同的按钮单击事件重用对象 - C# Windows Form - How to Reuse an Object from a Different Button Click Event 检查是否单击了另一个窗口中的按钮 - check whether a button in another window is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM