开发者

VB.NET DOEVENTS

开发者 https://www.devze.com 2023-04-09 17:11 出处:网络
I have a VB6 app, which does a lot of processing in the form_load.A call t开发者_运维知识库o DoEvents ensures that the form loads before the processing is complete.However,this does not appear to work

I have a VB6 app, which does a lot of processing in the form_load. A call t开发者_运维知识库o DoEvents ensures that the form loads before the processing is complete. However,this does not appear to work in VB.NET i.e. in the following code, the loop finishes befoRe the form is loaded, even though I have called DOEVENTS:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    For i = 0 To 100000
        Application.DoEvents()
        Dim Test As String = "Test"
    Next
End Sub

What have I missed?


Your statement:

A call to DoEvents ensures that the form loads before the processing is complete

To the best of my knowledge, this is NOT true. The form "paints" before processing your load event - the processing (that is loading) is still not complete. All that is ensured by a call to DoEvents is OTHER messages get a chance to be handled when you are in the middle of a long processing. MSDN's help of DoEvent() describes it as:

Processes all Windows messages currently in the message queue.

Also, it specifically states:

Unlike Visual Basic 6.0, the DoEvents method does not call the Thread.Sleep method.

I believe it might be risky for you to handle your requirement in the Load event. Just a search for "Application.DoEvents in load" in google talks about bad experiences for many. I suggest you can explore handling your requirement in Shown event.


DoEvents in VB.NET should be actively avoided.

Have a look at DoEvents in .NET and Stop DoEvents and DoEvents is Evil to see why.

It is a common misconception that this does the same as the VB6 DoEvents. In almost all cases what you want to achieve can be done in some other way in .NET without the need to call DoEvents.

Your best bet is probably a BackgroundWorker object have a look at this example to get you started.

A point to note though is that you can't update controls on your form from a background worker without using a delegate - but that is another question...


You should use a BackgroundWorker component in order to do lots of background work without freezing up the user interface in VB.Net. Look at this MSDN tutorial How to run an operation in the background

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号