[英]how do I implement something like JavaScript's setTimeout in C#?
I have a C# app that receives updates from a server, processes it, and updates the GUI. 我有一个C#应用程序,它从服务器接收更新,处理它并更新GUI。 the updates come in constantly, say several times a second. 不断更新,每秒说几次。 I want the app to update the GUI at most once every 2 seconds. 我希望应用最多每2秒更新一次GUI。 so if an update comes in at time T, I want all updates that come in from T through T+2sec to stay in a buffer, and at T+2sec do the processing and GUI update. 因此,如果更新在时间T出现,我希望从T到T + 2sec的所有更新都保留在缓冲区中,并在T + 2sec进行处理和GUI更新。 I know in JS you can use setTimeout() to execute some code at some time in the future, so I want something like that. 我知道在JS中,您可以在将来的某个时候使用setTimeout()执行一些代码,因此我需要类似的东西。
what's an appropriate way to do this? 有什么合适的方法做到这一点? I've heard that using threads to "schedule" a function call isn't a great idea, but I'm not sure of a better way to do this. 我听说使用线程“调度”函数调用不是一个好主意,但是我不确定这样做的更好方法。 would it be so bad to use a Timer with a two second interval, synchronized to the GUI thread, that does the processing/updating? 使用间隔为两秒的计时器(与GUI线程同步)会很糟糕吗,该计时器会进行处理/更新吗?
You can use a System.Windows.Forms.Timer
. 您可以使用System.Windows.Forms.Timer
。 It doesn't run events in a separate thread, it's the GUI thread that runs them. 它不会在单独的线程中运行事件,而是由GUI线程运行事件。 That way you can update the GUI directly without having to use Invoke. 这样,您可以直接更新GUI,而不必使用Invoke。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.