gpt4 book ai didi

c# - 如何使用 BackGroundWorker 连续执行依赖于用户输入停止的方法?

转载 作者:太空宇宙 更新时间:2023-11-03 21:51:03 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
How To Start And Stop A Continuously Running Background Worker Using A Button

我有 2 个按钮,第一个是“Continuous”.. 第二个是“Stop”

我想在按下连续按钮时调用一个方法:

private void continuous_Click(object sender ,EvantArgs e) 

{

// continuous taking pictures ...

}

我的问题是:如何按停止按钮停止执行??

我写了一个拍照的代码,拍照成功了...现在我想让相机拍摄连续快照......但是如果我按下停止按钮相机应该停止拍照......

我使用了 BackGroundWorker,但代码不起作用!!!

这是代码:

private void ContinousSnaps_Click(object sender, EventArgs e)
{

Contiguous.DoWork += Contiguous_DoWork;
Contiguous.RunWorkerCompleted += Contiguous_RunWorkerCompleted;
Contiguous.RunWorkerAsync();
}

private void Contiguous_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; ; i++) TakeSnapShotCommand();
}

private void Contiguous_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
MessageBox.Show("complete");
}

//------------------------------------------------------------------//

private void Stop_Click(object sender, EventArgs e)
{
Contiguous.CancelAsync();


}

//--------------------------------------------------------------------//

我怎样才能达到我想要的结果?!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com