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
    Brian: Thank you so much Edward! :-)

  2. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Tom: Thank you Edward! After beating my head against a wall for days, tried your suggestion out and lo and...

  3. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Mike: DPM 2016 setup will fail if you have SQL Server Management Studio (SSMS) V17.x installed. Re-Install...

  4. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Rob: Edward, thanks man! you were a lifesaver. My scenario was Win Server 2016 from scratch, SQL 2016 (N...

  5. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Edward: It also crashes with the 4387 error if you have the SQL Management Studio 17 tools installed. Installing...

  6. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Ram: Hi - I followed richsmif instruction and was able to successfully install DPM 2016 on SQL 2016. Completed...

  7. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Neighborgeek: Thanks for the post, this is exactly the issue I am running into. I'm disappointed to see that you didn...

  8. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    richsmif: I have DPM 16 working with SQL 16. Install SQL 16 first, don't touch, install DPM 16 , upgrade to ...

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

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

Archive

Tag Cloud