Ho un problema con PowerShell e forse qualcuno può aiutarmi. Sto usando PowerShell 2.0 e voglio creare e usare i thread. So che posso usare i lavori, ma non è quello che voglio. Voglio uno script, che crea i moduli di Windows ed esegue anche i thread in background. Poiché le forme hanno bisogno di STA, questo non è facile. L'esecuzione di "powershell.exe -sta" non è una soluzione.Problema di gestione thread Powershell
Di seguito è riportato il mio script che ho scritto, per la gestione semplice dei thread. Ma non funziona. Anche il nuovo thread non verrà creato. Qualche suggerimento, cosa c'è che non va? Per favore aiutami se puoi!
Saluti, Peter.
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.")
Grazie, ci provo se posso. –