Mic*_*eld 8 .net powershell multithreading
在调试使用Semaphores进行跨进程同步的应用程序时,我偶然发现了使用PowerShell取代"其他"进程的想法.在PowerShell中做这样的事情很好:
// In C# application:
var sem = new Semaphore(0, 1, "CrossProcSem");
sem.WaitOne();
# In PowerShell session:
[1] C:\Projects $ $sem = New-Object System.Threading.Semaphore(0, 1, "CrossProcSem")
[2] C:\Projects $ $sem.Release()
Run Code Online (Sandbox Code Playgroud)
我可以打电话WaitOne()和Release()基于信号的同一实例多次,经常因为我需要.
但是当我尝试使用Mutex做同样的事情时,PowerShell一直声称该互斥体被放弃了:
[1] C:\Projects $ $mtx = New-Object System.Threading.Mutex($false, "CrossProcMtx")
[2] C:\Projects $ $mtx.WaitOne()
True
[3] C:\Projects $ $mtx.ReleaseMutex()
[4] C:\Projects $ $mtx.WaitOne()
Exception calling "WaitOne" with "0" argument(s): "The wait completed due to an abandoned mutex."
At line:1 char:13
+ $mtx.WaitOne <<<< ()
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : DotNetMethodException
Run Code Online (Sandbox Code Playgroud)
这个错误似乎发生WaitOne()在我之前曾经获得过互斥锁之后调用的任何时候,无论是之前的WaitOne调用还是要求它最初在构造函数中拥有:
[5] C:\Projects $ $mtx2 = New-Object System.Threading.Mutex($true)
[6] C:\Projects $ $mtx2.WaitOne()
Exception calling "WaitOne" with "0" argument(s): "The wait completed due to an abandoned mutex."
At line:1 char:14
+ $mtx2.WaitOne <<<< ()
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : DotNetMethodException
[7] C:\Projects $ $mtx3 = New-Object System.Threading.Mutex
[8] C:\Projects $ $mtx3.WaitOne()
True
[9] C:\Projects $ $mtx3.WaitOne()
Exception calling "WaitOne" with "0" argument(s): "The wait completed due to an abandoned mutex."
At line:1 char:14
+ $mtx3.WaitOne <<<< ()
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : DotNetMethodException
Run Code Online (Sandbox Code Playgroud)
Powershell是在后台做了一些奇怪的线程诡计,还是我完全忘记了互斥锁是如何工作的?
x0n*_*x0n 26
默认情况下,powershell v2.0(在控制台,而不是图形ISE)使用MTA线程池.这意味着每个交互式行在不同的线程上执行:
PS> [threading.thread]::CurrentThread.ManagedThreadId
13
PS> [threading.thread]::CurrentThread.ManagedThreadId
10
PS> [threading.thread]::CurrentThread.ManagedThreadId
8
PS> [threading.thread]::CurrentThread.ManagedThreadId
4
Run Code Online (Sandbox Code Playgroud)
但是,非交互式脚本将在单个线程下运行,也就是说,调用命令来运行它的线程:
PS> $script = {
>> [threading.thread]::CurrentThread.ManagedThreadId
>> [threading.thread]::CurrentThread.ManagedThreadId
>> [threading.thread]::CurrentThread.ManagedThreadId
>> [threading.thread]::CurrentThread.ManagedThreadId
>> }
>>
PS> & $script
16
16
16
16
Run Code Online (Sandbox Code Playgroud)
如果要使用单个线程以交互方式运行powershell,请使用-STA开关启动shell.您可以以交互方式执行此操作:
PS> powershell -sta
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
PS> $host.runspace | select threadoptions, apartmentstate
ThreadOptions ApartmentState
------------- --------------
ReuseThread STA
Run Code Online (Sandbox Code Playgroud)
如您所见,powershell将使用单线程单元来执行交互式命令.这通常是使用WPF或WinForms的理想选择,或者如果您想使用系统范围的互斥锁:
PS> $mtx = New-Object System.Threading.Mutex($false, "CrossProcMtx")
PS> $mtx.WaitOne()
True
PS> $mtx.ReleaseMutex()
PS> $mtx.WaitOne()
True
Run Code Online (Sandbox Code Playgroud)
在btw,powershell v3(与Windows 8一起发布,在win 7上也是可用的下层)使用-STA作为控制台的默认模式.图形PowerShell ISE始终在v2和v3中使用-STA.
| 归档时间: |
|
| 查看次数: |
5223 次 |
| 最近记录: |