Potentially the longest thread in history...

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 :p 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:
NtIuSie.png
 

Attachments

  • ExampleThreading.ps1.txt
    4.2 KB · Views: 4
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 :p 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:
NtIuSie.png

Yeah it works fine for really small scripts like that. I cooked up a similar really small sample.

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.

Doesn't help that I don't really understand at all how that runspace/hash code works, so I am surely misinterpreting something.

I think it's failing at the part you skipped :p taking input from the form. Unless your PC is callled "MMMMM" :p As I see no reference to your Input1 field anywhere other than the XAML. So it's essentially pointless ?




Thanks, will take a read.
 
Last edited:
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?
 
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 ?

PowerShell has a Start-Job / Get-Job / Retrieve-Job feature. But a) nothing happened when I put my stuff in the Start-job script block, and b) apparently they're kind of ugly and use excessive resources etc. So I didn't try for long. But yeah, in theory, you can run a bunch of slow commands inside several PS-Jobs (Start-Job) at the same time while letting other code execute.
 
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 ?

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...
}
 
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 :p 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. :annoyed:

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...
 
Last edited:
That seems about 1000x easier than PowerShell :p 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. :annoyed:

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

Honestly I have a harder time remembering PowerShell stuff than other programming language syntax (C#, JavaScript, SQL, etc.). Not sure if that's just b/c I use the others more (probably part of it).

And really, learning to code isn't that tough. The hardest part is understanding the logic behind languages..once you've learned that, and learned some basic syntax, you can pick up just about any language after looking at the proper syntax, because most of the logic is the same in a lot of languages (e.g. loops, if/else conditionals, functions, etc.).
 
Back
Top Bottom