Wait for a certain Time

How can i wait for a certain time (3000 to 5000ms) before proceeding with the code? A delay (ms) routine would be perfect. I have to wait until my device answers. No chance fiddling around.
Thanks

Put your code in a Thread and then use Thread.Sleep.

App.SleepCurrentThread(milliseconds As Integer)

Another way is to create a class for your device and add an Event that represents the answer. Your main code can react to the Event instead of waiting around. This is a reusable way to access your device that you can benefit from in other parts of your code, or even in other projects.

Eli’s tip works but Kem’s idea is also not bad. But too hard for a XoJo newbie.
Kem do have any sample source for a newbie ?

I’d need to know how you access your device. The Shell?

Via Serial Port.
I send a ASCII command to the device. The device parses the command, does some calculations and then answers in binary on the same port.
As the answer comes slowly and bytewise i have to wait until the device sends the end of answer sign (0x00).

This is what i tried with Eli’s App.SleepCurrentThread(milliseconds As Integer)

In Button Action
DSCMod_Serial.Write ChrB(&H78) + ChrB(&HA0) + ChrB(&H54) + ChrB(&H61) // Cmd is asking DSC Signal levels
// The data sheet of the DSCModul suggests to wait for about 2 seconds before all gathered data is sent.
App.SleepCurrentThread(2000)
Test_Lbl.Text = “got the results”

What happens pressing the Button? The Button remains pressed. After 2 Seconds the Button is released and the data is sent to the DSCModul. So that’s not what i want. I want to send the command, wait and read back the result.

So you’re using the Serial class? In that case, subclass it for you device and define a new Event, AnswerReceived (or something like that). The code in DataAvailable will collect the data as it comes in, then raise the AnswerReceived event when it receives the null, so the DataAvailable Event might look something like this:

dim thisData as string = me.ReadAll
zData = zData + thisData // zData is a private property
if zData.InStrB( Chr( 0 ) ) <> 0 then
  Result = zData // Result is a property
  RaiseEvent AnswerReceived()
end if

It would be better for Result to be a calculated property, or better yet, to create a method that reads the result and clears it at the same time so you don’t keep adding to the same data, but I’ll leave those details to you.

Thanks for help but i still have some problems.
I use New Subclass with Serial1 and get CustomSerial1. Then i “Add to CustomSerial1” -> Event Definition -> i get Event Definitions / untitled. I rename it “AnswerReceived”. But i cannot add any code to this event. Is this correct?

Right, that event will be implemented by its own subclass.

Think of it this way. You want a class that represents your device and communicates with it. Maybe the communication is standard or not, I don’t know, but let’s say that various commands can be sent to it and various answers received. You want to make your class specific enough to send those commands and receive those answers, but general enough so it can be used in a variety of situations. Therefore, your class will only know how to send commands and receive answers, but will leave it up to whatever is using that class do deal with those answers.

Then you can put your class into a window and put the specific code into the AnswerReceived event that gets raised there so it can do whatever is appropriate at the moment.

Again, this is all very general because I don’t know what your device is, what commands might be sent, or what answers might be received. It’s up to you to fine-tune it into something useful.

Okay understood. I’ll give it a try.
Thanks

Hi Kem,
works perfect. I’ll have to polish my code to get everything running as it should. Thanks again.
BTW how is the weather in NY. In Germany it is getting frosty and cold now.

Glad to hear it.

Moderate today in the 40s, but we are expecting unusually low temperatures and snow starting tomorrow.