Timer vs BackgroundWorker

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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.
This topic is 5 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ALIENQuake
Posts: 112
Last visit: Mon Jan 29, 2024 7:35 am
Has voted: 4 times

Timer vs BackgroundWorker

Post by ALIENQuake »

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 ?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Timer vs BackgroundWorker

Post by jvierra »

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.
User avatar
ALIENQuake
Posts: 112
Last visit: Mon Jan 29, 2024 7:35 am
Has voted: 4 times

Re: Timer vs BackgroundWorker

Post by ALIENQuake »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Timer vs BackgroundWorker

Post by jvierra »

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
This topic is 5 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked