do until loop, and stop

I have a do until loop and I want stop then loop with a button. In VB I did use DoEvents, in Xojo what I have to use?

In my code:

self.myStop = false
Do Until self.myStop = true

In button Action:

self.myStop = true

Never use DoEvents in a desktop app. Ever. Under any circumstances. Ignore any advice to the contrary, always.

Instead, use the existing event loop to your advantage through a Timer or other mechanism, or use a Thread. Either can be dragged to a Window.

If you are not manipulating the UI, a Thread is the easiest. Put the looping code in the Thread and have it check of a flag, like a Window property, to stop it and exit.

If you are manipulating the UI, you can put the code in a Timer with a low Period. The code in the Timer should be able to exit after a certain about of time to keep the UI responsive (I find about 3 ms reasonable) and resume where it left off on its next cycle.

If I put my loop in a Timer and not put App.DoEvents my App remain blocked.

You replace the loop by the timer, you do not put the loop in the timer.

[quote=360347:@Kem Tekinay]Never use DoEvents in a desktop app. Ever. Under any circumstances. Ignore any advice to the contrary, always.
I know that, but I would like to know why. only because it’s deprecated ?
your sentence seems to tell that even if it’s wans’t deprecated you should not use it.

Why not check the docs: Application.DoEvents, section “In GUI applications”.

This is the code for a label counting upwards and a button which can be used to stop and restart the counting:

[code]Control Label1 As Label

Control CounterTimer As Timer
IDE-Property Mode = Multiple
IDE-Property Period = 10
Sub Action()
Label1.Text = Str(Label1.Text.Val + 1)
End Control

Control InterruptTimer As Timer
IDE-Property Mode = Multiple
IDE-Property Period = 50
Sub Action()
CounterTimer.Enabled = Not mInterrupt
End Control

Control Button As PushButton
Sub Action()
mInterrupt = Not mInterrupt
Me.Caption = If(mInterrupt, “Stop”, “Start”)
End Control

Property mInterrupt As Boolean = False[/code]
You can drag the window around, resize it, etc. while the CounterTimer is counting upwards.

@Eli Ott how I can replace a loop with a timer?

a) Have a look what a ThreadAccessingUIException is. You can’t manipulate the UI out of a thread.

b) You both a thread and a timer. Use the thread to start the timer.

Code in Timer.Action runs every Timer.Period at it’s first opportunity, meaning during otherwise idle time. It still runs in the main loop so your Timer code could look something like this:

dim startTicks as integer = Ticks

for index as integer = ResumeFromLast to LastIndex // Properties from the Window
  if ( Ticks - startTicks ) >= 3 then
    ResumeFromLast = index
  end if

  // Processing code

// Finished
me.Mode = Timer.ModeOff

The code that starts this Timer would also fill in ResumeFromLast and LastIndex.

I developp 11 apps with Xojo. All of them did use App.DoEvents without any problem. I create this thread You know what ? I’m happy (no DoEvents) because I removed them all replacing them by using Thread of Timer.

But I repeat, I never had issue before I removed them. Then it’s true it’s better to not use them, but for a simple program and just to learn, I think you can.

I would disagree. If you learn to program with bad habits, you will rely on bad habits in production environments.

Don’t do it. The results are unpredictable. It might work for simple apps, it might work for complex apps, it might work for a short while, it might work for a longer period. And then suddenly it does not work – maybe just once. The docs are clear: do not use DoEvents in a desktop app. Never.

No offense, but do not post a stupid sentence like … and just to learn, I think you can. Really? You say I think which means you do not actually know. Despite the docs being very clear about it. So you actually recommend to use it. And you recommend it especially to newbies for learning Xojo. Come on.

[quote=360365:@Jean-Yves Pochez]I know that, but I would like to know why. only because it’s deprecated ?
your sentence seems to tell that even if it’s wans’t deprecated you should not use it.[/quote]

Do events can cause reentrancy issues since it can cause events to be raised while events are already being handled.
There ARE some limited use cases where it is safe to use in Desktop applications - but they are very few & far between.
It really serves the most useful role in Console apps where there is no event loop and you have to write your own to make event driven controls, like sockets, function properly

I think Gabriele understood he shouldn’t use DoEvents. I’m stupid myself and I understood it as I spent many hours to remove them all from applications develop by a friend of me more than 10 years ago. But I said he could do it to finish what he was learning and came back on the DoEvents later to remove it. Because I know it’s difficult when you’re trying to understand something and you go on something else. I think we can program a bad thing temporally to continue what we were doing and coming back after.

Note that I never had issue with DoEvents maybe because I use a drap to disable all other events except a click on a stop button.

And yes, I took a little offense you say I say stupid thing. There are professional approach to Xojo, and there hobbyists like me. It’s not the first time someone speak (write) like that to me. But every time you help us and give us very good advice. Our programs (develop by hobbyist) won’t be used to pilot a navet to carry people to the mon or Mars. If our programs crash, the earth will continue to go round :slight_smile: .

Here you go Gabriele, a for loop that you can pause / resume / start / stop

If you use a thread, don’t reach from the thread into your window to change things, do it the other way around, reach from your window (UI) into your thread to check things and then make the change in your UI, don’t make any UI changes from the thread.