locked
C# Equivalency of VB function "DoEvents" RRS feed

  • Question

  • Basically the DoEvents function does something mainly used for waiting.
    Here's an example:
    Do Until Winsock1.State = 7
        DoEvents

    Loop
    That example waits for the winsock control to successfully connect. So how would I perform the same task in C#?
    Thanks in advance
    Thursday, March 16, 2006 7:54 PM

Answers

  • Edit: Nevermind figured it out. It's Application.DoEvents();
    Thanks for the help =]
    Thursday, March 16, 2006 9:58 PM
  • do
    {
        DoEvents();
    }
    while(WinSock.State==7);
    Thursday, March 16, 2006 9:09 PM
  • And for the record, we C Sharp guys do not use "On Error Goto", or any goto for that matter.

     

    Wednesday, August 13, 2008 8:34 PM

All replies

  • do
    {
        DoEvents();
    }
    while(WinSock.State==7);
    Thursday, March 16, 2006 9:09 PM
  • Edit: Nevermind figured it out. It's Application.DoEvents();
    Thanks for the help =]
    Thursday, March 16, 2006 9:58 PM
  • Does C# has On error resume next?
    Sunday, February 18, 2007 7:34 AM
  • Something like this should do the trick.

    for (int i=0; i<1000; i++) {
        try {
            // Do something here.
        } catch {
        }
    }
    Tuesday, August 12, 2008 3:49 PM
  •  

    Hey Tim J. Adams!

    I really hope you don't find this offensive (if you do I'm terribly sorry, I know you ment well by answering the question that was posed), but check the dates before you answer a post

    Wednesday, August 13, 2008 3:17 PM
  • And for the record, we C Sharp guys do not use "On Error Goto", or any goto for that matter.

     

    Wednesday, August 13, 2008 8:34 PM
  • If you call DoEvents in your code, your application can handle the other events. ... Unlike Visual Basic 6.0,
    application.DoEvents(); in c#

     Please chck : For  C#
                          For vb6.0

    So i think it is not waiting for anything, It helps other control in a vb or c# forms to run. Otherwise if a time consuming job called ,the forms looks like not responding. to avoid this you can use DoEvents() method.

    I will recommend

    The BackgroundWorker class allows you to run an operation on a separate, dedicated thread. Time-consuming operations like downloads and database transactions can cause your user interface (UI) to seem as though it has stopped responding while they are running. When you want a responsive UI and you are faced with long delays associated with such operations, the BackgroundWorker class provides a convenient solution.

    For more information
       http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
    Wednesday, November 19, 2008 5:30 AM
  • jannemanrobinson,

    Remember that people come by these posts many months (& perhaps years) later to read these posts.  Nothing frustrates me more than to be googling for an answer, and find that someone had asked the same question & it was left hanging..

    These forums will likely serve as a good resource for as long as C# remains an actively used language.  I have a feeling that these posts will be preserved somewhere, long after we are gone. (along with cigarrette TV commercials from the 50's!).
    Wednesday, December 10, 2008 8:01 PM
  • That's it. Ive read that post and solved a problem mine today, 2 years late. : )

    Also is usefull to say that, sometimes, the Application class should get ambiguous with some

    other libraries, like Microsoft.Office.Interop.Excel and etc... in these cases, we can specify the full path of

    Windows Forms Application class, by typing System.Windows.Forms.Application.DoEvents();

    Kisses,

    Sinistrão de São Gonçalo

    Monday, September 27, 2010 9:59 PM
  • I will recommend

    The BackgroundWorker class

    For the benefit of future seakers of solutions, this is the answer. The DoEvents method is not the best solution for C#. DoEvents is something that unmanaged VB programmers needed because it does not support multiple threads. A developer using managed code (and/or a language such as C++ that supports threads) should know how to use threads instead of DoEvents. DoEvents is convenient and can be used if a developer knows how to use threads such as what the BackgroundWorker class provides and understands the consequences.



    Sam Hobbs
    SimpleSamples.Info
    • Proposed as answer by Rudedog2 Saturday, February 4, 2012 4:16 PM
    Wednesday, February 1, 2012 3:26 AM
  • Simple Samples,

    The purpose of DoEvents is not really related to threads.

    Its purpose is to allow Windows to interact with the form and its controls for things like Refresh() and Event-processing.  It also allows the screen to update and for the user to interact with parts of the form (such as buttons.)  One side-effect is that it can allow the user to click the exit button while a loop is processing and after exiting, the loop resumes processing, which may in turn re-load the form.

    --

    Rob

    Monday, July 16, 2012 10:02 PM