# Shell Poll Timeout

**URL:** <https://forum.xojo.com/t/shell-poll-timeout/77881>\
**Category:** General\
**Created:** [October 30, 2023, 1:57pm UTC](https://forum.xojo.com/t/shell-poll-timeout/77881 "2023-10-30T13:57:46Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastian\_Fischer1](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@Sebastian\_Fischer1](https://forum.xojo.com/u/Sebastian_Fischer1)\
**Post date:** [October 30, 2023, 1:57pm UTC](https://forum.xojo.com/t/shell-poll-timeout/77881/1 "2023-10-30T13:57:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Gilles\_Plante](https://forum.xojo.com/user_avatar/forum.xojo.com/gilles_plante/32/667_2.png) [@Gilles\_Plante](https://forum.xojo.com/u/Gilles_Plante)\
**Post date:** [October 30, 2023, 2:02pm UTC](https://forum.xojo.com/t/shell-poll-timeout/77881/2 "2023-10-30T14:02:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sebastian\_Fischer1](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@Sebastian\_Fischer1](https://forum.xojo.com/u/Sebastian_Fischer1)\
**Post date:** [October 30, 2023, 2:04pm UTC](https://forum.xojo.com/t/shell-poll-timeout/77881/3 "2023-10-30T14:04:38Z")

</div>

Yes It’s running on windows. So timout for Shell is not working? This means that I have to program it by my own.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [October 30, 2023, 2:27pm UTC](https://forum.xojo.com/t/shell-poll-timeout/77881/4 "2023-10-30T14:27:23Z")

</div>

I’m afraid you have that backwards. `Timeout` is _only_ available on Windows. See:

[https://documentation.xojo.com/api/os/shell.html#shell-timeout](https://documentation.xojo.com/api/os/shell.html#shell-timeout)

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [October 30, 2023, 2:36pm UTC](https://forum.xojo.com/t/shell-poll-timeout/77881/5 "2023-10-30T14:36:28Z")

</div>

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:

```auto
sh(i).Execute(CMD(i))

if sh(i).ExitCode = -2 then
  // timed out
else
  // check sh(i).Result
end if

```

---

<div class="post-metadata">

**Author:** ![Gilles\_Plante](https://forum.xojo.com/user_avatar/forum.xojo.com/gilles_plante/32/667_2.png) [@Gilles\_Plante](https://forum.xojo.com/u/Gilles_Plante)\
**Post date:** [October 30, 2023, 2:42pm UTC](https://forum.xojo.com/t/shell-poll-timeout/77881/6 "2023-10-30T14:42:59Z")

</div>

> [@Kem\_Tekinay](#):
>
> I’m afraid you have that backwards.

Oh sorry, my bad 🙇‍♂️.

---

<div class="post-metadata">

**Author:** ![Sebastian\_Fischer1](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@Sebastian\_Fischer1](https://forum.xojo.com/u/Sebastian_Fischer1)\
**Post date:** [October 30, 2023, 3:53pm UTC](https://forum.xojo.com/t/shell-poll-timeout/77881/7 "2023-10-30T15:53:24Z")

</div>

Thanks.

What should or can I use instead of App.DoEvents?

It’s asnchronous 😉

---

<div class="post-metadata">

**Author:** ![Tim\_Parnell](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_parnell/32/161_2.png) [@Tim\_Parnell](https://forum.xojo.com/u/Tim_Parnell)\
**Post date:** [October 30, 2023, 3:58pm UTC](https://forum.xojo.com/t/shell-poll-timeout/77881/8 "2023-10-30T15:58:56Z")

</div>

The events of the Shell class.

[Completed](https://documentation.xojo.com/api/os/shell.html#shell-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.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [October 30, 2023, 4:01pm UTC](https://forum.xojo.com/t/shell-poll-timeout/77881/9 "2023-10-30T16:01:32Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![Sebastian\_Fischer1](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@Sebastian\_Fischer1](https://forum.xojo.com/u/Sebastian_Fischer1)\
**Post date:** [October 30, 2023, 4:02pm UTC](https://forum.xojo.com/t/shell-poll-timeout/77881/10 "2023-10-30T16:02:01Z")

</div>

It can happen that it is not completed therefore I need something like a timeout…if there is no response.

---

<div class="post-metadata">

**Author:** ![Tim\_Parnell](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_parnell/32/161_2.png) [@Tim\_Parnell](https://forum.xojo.com/u/Tim_Parnell)\
**Post date:** [October 30, 2023, 4:12pm UTC](https://forum.xojo.com/t/shell-poll-timeout/77881/11 "2023-10-30T16:12:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sebastian\_Fischer1](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@Sebastian\_Fischer1](https://forum.xojo.com/u/Sebastian_Fischer1)\
**Post date:** [October 30, 2023, 4:29pm UTC](https://forum.xojo.com/t/shell-poll-timeout/77881/12 "2023-10-30T16:29:10Z")

</div>

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.

```auto
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

```

---

<div class="post-metadata">

**Author:** ![Sebastian\_Fischer1](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@Sebastian\_Fischer1](https://forum.xojo.com/u/Sebastian_Fischer1)\
**Post date:** [October 30, 2023, 4:32pm UTC](https://forum.xojo.com/t/shell-poll-timeout/77881/13 "2023-10-30T16:32:52Z")

</div>

It’s working now. Seems that 100 is in ms… 😅

---

<div class="post-metadata">

**Author:** ![Sebastian\_Fischer1](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@Sebastian\_Fischer1](https://forum.xojo.com/u/Sebastian_Fischer1)\
**Post date:** [October 30, 2023, 5:16pm UTC](https://forum.xojo.com/t/shell-poll-timeout/77881/14 "2023-10-30T17:16:10Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [October 30, 2023, 6:01pm UTC](https://forum.xojo.com/t/shell-poll-timeout/77881/15 "2023-10-30T18:01:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sebastian\_Fischer1](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@Sebastian\_Fischer1](https://forum.xojo.com/u/Sebastian_Fischer1)\
**Post date:** [October 31, 2023, 8:09am UTC](https://forum.xojo.com/t/shell-poll-timeout/77881/16 "2023-10-31T08:09:05Z")

</div>

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…
