Hello,
I’m using a shell on Windows as below. It can happend that there is no repsonse/result. Therefore I want to use a timeout. I have seen that I can set a timeout for the shell. But also if I set this nothing changes and I’m waiting and waiting… has anybody an idea or example how to implement this?
“sh(i).Execute(CMD(i))
Do
sh(i).Poll
App.DoEvents
Loop Until Not sh(i).IsRunning”
BR Sebastian
Hi @Sebastian_Fischer1 ,
welcome to the Xojo community.
Are you running your app for Windows ? If this is the case, on Windows timeout is not available.
Yes It’s running on windows. So timout for Shell is not working? This means that I have to program it by my own.
I’m afraid you have that backwards. Timeout
is only available on Windows. See:
https://documentation.xojo.com/api/os/shell.html#shell-timeout
Welcome @Sebastian_Fischer1 . A few notes…
When posting code, please use the code tags. You will find them in the toolbar as the “</>” icon, or just type three backticks before and after your code block. This will make it easier for us to help you.
Unless you are writing a console app, do not use App.DoEvents. Ever. If someone tells you that you can or should, please don’t.
In your code block, it’s unclear what mode your Shell is in. If it’s Synchronous (the default mode), all of that additional code is unnecessary. The Shell.Execute
method will effectively “freeze” execution of your code until it returns a result or, in Windows, times out. This should be all you need:
sh(i).Execute(CMD(i))
if sh(i).ExitCode = -2 then
// timed out
else
// check sh(i).Result
end if
1 Like
Thanks.
What should or can I use instead of App.DoEvents?
It’s asnchronous 
The events of the Shell class.
Completed will tell you when a task has ended. You may not even need to set a timeout if these commands are exiting when they are done.
The reason to use Asynchronous is to issue the command and deal with the result later, allowing other code to execute while waiting for completion. This is useful when you know execution could take a while.
If you are going to wait for the result anyway, use Synchronous mode.
Otherwise, create a subclass of the Shell that implements the Completed
event.
In either case, there is no reason to use anything like DoEvents
or Poll
.
1 Like
It can happen that it is not completed therefore I need something like a timeout…if there is no response.
It’s worth testing (because I don’t recall off the top of my head and it’s not explicitly documented), but it may be that when the Timeout is hit the Completed event is raised. The documentation doesn’t indicate any other way of knowing the timeout has been hit.
But if I use below code instead it will run into timeout which is not correct, because I set timeout to 100 and mode to sync.
sh(i).Execute(CMD(i))
Select Case sh(i).ExitCode
Case -2
msgbox("the command timed out")
Case 0
msgbox("the command succeeded")
Else
msgbox("Something else happened")
End Select
It’s working now. Seems that 100 is in ms… 
2 Likes
Now it encountered one problem. I want to run 4 shell’s in parallel, but it seems that with sync mode it waits to finish the first shell and then will start the second one. This is very time comsuming and not what I need… Any ide?
Yes, my suggestion above about creating subclasses and running them asynchronously.
This is doable, but requires some understanding of classes, subclasses, and handling events asynchronously. Show your work as you go and we’ll try to help.
1 Like
Completed is just waiting that the command is executed but not waiting for the result which may take in some cases longer. That’s a problem…