简体   繁体   English

C#如何将休息发送到另一个子例程?

[英]C# How can I send a break to another sub routine?

I have a loop running that will process 1000's of records, currently once the loop is running it can't be stopped and the user must wait until it is finished. 我有一个循环运行,将处理1000的记录,目前一旦循环运行它无法停止,用户必须等到它完成。 How can I stop the loop when someone clicks a 'Cancel' button? 当有人点击“取消”按钮时,如何停止循环? How do I break into that other routine? 我如何打破其他常规?

Thanks 谢谢

You can run it in its own thread and abort the thread. 您可以在自己的线程中运行它并中止线程。 Just beware that that might leave the operation in a bad state. 请注意,这可能会使操作处于不良状态。

Instead, you should create an exit flag that the thread checks at safepoints. 相反,您应该创建一个线程在安全点检查的退出标志。 If it's marked for exiting, the thread will stop as soon as it's safe for it to do so. 如果它被标记为退出,则线程将在它安全的情况下立即停止。

You can just use the BackgroundWorker component. 您只需使用BackgroundWorker组件即可。

It's event-based and is very easy to utilize. 它是基于事件的,非常容易使用。 Looks very appropriate for what you are describing. 看起来非常适合您所描述的内容。

It has nice support for cancellation signaling as well as progress reporting too. 它对取消信令以及进度报告也提供了很好的支持。

And a lot of code examples you can lookup on google. 你可以在谷歌上查找很多代码示例。


Set the WorkerSupportsCancellation property so it is true. 设置WorkerSupportsCancellation属性,使其成立。

backgroundworker1.WorkerSupportsCancellation = true;

Do it before you start the worker. 在你开始工作之前做它。

Then, in the loop, you can poll the CancellationPending property: 然后,在循环中,您可以轮询CancellationPending属性:

if (backgroundWorker1.CancellationPending) return;

Just an example, but you should get the idea. 举个例子,你应该明白这个想法。

thanks for the replies. 谢谢你的回复。 It is a backgroundworker but when I use backgroundworker1.cancelAsync(); 它是一个backgroundworker但是当我使用backgroundworker1.cancelAsync(); I get the exception: 我得到了例外:

"This BackgroundWorker states that it doesn't support cancellation. Modify WorkerSupportsCancellation to state that it does support cancellation" “此BackgroundWorker声明它不支持取消。修改WorkerSupportsCancellation以声明它确实支持取消”

How do I go about altering this? 我该如何改变这个? This is inherited code, sorry! 这是继承的代码,对不起!

As Mark said you need to implement some kind of co-operative synchronization. 正如马克所说,你需要实施某种合作同步。 People often gravitate to wanting to use Thread.Abort here ... but that is a bad idea. 人们经常倾向于想在这里使用Thread.Abort ......但这是一个坏主意。

There are a number of questions that address this, for example see: " Is there a good method in C# for throwing an exception on a given thread. " 有很多问题可以解决这个问题,例如,参见:“ C#中是否有一个很好的方法可以在给定的线程上抛出异常。

You can add a cancel button which sets a boolean flag 您可以添加一个设置布尔标志的取消按钮

Inside the loop add "Application.DoEvents" at key points and then check the boolean flag. 在循环内部在关键点添加“Application.DoEvents”,然后检查布尔标志。 If the flag has been tripped, preform your cleanup and exit the loop. 如果标志已被触发,则执行清理并退出循环。

Set the WorkerSupportsCancellation property on your BackgroundWorker to true :-) 设置WorkerSupportsCancellation你的财产BackgroundWorker真:-)

You've also got to check the CancellationPending property inside your loop to see if you should be aborting processing. 您还必须检查循环中的CancellationPending属性,看看是否应该中止处理。 The MSDN article that chakrit linked has a nice block of sample code chakrit链接的MSDN文章有一个很好的示例代码块

Edit: D'oh, you got me by a few seconds there chakrit :) 编辑:D'哦,你让我在几秒钟内有chakrit :)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM