简体   繁体   English

如何在C#中暂停循环(同时,foreach,for或其他方式)?

[英]How can I pause a loop (while, foreach, for, or otherwise) in C#?

Is there a way to pause a loop for a specified time without blocking? 有没有一种方法可以将循环暂停指定的时间而不会阻塞? I don't want to use Thread.Sleep(xx). 我不想使用Thread.Sleep(xx)。 Is there a way to use a manual event, or dispatcher timer to pause the loop? 有没有办法使用手动事件或调度程序计时器来暂停循环? I don't want to use thread.sleep because it blocks the thread and I don't want that. 我不想使用thread.sleep,因为它阻塞了线程,我也不想这样。

EDIT: 编辑:

what i want to do is similar to post here: 我想做的类似于在这里发布:

serialport responding to EventHandler, but not ReadExisting or ReadLine? 串行端口响应EventHandler,但不响应ReadExisting或ReadLine?

it's not answered there, so i've explained what i need to do here. 那里没有答案,所以我在这里解释了我需要做的事情。

i cannot open the serial port and keep it open, i have to close it every time. 我无法打开串口并保持打开状态,每次都必须关闭它。 so, what i want to do is hit first item in loop, open the comport, write to it, read from it, then close it. 因此,我要执行的操作是单击循环中的第一项,打开该comport,对其进行写入,然后从中读取,然后将其关闭。 if you've ever used a mouse or bluetooth scanner with windows, you know that not every comport sends back a message all the time. 如果您曾经在Windows上使用过鼠标或蓝牙扫描仪,那么您就会知道并非每个命令都会一直发送信息。 so if a port doesn't send a message back, it has to time out. 因此,如果端口不回传消息,则必须超时。 if a message isn't received in a short period of time, i need to move on. 如果短时间内没有收到消息,我需要继续。

i might hit the same port again with a different message if it didn't respond the first time around. 如果第一次没有响应,我可能会用不同的消息再次击中同一端口。 when i do this, i get an error that the port is already open; 当我这样做时,我得到一个错误,表明该端口已经打开; and it is, because the loop just blew through the code and didn't pause enough. 这是因为循环只是遍历代码而没有足够的停顿。

thread.sleep just blocks the thread, along with the message the comport sends back to it, i don't know why, it just does. thread.sleep只是阻止线程,以及该命令发送回该线程的消息,我不知道为什么,它只是这样做。 i'd rather not use thread.sleep. 我宁愿不使用thread.sleep。

when i loop through it, i want it to pause until i close the port, then start with next item in loop. 当我循环通过它时,我希望它暂停直到关闭端口,然后再从循环中的下一个项目开始。 that's the most important part. 那是最重要的部分。

is there a way to start the loop, spawn a method on a new thread, pause the loop that called it, wait for either a message back or a time limit, then have the loop continue when the other thread says so? 有没有办法开始循环,在新线程上产生方法,暂停调用它的循环,等待消息返回或时间限制,然后在另一个线程这样说时让循环继续? thanks 谢谢

EDIT 编辑

i used an AutoResetEvent. 我用了一个AutoResetEvent。 thanks for you help 谢谢你的帮助

If you pause something, that it is blocked by definition. 如果您暂停某些内容,则其将被定义阻止。 Pausing one thread will certainly give control to other threads. 暂停一个线程肯定会控制其他线程。

Unless you need something else INSTEAD of pausing? 除非您需要其他代替暂停的内容?

What is PAUSING to you - maybe if you update your question... 对您造成了什么困扰-也许如果您更新问题...

I just figured out - maybe you want to pause and have the UI running - then do something like this (in each step of your loop): 我刚刚弄清楚-也许您想暂停并让UI运行-然后执行以下操作(在循环的每个步骤中):

int times = 100;
while (times > 0) 
{
    Application.DoEvents();
    Thread.Sleep(10);
    times--;
}

This will both have GUI respond to you and will slow your loop. 这都会使GUI响应您,并会降低循环速度。

use a ManualResetEvent . 使用ManualResetEvent It allows you to pause a thread until another thread signals to continue the execution. 它允许您暂停一个线程,直到另一个线程发出信号以继续执行。

In the provided link there is an example at the bottom where it is shown how to use it 在提供的链接中,底部有一个示例,显示了如何使用它

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

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