Multithreading in Winform RRS feed

  • Question

  • How can I do multithreading in Powershell with GUI, I created a script file with some features, it has a GUI to interact with, but I don't know how to do that. I did lots of searching but multithread in powershell is very rare. I knew Background worker in Winform will not work in powershell, I also read about runspace but it seems that we can just collect result from it at the end, I want a scriptblock to run continuously while also updating the form. So I tried timer, it works but not really good. More specifically, in my case I create a form to check for new gmail mail, by downloadstring from When I use system.timers.timer, it creates another thread so the GUI is not freeze but it cannot update the GUI. When I use, it can update GUI but it freezes the GUI a bit when time elapsed. I think it's because the net.webclient downloadstring method that blocks the thread so I tried downloadstringasync but when I used downloadstringcompleted event, powershell stop working, don't know why. I want my GUI to be smooth with no freeze.

    Does anyone have any solution? Please help me. It takes lots of time when things involve multithreading!

    Functions timer and chkmail in my script can be found here

    Tuesday, October 20, 2015 4:17 PM


All replies

  • There is no multi-threading in PowerShell and Forms in PS must have only one thread.  They are "modal dialogs" and can only manage one event at a time.

    To do background processing use jobs or runspaces.



    • Marked as answer by Eikacy Wednesday, October 21, 2015 9:57 AM
    Tuesday, October 20, 2015 4:48 PM
  • Thank you! Now my script is working greatly, I tried using job first but the form still not responsive, then I read about runspace here and I used it along with timer.
    Thursday, October 22, 2015 5:23 PM