While...wend and condition

I think that’s a subjective personal preference.

I almost… kind of… “hate” reading things like:

If x.IsEmpty() = True Then ...

But some people write this kind of stuff.

I think even more readable would be simply:
While (data1<>127 or data2<>127)

Yep. That’s the equivalent using OR

Pure De Morgan.

image

By step count operation per input.

That’s better! :thinking:

I still prefer :point_up:t2:

And I guess I have an aversion to the keyword Wend :rofl: :rofl: :rofl:

Yes, Wend is a bit soppy, isn’t it?

Depends really whether you want to imply that the loopwill be done done at least once or that it may not be done at all.

I can’t imagine a thing that I can only do with While/Wend because I have not a Do/Loop equivalent.

  • I like @TimStreater 's format, because it’s readable and clear what is intended.
  • Also, you should think about the error condition / timeout.
  • and sleeping in the loop so you don’t burn 100% CPU

Here’s what I would do:

dim startTime as double = microseconds / 1000000.0  // start time in seconds
dimt timeLimit as double =  10.0  // limit 10 seconds
while true
  // get data1 and data2
  data1 = GetData1()
  data2 = GetData2()
  if data1 = 127 and data2 = 127 then
    // Time to leave!
    exit while
  end if

  // if too much time has passed, we need to exit with an error
  dim elaspsedTime as double = microseconds()/1000000.0 - startTime
  if elapsedTime > timeLimit then
     DoSomethingAboutTheError()
     exit while
  end if


  // to avoid overloading CPU, you don't want to do a hard loop. 
  // sleep here a little bit
  App.SleepCurrentThread(8) // adjust the milliseconds time as needed
  // macOS for example will complain if your app "wakes up" more than 150 times per second
wend
2 Likes

It’s not a question of liking when you have a need. In your example, differently of the OP’s, data1 and data2 does not come preset with whatever he did in the past.

If the need was something as you designed, your example would be perfect, the only change I would do would be a bit modernization and killing my While/Wend Pet Peeve. :laughing: as:

Var startTime as double = microseconds / 1000000.0  // start time in seconds
Var timeLimit as double =  10.0  // limit 10 seconds

Do
  
  // get data1 and data2
  data1 = GetData1()
  data2 = GetData2()
  
  // Tasks done?
  If data1 = 127 and data2 = 127 then Exit Do
  
  // if too much time has passed, we need to exit with an error
  Var elapsedTime as double = microseconds()/1000000.0 - startTime
  
  If elapsedTime > timeLimit then
    DoSomethingAboutTheError()
    Exit Do
  End
  
  // to avoid overloading CPU, you don't want to do a hard loop. 
  // sleep here a little bit
  
  Thread.SleepCurrent(8)
  // adjust the milliseconds time as needed
  // macOS for example will complain if your app "wakes up" more than 150 times per second
  
Loop
1 Like

Slightly off topic, but a means to prevent Windows from complaining ‘not responding’ during a long process has led me to all kinds of horrible workarounds over the years.
The hoops I have had to jump through to try and get things into a thread, and use timers… I hate them.
Too tight and the CPU usage goes to 100% on a single core and looks awful.

So does Thread.SleepCurrent(8) avoid that kind of issue?
Does it allow some app events to occur and prevent ‘not responding’?

This was Mike’s design maybe specifically tailored for macOS to yield some CPU time to other processes probably to avoid “beach-balling”. 8ms may be a bit short time for Windows.The idea yes, is targeted to avoid OS locks, but not app freezing if such loop is in a main thread, app freezing is avoided keeping the main thread not busy. For such case something like this must be moved to an spawned thread in background.

Maybe I would try to yield like 35ms after wasting at least 35ms in my background thread.

I need to write a forum post about how to do threading right. Stay tuned!

1 Like

This seemed to get lost in the conversation, so I wanted to reinforce that the simple answer to the OP’s question was that he should have been using OR instead of AND. It’s certainly interesting to see all the other approaches that could be used, though. This forum is full of knowledgeable folk.

3 Likes

Maybe do it as a gust post on the Xojo blog?
@Dana_Brown @Alyssa_Foley

1 Like

Yes I know. And I have to admit that I never write, as Jeff T wrote just below your post:

if thefile <> nil and thefile.exists then

but

if not(thefile = nil) then
  if thefile.exists then

I say to myself that if one day the compiler change, if the logic is not the same, or if I copy paste my code in other language, I may have less trouble writing like that.
In fact, I wrote a small Method which test if the file is not Nil, then if it exists, and then (eventually) if it is an alias then if the target of this alias exists.

Maybe some blog posts could start as a simple draft discussion post, providing some code, receiving feedback (if people wanted to share some personal experiences, or question things like “why you chose x instead of y?”), and then, based on all that content and feedback (if some rises instead of a dozen “love clicks”), write the final content and post a more elaborate blog entry that at the end can be kind of the same basic idea, but sometimes, evolve a bit?

1 Like

I like that idea, and in fact would be happy if the author was “the xojo community”. I’ll start a new thread with a draft

The effort is yours, you are the author. Just in case of rising contributions, ignore the noise, pick contributions (if any), do your writing, and thank contributors (if any) somewhere in the final article.

2 Likes