私はpowershellの問題があり、誰かが私を助けてくれるかもしれません。 PowerShell 2.0を使用しています。作成してスレッドを使用したいと思います。私は仕事を使うことができることを知っていますが、それは私が望むものではありません。 スクリプトを作成して、Windowsフォームを作成し、バックグラウンドスレッドも実行します。フォームにはSTAが必要なので、これは簡単ではありません。 "powershell.exe -sta"を実行することは解決策ではありません。Powershellスレッド処理の問題
以下は、単純なスレッド処理のために書いたスクリプトです。しかし、それは動作しません。新しいスレッドは作成されません。どのような提案、何が間違っていますか?あなたができるなら私を助けてください!
よろしくお願いします。
function ThreadProc() {
for ($i = 0; $i -lt 10; $i++) {
$ApartmentState = [System.Threading.Thread]::CurrentThread.GetApartmentState()
Write-Host "ThreadProc ($ApartmentState): $i"
# Yield the rest of the time slice.
[System.Threading.Thread]::Sleep(0)
}
}
$ApartmentState = [System.Threading.Thread]::CurrentThread.GetApartmentState()
Write-Host "Main thread ($ApartmentState): Start a second thread."
$thread_job = New-Object System.Threading.ThreadStart(ThreadProc)
$thread = New-Object System.Threading.Thread($thread_job)
$thread.CurrentThread.SetApartmentState([System.Threading.ApartmentState]::STA)
$thread.Start()
for ($i = 0; $i -lt 4; $i++) {
Write-Host("Main thread: Do some work.")
[System.Threading.Thread]::Sleep(0)
}
Write-Host("Main thread: Call Join(), to wait until ThreadProc ends.")
$thread.Join()
Write-Host("Main thread: ThreadProc.Join has returned. Program end.")
可能であれば、私はこれを試してみます。 –