Monthly archives: April 2009

Timothy

Safety Through Torture Meme

Would the "torturing keeps us safe from terrorists" meme please die already?  Please?  Even if it torture does work (it doesn't), it is still morally wrong!  Terrorism is not as much about killing as about making everybody afraid of being killed.  If we are so afraid that we throw morals out the window, then the terrorists have won.

Timothy

Automating Your VB.Net App Using PowerShell May Lead to STAThread Exceptions

I'm adding PowerShell support to RD Tabs to create a scripting environment where the application can be automated in many wonderful ways.  I followed the super easy instructions at the PolyMon project for adding PowerShell support.  The basic tests succeeded and I was happy.  Then I tried actively integrating RD Tabs with the PowerShell script by providing a pre-defined variable that contained various methods for controlling aspects of RD Tabs.  The simplest example was creating a new tab from within the script itself.  When I ran the script and got to the step that executed that function, I got the following exception.

Current thread must be set to single thread apartment (STA) mode before OLE calls can be made

That's quite strange, because VB.Net Windows Forms applications are STA by default.  If you don't know what STA is, don't worry about.  Just know that it's related to how COM handles multithreading.  Since RD Tabs itself extensively P/Invokes things, I do make heavy use of COM interop.  However, there are plenty of built-in framework classes that also wrap COM, so mismatched threading models may happen even without any P/Invokes. 

Why was I getting an error that implied I was not set to STA?  It turns out the problem is with PowerShell.  PowerShell runs in MTA mode by default (the other threading model).  When you create a new RunSpace and Pipeline, you are effectively creating a new thread for PowerShell to run on.  Since it's a brand new thread, it sets it to the default that PowerShell uses, MTA.

Thankfully, The Google did not fail me.  I found somebody else having a similar problem with PowerShell when creating a snap-in for MMC.  The solution is to cleverly dig into the RunSpace and essentially tell PowerShell to execute scripts on the hosting application's thread.  The original author used C#, so below is a translated VB.Net summary without any exception handling.

Dim rs As Runspace = Nothing 
rs = RunspaceFactory.CreateRunspace
rs.Open()

' Set the default runspace to the one we just created
Runspace.DefaultRunspace = rs

' Fetch the EngineIntrinsics from the runspace (this is needed so we run the engine on the current thread
' (which will be STA instead of MTA) which is required because VB.Net defaults to STA)
Dim ei As EngineIntrinsics = CType(rs.SessionStateProxy.GetVariable("ExecutionContext"), EngineIntrinsics)

' Create a scriptblock from the engine, supply the user code
Dim sb As ScriptBlock = ei.InvokeCommand.NewScriptBlock("Insert Your PowerShell Code Here")

' Invoke the script
sb.Invoke()

I'm glad there's a solution, but I'm also a bit perplexed that there isn't an easy property you can use to change PowerShell's default threading model.  Such as rs.SetThreadingModel() or something.  Oh well.  Such is life!

Timothy

Deep Thought

Why is it acceptable that employment is a top-down dictatorship when government is a bottom-up democracy?  I've been thinking about this and I can't figure out why those two completely opposite approaches seem so unquestioningly compatible to basically everybody (myself included) in day-to-day life.

Blog

Search Posts

Recent Comments

  1. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    ptbNPA: That should have been *ID 810*, not 820

  2. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    ptbNPA: For anyone else coming across this in the future and have an ID 820 error: For some strange reason...

  3. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Timothy: Sorry, I don't have any other insight. I'm sure you ran into all the same articles I did about the error...

  4. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    ptbNPA: FYI, after posting the issue on technet + windows-noob.com with no replies I decided to do a fresh Windows...

  5. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    ptbNPA: Hi and thanks for your post. I originally installed SQL 2016 with SP1 on a Windows server 2016 and got...

  6. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Timothy: Update Rollup 2.

  7. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Catwiesl: You write "Microsoft System Center Data Protection Manager (DPM) officially supports SQL 2016 with U...

  8. Re: PowerShell: Quickly Finding the Oldest and Newest Files in a Folder
    Neki: i would like to compare a local file with a remote file and download the remote file if its newer than...

  9. Re: RD Tabs: Tabbed Windows Remote Desktops
    lukas: Fantastic software though I am having issues saving passwords. As soon as I kill the program the setting...

  10. Re: Easy Way to Change Permissions on the Windows Server Scheduled Tasks Folder (C:\Windows\Tasks)
    George: From another server, using an admin account, I opened \\SERVER\c$\windows in Windows Explorer. Tasks...

Archive

Tag Cloud