ConsoleApplication.DoEvents()

I’m fairly new to Xojo and have been working with it for only a few months now, and I’ve been trying to write a console application that needs a main loop to execute some reading and writing to and from a serial port. I read through the ConsoleApplication.DoEvents() documentation to get an idea of how I might do that and I found it a little confusing. My understanding of it is that once you hit the Do loop that contains DoEvents, it is simply running any event handlers you might have in the application, such as timers, in a loop. I was hoping that someone could shed some more light on what exactly DoEvents is doing and tell me if I’m on the right track in my assumption. Thanks

Hi Daniel,

The App.Run event is where you can put your Do Loop. The Do Loop does not automatically add doevents and you certainly can add it. Since the console app doesn’t have a main loop DoEvents can yield/give back time to the app when the loop is hit. For example if you have some asynchronous things going on and/or timers if you don’t give back time at the loop boundary with DoEvents then those items will not work properly. I wasn’t giving enough time back in one app properly and I was missing my SMTP events.

Xojo Docs:
http://documentation.xojo.com/index.php/Application.DoEvents

If you don’t use a loop to keep the app alive, the Run event terminates and your app is gone.

The Do Loop is here to keep it alive, and Do Event is here to allow other processes to take place while the loop goes merry go round.

Until you are satisfied the app did all it needs to do, and exit the loop to quit the app.

In Desktop and Web the loop is created for you.

Really interesting Mike. I wonder if the default 10ms can be reduced to 1ms w/o having any issues. I need every tick in a server console app that needs to handle as much users as possible. Would be great ( as a console master I know you are ) if you can share your doEvents timing thoughts.

Amando

[quote=201955:@Amando Blasco]Really interesting Mike. I wonder if the default 10ms can be reduced to 1ms w/o having any issues. I need every tick in a server console app that needs to handle as much users as possible. Would be great ( as a console master I know you are ) if you can share your doEvents timing thoughts.

Amando[/quote]

I think in this scenario, you want to increase the doEvents interval (not lower it to 1ms), which will yield more time to your other processes and not the loop which is keeping it alive.

EDIT: I may be wrong in this. I just tested with:

[code] myTimer = new timer

myTimer.period = 1000
myTimer.Mode = timer.ModeMultiple

addHandler myTimer.action, addressOf handleTimer

myTimer.enabled = true

do
app.doEvents(2000)
loop[/code]

The timer fired every 2000ms (the time in the doEvents) and not the time set for the period. This is confusing to me, because when you set a low value in doEvent within a loop… that loop is faster, which means it’s dedicating more CPU cycles to THAT loop/thread and less to everything else. So, I guess a console uses this differently when it comes to its main thread? Trying to understand the logic behind this…

It’s the call to doevents that runs the timer’s action event. It doesn’t run on its own. You’re not sharing time with the events, you’re executing them.

app.doEvents(2000) = Run the event loop then sleep for 2000ms.

The maximum I’ve used is 3600000, but that was in service apps that only need to run their service infrequently.

Alright, thanks guys…

[quote=201955:@Amando Blasco]Really interesting Mike. I wonder if the default 10ms can be reduced to 1ms w/o having any issues. I need every tick in a server console app that needs to handle as much users as possible. Would be great ( as a console master I know you are ) if you can share your doEvents timing thoughts.

Amando[/quote]
I don’t know about “Console Master” :slight_smile: but I can tell you I write a lot of console apps for the networking industry. My issue with my SMTP not getting enough “attention” was due to me having DoEvents with the incorrect amount of “sleepy” time. Through a bunch of Trial and error using the debugger I have deemed for this specific app that using the default works best for me.

  do
    iperfOps.Process()
    DoEvents()
  loop

This maximized by ability to give enough time back fast enough for my spawning classes.