English 中文(简体)
我如何在vb.net中建立二级信息循环?
原标题:How do I create a secondary message loop in vb.net?

我在“全球倡议”的深处看到一只半干.。 另一条read子赢得了笔工作,一名时手赢得了笔工作。 但是,在我从事背景工作时,我希望大家能够作出回应。

我共同使用。 偶像——但给我3个坏选择:

  • loop with no sleeps - end peg an entire CPU (my task waits on stuff besides calcs & I/O so this matters)
  • loop with a sleep - then the app slows down
  • Try to make a smart sleep loop that speeds up when lots of messages happen (check for messages in messagefilter function)

在白天,我可以这样做:

while(PeekMessage(&msg,0,0,0,PM_REMOVE) && still_working)  
{
       TranslateMessage( &msg );
       DispatchMessage(&msg);
      // do a bit of my background work with each message here, quit the loop when done.
}

排他性比静电灯要好一点,因为它将连续处理多条信息,但我仍然必须使用智能睡觉来挽救pu——我想避免这一点。

还有一种方式是做主要的讯息吗?

    MSG msg; 
    while( GetMessage( &msg, NULL, 0, 0 ) )
    {
        TranslateMessage( &msg );
        DispatchMessage( &msg );

    }

Is there a proper .NET equivalent, or do I just have to p/invoke the sdk stuff? Thanks!

http://www.ohchr.org。 Winforms。 你们所有回答者都想知道,我为什么可以使用申请。 还是听起来的,我想要做的是处理“无人听闻”事件,然后让我听起来,而有些胜利者却 close倒了,并告诉我的看望方案,以便重新开始评估。 是的,我知道有人说,一个“无所事事”的节目应该死去(显然,管理),会发生危险的事情;但其中没有一个像我的使用者那样危险。

Edit #2 - Looking for vb.net sourcecode exact translations of the above two functions.

也许有些人善于p/invoke,并宣称可以 quick得core;-

最佳回答

如果你决心书写你自己的信息。

Application. 页: 1 由于DoEvents,只有等待电文和 does等处理的程序,你可以打电话<代码>WaitMessage,以便在有新电文处理之前,制作这份清单。

Declare Function WaitMessage Lib "user32.dll" () As Boolean

Shared Sub Main()
    Dim f As New Form1
    f.Show()
    Do While f.Visible
        Try
            Application.DoEvents()
        Catch e As Exception
            MsgBox(e.Message)
        End Try
        WaitMessage()
    Loop
End Sub

这等于是万国邮联,但是它没有做任何背景工作。

阁下说,您的背景工作是正确的,但是如果其 st子完全由你来取代<条码>,WaitMessage ,改为: 编码> - 虽然你需要处理你等待的物体的Win32。

问题回答

你似乎有两个理由想写自己的信息 lo:

1. To avoid "end pegging an entire CPU".

这通常是一个值得称道的目标,但如果你进行某种计算,则使用通用报告格式是完全适当的。 假定正常程序优先事项,它不会使万国邮联的其他进程陷入瘫痪。 如果你故意贬低你的计算,其速度将比必要的要慢,使用户无法使用。

你的“黄热日”版本很可能在互联网上重复,但我看不出它会如何避免在邮联的头上结束。 你们是如何想象的,这不同于“<代码>申请”。 在从事“回场”工作时,是否在休息室使用?

2. To allow handling "unhandled" exceptions.

看来,你可能希望把<代码>DispadMessage((或全程)填入。 Try ... Catch/2005/4。 没有必要。 这也是:

  // file ApplicationEvents.vb
  // <- the stackoverflow syntax highlighter doesn t know about VB comment syntax
Namespace My
    Partial Friend Class MyApplication
        Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
            e.ExitApplication = False
              // notify the user or whatever...
        End Sub
    End Class
End Namespace

See the docs:

查看BackgroundWorker,过去对我的工作良好。

另一条read子赢得了打工

为什么? 这名或背景工作者(ThreadPool Use)是你所需要的。





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签