Does this help at all?
https://www.codeproject.com/Tips/895840/Multi-Threaded-PowerShell-Cookbook
https://www.codeproject.com/Tips/895840/Multi-Threaded-PowerShell-Cookbook
I just love reading a thread full of Greek lol.
I cobbled together some tutorials and it works for me, hopefully you can modify that to test with one bit of your script at a time this one just gets info on your drives and spits them out with a 2 second delay between each drive.
This is what it looks like for me:
Does this help at all?
https://www.codeproject.com/Tips/895840/Multi-Threaded-PowerShell-Cookbook
I think it's breaking because mine is complicated thread wise. I have a thread for the GUI, thread for the code that runs on window load (connect to o365 etc), thread for onclick code.. idk, just seems to die. No code executes. The GUI loads but nothing happens when I do stuff.
I feel like that's too many threads lol.
Rather than manually manage threads, can PowerShell do tasks asynchronously?
What I not quite sure of, when you create a new runspace and thread, whether it is literally putting it on a different CPU thread/core, or whether it's an in-software thing or at the O/S level ?
static void Main(string[] args)
{
//First way, asynchronously
DoSomethingAsync();
//Second way, asynchronously
Task.Run(() => DoSomethingAsync);
//Third way, run async method synchronously (meaning you wait for the return value)
Task.Run(() => DoSomethingAsync).Wait();
}
public async void DoSomethingAsync()
{
//do something...
}
Not sure how PS manages threads.
As for the async bit I mentioned above, in C# it'd be something like this:
Code:static void Main(string[] args) { //First way, asynchronously DoSomethingAsync(); //Second way, asynchronously Task.Run(() => DoSomethingAsync); //Third way, run async method synchronously (meaning you wait for the return value) Task.Run(() => DoSomethingAsync).Wait(); } public async void DoSomethingAsync() { //do something... }
That seems about 1000x easier than PowerShell I guess 98% of PS use cases are so lightweight Microsoft never envisioned needing real multithreaded support.
I love learning to code even though I am too dumb to ever understand the advanced stuff. My brain melts when I look at complex code. Maybe one day I will get there if I stick at it for long enough and read enough various explanations on how stuff works... I think the biggest inhibitor I have to learning is my danged crappy memory. Really holds me back. The amount of times I have to read something for it to stick. I use basic PowerShell commands like Get-mailboxPermissions against Office 365 on a weekly basis at work, and I still occassionally forget the correct syntax.
edit:
And lol see I got it wrong. It's Get-MailboxPermission. Not plural. No S. I've typed that line at work probably several hundred times...