停止功能 [英] Stop a function

查看:71
本文介绍了停止功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个生成随机数的程序并将其放入列表框中

用户点击go。

问题是:我怎样才能停止按钮,停止运行的方法

运行???

[] s ...

I made a program that generate random numbers and put it in a listbox when
the user click go.
The problem is: how can i made a button stop, to stop the method that is
running???
[]s...

推荐答案

有几种方法,但你可以使用IAsyncResult,并使用

while循环。那段时间可以读到一个布尔。当用户点击按钮时,

将bool设置为false。通过使用另一个线程,你不会冻结UI。
http://www.knowdotnet.com/articles/delegates.html

" Ricardo" <博士******* @ terra.com.br>在消息中写道

新闻:O9 ************** @ TK2MSFTNGP12.phx.gbl ...
There are a few ways, but you could use IAsyncResult for instance, and use a
while loop. the while could read in a bool. When the user hits the button,
set the bool to false. By using another thread, you won''t freeze up the UI.
http://www.knowdotnet.com/articles/delegates.html
"Ricardo" <dr*******@terra.com.br> wrote in message
news:O9**************@TK2MSFTNGP12.phx.gbl...
我做了一个程序,当用户点击时,生成随机数并将其放入列表框中。
问题是:如何使按钮停止,停止运行的方法???

[] s ...
I made a program that generate random numbers and put it in a listbox when
the user click go.
The problem is: how can i made a button stop, to stop the method that is
running???
[]s...



>我做了一个程序,当
> I made a program that generate random numbers and put it in a listbox when
用户点击go时,生成随机数并将其放入列表框中。
问题是:如何使按钮停止,停止方法
运行???
the user click go.
The problem is: how can i made a button stop, to stop the method that is
running???



您需要在单独的线程中运行该方法。如果你按下按钮,

设置一个你在方法中测试并在这种情况下返回的标志。


-

cody


免费工具,游戏和幽默
http://www.deutronium.de.vu || http://www.deutronium.tk


You need to run that method in a separate Thread. If you press the button,
set a flag which you test in the method and return in this case.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk

嗨Ricardo,


有几种方法可以做到这一点,这完全取决于你运行

随机发生器的方式/地点,我将告诉你两个escenarios

1- gen。运行在同一个线程上。


当你生成数字时,在你生成的数字生成之间

Application.DoEvents()这个过程事件查询,你可以然后使用

停止按钮将标志设置为false,然后生成下一个数字yo

检查此标志,如下所示:


bool generate = true;

void generaterandom(object sender,EventArgs e)

{

while(generate)

{

//生成并更新UI

Application.DoEvents();

}


}

void Stop_OnClick(对象发件人,EventArgs e)

{

generate = false;

}


2-在另一个线程上运行生成器,然后Stop按钮可以暂停

线程执行,请记住,如果你使用这种方法你必须

确保在主线程中调用更新接口的调用

使用Control .Invoke


Pd:我不会为此发布代码,因为它有点复杂,只需要下载

如果你需要的话,请给我一个便条


HTH,

-

Ignacio Machin,

ignacio.machin AT dot。 state.fl.us

佛罗里达州交通局


里卡多 <博士******* @ terra.com.br>在消息中写道

新闻:O9 ************** @ TK2MSFTNGP12.phx.gbl ...
Hi Ricardo,

There are a few ways of doing it , it all depend of how/where you run the
random generator, I will show you two escenarios
1- gen. runs on the same thread.

while you are generating numbers , between number generation you call
Application.DoEvents() this process the events queries, you can then use a
Stop button that set a flag to false, before the next number is generated yo
check for this flag, like this:

bool generate = true;
void generaterandom( object sender, EventArgs e )
{
while ( generate)
{
// generate it and update the UI
Application.DoEvents();
}

}
void Stop_OnClick( object sender, EventArgs e )
{
generate = false;
}

2- run the generator on another thread and then the Stop button can pause
the thread execution, remember that if you use this approach you have to
make sure that the call to update the interface be called in the main thread
using Control.Invoke

Pd: I will not post code for this as its a little more complex, just drop
me a note if you need it

HTH,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ricardo" <dr*******@terra.com.br> wrote in message
news:O9**************@TK2MSFTNGP12.phx.gbl...
我做了一个程序,当用户点击时,生成随机数并将其放入列表框中。
问题是:如何使按钮停止,停止运行的方法???

[] s ...
I made a program that generate random numbers and put it in a listbox when
the user click go.
The problem is: how can i made a button stop, to stop the method that is
running???
[]s...



这篇关于停止功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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