什么的DoEvents()的在C#中实际上做? [英] What do DoEvents()'s in C# actually do?

查看:1562
本文介绍了什么的DoEvents()的在C#中实际上做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们雇了一个公司来转换控制部分工业机械到C#一个老VB6 DLL。旧VB6代码组成了以的DoEvents ,这样睡觉时,在DLL定时器和套接字事件仍然会得到处理洒睡眠电话暂停程序。在调用DoEvents转换为

We hired a company to convert an old VB6 DLL which controls some industrial machinery to C#. The old VB6 code had "pause" routines consisting of sleep calls sprinkled with DoEvents so that while sleeping, timer and socket events in the DLL would still get processed. The DoEvents were converted to

System.Windows.Forms.Application.DoEvents();



我不是一个VB6程序员,但我的理解是,VB6实际上是单线程等等长时间的睡眠将关闭一切,包括定时器和插座事件处理。

I'm not a VB6 programmer but my understanding is that VB6 is actually single-threaded and so a long sleep would shut down everything including timer and socket event handling.

在代码转化成C#暂停程序这个样子。 。 。

When the code was converted to C# the pause routines looked like this . . .

public static void pauseit_ms(ref int milliseconds)
{
    try
    {
        Sleep(milliseconds / 2);
        System.Windows.Forms.Application.DoEvents();
        Sleep(milliseconds / 2);
    }
    catch (Exception exc)
    {
        LogException("pauseit_ms", exc);
    }
} 



在.NET中,定时器和套接字事件中运行他们的自己的线程(我的大部分在此工作的转换的代码一直尽量做到线程安全的!),所以它并不清楚在的DoEvents()收购我们。但是,MSDN说:

In .Net, timer and socket events run in their own threads (most of my work in this converted code has been to try to make it thread-safe!) so it's not obvious what the DoEvents() buys us. But the MSDN says

调用此方法导致当前线程,而
所有的等待窗口消息处理暂停。

Calling this method causes the current thread to be suspended while all waiting window messages are processed.

所以,我们应该让这些的DoEvents()在那么其他种类的事件(而不是定时器或插座回调)AREN T受阻?或者他们在.NET / C#背景下多余的?

So should we leave these DoEvents() in so other kinds of events (not timer or socket callbacks) aren't blocked? Or are they superfluous in a .Net/C# context?

推荐答案

的DoEvents 创建一个额外的消息循环。这只是一个循环,读取邮件,处理它,然后读取下一消息,直到它得到一个消息,它应该停止,或者有没有消息进行处理。调用 Application.Run 为应用程序创建的最初消息循环。创建其他嵌套的消息从处理这些消息之一内循环可引起种种问题的,因此应该避免除非你非常熟悉它做什么,什么情况下可以正确地使用它。

DoEvents creates an additional message loop. It's just a loop that reads in a message, processes it, and then reads in the next message, until it gets a message that it should stop, or has no messages to process. Calling Application.Run creates the initial message loop for your application. Creating additional nested messages loops from within one of the handlers for these messages can cause all sorts of problems and so it should be avoided unless you're intimately familiar with what it does and under what circumstances you can use it correctly.

在大多数情况下,而不是创建一个额外的消息循环,你的程序应该简单地asyncrhonous。而不是阻塞一段时间当前线程,你应该使用像一个定时一段时间的后执行的方法不会阻塞当前线程

For the most part, rather than creating an additional message loop, your program should simply be asyncrhonous. Rather than blocking the current thread for a period of time, you should be using something like a Timer to execute a method after a period of time without blocking the current thread.

在.NET中,定时器和套接字事件在自己的线程中运行。

In .Net, timer and socket events run in their own threads

实际等待计时器的时间结束后,或者套接字得到回应,做的无需使用任何线程在所有的。没有线程坐在那里睡觉,而操作本质上是异步的。作为该用于执行的事件处理程序的线程,这将有所不同。一些定时器将使用一个线程池,一些UI线程,有些是可配置的,并可以执行。套接字可能将只使用一个线程池线程。

The actual waiting for the Timer's time to elapse, or for the socket to get a response, is done without the use of any thread at all. There is no thread sitting there sleeping, rather the operations are inherently asynchronous. As for the thread that is used to execute the event handlers, that will vary. Some Timers will use a thread pool, some the UI thread, and some are configurable and can do either. The socket is likely going to just be using a thread pool thread.

这篇关于什么的DoEvents()的在C#中实际上做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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