dcsimg
Results 1 to 8 of 8

Thread: Application.DoEvents

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    285

    Application.DoEvents

    Hello. I remember DoEvents being considered bad practice back in my vb6 days, but coming on to .net, the information I have gathered so far from the web has been mixed.

    Basically, I run my game loop with "QueryPerformanceCounter" and "QueryPerformanceFrequency", for what I understand is about as accurate as timing gets.

    However, there is a DoEvents in there (between frames basically), and if it indeed does take a toll on cpu or doing something else that is evil, then I shall be attempt to re-write and look for alternatives.

    Thanks

  2. #2
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Application.DoEvents

    Not sure what the question is, but yes, there is no need for DoEvents. I'm not sure where you get the idea that usage in the .NET world is mixed - it's universally a 'do not use' situation. I can't see why you would have a DoEvents anywhere in a game loop.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,889

    Re: Application.DoEvents

    As you noted, that's a mixed bag. DoEvents itself appears to be fairly costly, even if nothing happens as a result of it. Considering some of your other posts, that may be sufficient reason to abandon it. However, it often does do other things, and those could be bad...but only in some situations. It causes a pause while all pending messages get pumped. Generally, this means that pending actions by the user (keypresses and mouse actions) get processed, but other things, such as painting the screen or timers ticking could also happen if they are pending. What this does to your game loop is up to you. In theory, you could be opening the door to a long running process, thereby hosing your game loop, but you'd likely know whether that even makes sense...and it sounds like it doesn't, as I would guess that your user interaction doesn't cause a calculation of the Nth prime number above infinity, or anything like that.

    Personally, I'm not as totally opposed to it as some people who will respond, but I'd say that you don't want to do it simply because of the cost incurred.

    Many game loops actually poll for user input, which doesn't work all that well in the typical Windows, event-driven, environment. Without that, the alternatives to DoEvents might be kind of grim. You might be able to move much of it to a BackGroundWorker object and raise events when it is time for the UI to do something, but since you are dealing with graphics, it might be the case that you can't move anything useful to a BGW.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    285

    Re: Application.DoEvents

    Thanks, I'll definately be looking for alternatives in the future then, I can't afford to have any extra costs, especially on the server.

    I use a method I downloaded ages ago:

    Code:
    Do
                QueryPerformanceCounter(CurrentTime)
                FPS = TicksPerSecond / (CurrentTime - LastTime)
            Loop While (FPS > TargetFPS)
    If I don't put a DoEvents in the gameloop my program becomes unresponsive. I'll look to change this though, but my search on proper gameloops for vb.net have come up shallow thus far.

    Not sure how much I can realistically do with background workers in a game like this. The only thing I don't do on the mainthread so far is processing connection requests and data sending back and forth.

  5. #5
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,191

    Re: Application.DoEvents

    Perhaps this might offer some insight?
    Managed-Game-Loop

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,889

    Re: Application.DoEvents

    That loop looks a whole lot like a spin wait. None of those seem to be blocking calls, so what you are doing is totally pegging the CPU for the duration of the loop as a means to wait a set amount of time (by the way, look at the Stopwatch object for something that isn't ages old. Great accuracy and precision down to a millisecond.). It's a waste of time and energy (a surprising amount of energy) as it stands, but less so if that loop is only running for a millisecond or two out of every iteration. That's something worth looking into. Spin waits will increase power consumption by the system, which means more heat being produced, which increases wear on components, and all for nothing because it's basically a way to waste time destructively.

    An alternative might be to calculate the number of milliseconds you will be waiting, then sleep the thread for that amount of time. Sleep costs nothing, but it will be just as unresponsive to input as the spin wait is.

    However, since this is a server, do you even HAVE a UI? If all it is doing is running a process to decide what goes where and so on, what input is it responding to? If it is just responding to input from the clients, then grab that input and stick it in a Queue (of T), or two (in a background thread). Then the game loop can see whether there is anything in the Queues and process that on the next iteration of the loop.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    285

    Re: Application.DoEvents

    Quote Originally Posted by Shaggy Hiker View Post
    That loop looks a whole lot like a spin wait. None of those seem to be blocking calls, so what you are doing is totally pegging the CPU for the duration of the loop as a means to wait a set amount of time (by the way, look at the Stopwatch object for something that isn't ages old. Great accuracy and precision down to a millisecond.). It's a waste of time and energy (a surprising amount of energy) as it stands, but less so if that loop is only running for a millisecond or two out of every iteration. That's something worth looking into. Spin waits will increase power consumption by the system, which means more heat being produced, which increases wear on components, and all for nothing because it's basically a way to waste time destructively.

    An alternative might be to calculate the number of milliseconds you will be waiting, then sleep the thread for that amount of time. Sleep costs nothing, but it will be just as unresponsive to input as the spin wait is.

    However, since this is a server, do you even HAVE a UI? If all it is doing is running a process to decide what goes where and so on, what input is it responding to? If it is just responding to input from the clients, then grab that input and stick it in a Queue (of T), or two (in a background thread). Then the game loop can see whether there is anything in the Queues and process that on the next iteration of the loop.
    I've looked at that game loop before, and it didn't seem attractive to me for various reasons. Tons of code, starting new threads to the left and right etc.

    And yes, my server does have a UI. I even have an option to draw graphics to help with bugtesting (I need to see what's happening on the server, only staring at code sucks ).

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,889

    Re: Application.DoEvents

    Quote Originally Posted by Nirwanda View Post
    And yes, my server does have a UI. I even have an option to draw graphics to help with bugtesting (I need to see what's happening on the server, only staring at code sucks ).
    True dat!
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width