对于循环停止使用按钮单击 [英] For loop stops using button click

查看:73
本文介绍了对于循环停止使用按钮单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用诸如mp3播放器播放列表之类的listview项目,使用for循环,我选择并逐一移动项目.但是在运行我的应用程序时,其他按钮无法启用.在运行该forloop时,我无法执行我的应用程序.当我停止该程序时,它将停止.如何在应用程序中使用按钮单击来停止for循环.

问候
Vasanth

Hi Guys,

I am using listview items like mp3 player playlist, using for loop i select and move items one by one. but while its running my application other buttons not enable. i could n''t do anything my application while running that forloop.When i stop the program of it will stops. How can I stop that for loop using button click in my application.

Regards
Vasanth

推荐答案

您必须将复制循环移至其自己的线程.
最简单的方法是使用backgroundworker.
代码项目中有一个示例:
BackgroundWorker线程和支持取消 [
You have to move the copy loop to its own thread.
The easiest way to do that is using the backgroundworker.
There is an example here on the code project :
BackgroundWorker Threads and Supporting Cancel[^]


有最简单的方法:

首先定义一个布尔字段:
There is an easiest way:

first define a bool field:
private bool StopLoop = true;


然后将此条件添加到您的循环中:


then add this condition to your loop:

for (int i = 0; i < 30; i++)
{
    if (!StopLoop)
        // Your actions
}


private void Button_Click(object sender, EventArgs e)
{
    StopLoop = false;
}


这篇关于对于循环停止使用按钮单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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