Tengo un problema con Powershell, y tal vez alguien me puede ayudar. Estoy usando powershell 2.0 y quiero crear y usar hilos. Sé que puedo usar trabajos, pero eso no es lo que quiero. Quiero un script, que crea formularios de Windows y ejecuta hilos de fondo también. Como las formas necesitan STA, esto no es fácil. Ejecutar "powershell.exe -sta" no es una solución.Problema de manejo de la rosca Powershell
A continuación se muestra mi script que escribí, para el manejo simple de hilos. Pero no funciona. Incluso el nuevo hilo no se creará. Cualquier sugerencia, ¿qué está mal? ¡Por favor ayúdame si puedes!
Saludos, 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.")
Gracias, intentaré esto si puedo. –