Hi,
Powershell Studio uses Timer for Add-JobTracker and provide a Timer control for the ToolBox. I found another approach for background job: BackgroundWorker. I successfully used it for my 'pure c# Windows Form' project, now I wonder:
What are differences between those? Does anyone have experience with using two aproach ?
Timer vs BackgroundWorker
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
- ALIENQuake
- Posts: 112
- Last visit: Mon Jan 29, 2024 7:35 am
- Has voted: 4 times
Re: Timer vs BackgroundWorker
Look up runspaces.
Jobs are easier to manage than runspaces and other Net methods like threads and tasks.
BackgroundWorker is just a thread. See: https://blog.stephencleary.com/2013/05/ ... intro.html
PowerShell is not multi-threaded and cannot easily support threads without a separate runspace. PsJob is an external runspace. C# forms are multi-threaded and can use threads.
PowerShell cannot use "Run" or "Show" with a form. PS cannot run a true main window. All Forms in PS must be modal dialogs.
Note that the PowerShell application is a console app and can/does run multiple threads but it is just a host for the PS engine which only supports a single thread. All scripts must run on this single thread. A runspace is a separate instance of the PS engine. With Jobs and runspaces there is no direct access to variables between runspaces or between PS and its jobs.
Jobs are easier to manage than runspaces and other Net methods like threads and tasks.
BackgroundWorker is just a thread. See: https://blog.stephencleary.com/2013/05/ ... intro.html
PowerShell is not multi-threaded and cannot easily support threads without a separate runspace. PsJob is an external runspace. C# forms are multi-threaded and can use threads.
PowerShell cannot use "Run" or "Show" with a form. PS cannot run a true main window. All Forms in PS must be modal dialogs.
Note that the PowerShell application is a console app and can/does run multiple threads but it is just a host for the PS engine which only supports a single thread. All scripts must run on this single thread. A runspace is a separate instance of the PS engine. With Jobs and runspaces there is no direct access to variables between runspaces or between PS and its jobs.
- ALIENQuake
- Posts: 112
- Last visit: Mon Jan 29, 2024 7:35 am
- Has voted: 4 times
Re: Timer vs BackgroundWorker
Thnaks!
Now I think I get it: there is no point to use BackgroundWorker from Powershell because it requires separate thread which PS supports only in external separate instance of the PS engine. So if you create it, you can simply run tasks instead of warping it inside BackgroundWorker.
TW: You linked great article, I will switch my c# BackgroundWorker code to Task.Run.
Now I think I get it: there is no point to use BackgroundWorker from Powershell because it requires separate thread which PS supports only in external separate instance of the PS engine. So if you create it, you can simply run tasks instead of warping it inside BackgroundWorker.
TW: You linked great article, I will switch my c# BackgroundWorker code to Task.Run.
Re: Timer vs BackgroundWorker
No but you can use runspaces which are PowerShell background workers. They are based on the Net "Task" class and are easier to use.
Example: https://1drv.ms/u/s!AjiiPtIUqzK_he0C0i2623NXwMRc0Q
Example: https://1drv.ms/u/s!AjiiPtIUqzK_he0C0i2623NXwMRc0Q