Chqnged threading stuff, back to jobs

This commit is contained in:
Sébastien QUEROL 2024-03-13 23:11:49 +01:00
parent 7824d55859
commit 5614dcb11c
1 changed files with 12 additions and 40 deletions

View File

@ -14,12 +14,6 @@
###############Thread Setup
$Global:syncHash = [hashtable]::Synchronized(@{})
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState ="STA"
$newRunspace.ThreadOptions ="ReuseThread"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
###########################
@ -32,9 +26,6 @@ $newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
# $ScriptPath = Get-ScriptDirectory
###
#### Premier runspace, contien le programme dans sa totalité
$MainWindowGUI = [PowerShell]::Create().AddScript({
# Chargement des assemblies externes
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
@ -400,13 +391,9 @@ $FMain.Text = "SharePoint Utils GUI"
#Onglet Move
##Lister les sites dans les comboBoxes
$BlistSites.Add_Click({
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState ="STA"
$newRunspace.ThreadOptions ="ReuseThread"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
$BlistSitesRunSpace = [PowerShell]::Create().AddScript({
$jobListSP = Start-Job -ScriptBlock {
# Votre fonction ici
#On vide la variable pnpConnection pour ne pas avoir de faux positifs
$pnpConnection=$Null
@ -434,10 +421,15 @@ $BlistSites.Add_Click({
$LresultsMove.Items.Clear()
$LresultsMove.Items.Add("Connectez-vous au tenant d'abord")
}
})
$BlistSitesRunSpace.PowerShell.Runspace = $newRunspace
$BlistSitesRunSpace.Thread = $BlistSitesRunSpace.PowerShell.BeginInvoke()
}
if ($jobListSP.State -eq "Completed") {
$resultListSP = Receive-Job -Job $jobListSP
$button.Invoke({
$button.Text = "Terminé"
$textBox.Text = $resultListSP
})
}
})
##Bouger les dossiers
@ -455,11 +447,6 @@ $BlistSites.Add_Click({
$Bgo.Add_Click({
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState ="STA"
$newRunspace.ThreadOptions ="ReuseThread"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
$copySPRunspace.PowerShell = [PowerShell]::Create().AddScript({
$destSite = $CBdestSite.Text
@ -471,8 +458,6 @@ $Bgo.Add_Click({
$jobStatus = Receive-PnPCopyMoveJobStatus -Job $jobCopyPending -Wait
Write-Output $jobStatus
})
$copySPRunspace.PowerShell.Runspace = $newRunspace
$copySPRunspace.Thread = $copySPRunspace.PowerShell.BeginInvoke()
})
@ -485,14 +470,8 @@ $tenantURL = $URLTenant.Text
$BconnectSP.Add_Click({
Disconnect-PnPOnline
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState ="STA"
$newRunspace.ThreadOptions ="ReuseThread"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
$connectToSPRunspace.PowerShell = [PowerShell]::Create().AddScript({
Connect-PnPOnline -Url $tenantURL -Interactive | Out-String
Connect-PnPOnline -Url $tenantURL -Interactive | Out-String
Switch ($ERROR)
{
@ -513,9 +492,7 @@ $BconnectSP.Add_Click({
$label4.ForeColor = [System.Drawing.Color]::Green
}
}
})
$connectToSPRunspace.PowerShell.Runspace = $newRunspace
$connectToSPRunspace.Thread = $connectToSPRunspace.PowerShell.BeginInvoke()
})
@ -540,8 +517,3 @@ $FMain.Add_Shown({$FMain.Activate()})
$ModalResult=$FMain.ShowDialog()
# Libération de la Form
$FMain.Dispose()
})
$MainWindowGUI.Runspace = $newRunspace
$data = $MainWindowGUI.BeginInvoke()