From e47906eb4ab4b7f35586c8455bf072ccbb51e426 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Fri, 12 May 2023 18:09:40 +0200 Subject: [PATCH] Ok con gestione riavvio threads se cambiano/calano --- WebDoorCreator.CamSrv/ProcMan.Designer.vb | 13 +++ WebDoorCreator.CamSrv/ProcMan.resx | 6 ++ WebDoorCreator.CamSrv/ProcMan.vb | 119 ++++++++++++++++++---- WebDoorCreator.SDK/WDC.cs | 7 +- 4 files changed, 121 insertions(+), 24 deletions(-) diff --git a/WebDoorCreator.CamSrv/ProcMan.Designer.vb b/WebDoorCreator.CamSrv/ProcMan.Designer.vb index 2a1242d..ad111c0 100644 --- a/WebDoorCreator.CamSrv/ProcMan.Designer.vb +++ b/WebDoorCreator.CamSrv/ProcMan.Designer.vb @@ -22,6 +22,7 @@ Partial Class ProcMan 'Do not modify it using the code editor. _ Private Sub InitializeComponent() + Me.components = New System.ComponentModel.Container() Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(ProcMan)) Me.btnTestPing = New System.Windows.Forms.Button() Me.lblpingTest = New System.Windows.Forms.Label() @@ -39,6 +40,8 @@ Partial Class ProcMan Me.Label1 = New System.Windows.Forms.Label() Me.txtNumThread = New System.Windows.Forms.TextBox() Me.lblRunning = New System.Windows.Forms.Label() + Me.TimerUI = New System.Windows.Forms.Timer(Me.components) + Me.TimerProgBar = New System.Windows.Forms.Timer(Me.components) Me.StatusStrip1.SuspendLayout() Me.SuspendLayout() ' @@ -151,6 +154,7 @@ Partial Class ProcMan ' Me.tsProgBar.Name = "tsProgBar" Me.tsProgBar.Size = New System.Drawing.Size(100, 16) + Me.tsProgBar.Step = 1 ' 'txtQueue ' @@ -193,6 +197,13 @@ Partial Class ProcMan Me.lblRunning.Text = "running: -" Me.lblRunning.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' + 'TimerUI + ' + Me.TimerUI.Interval = 300 + ' + 'TimerProgBar + ' + ' 'ProcMan ' Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) @@ -239,4 +250,6 @@ Partial Class ProcMan Friend WithEvents Label1 As Label Friend WithEvents txtNumThread As TextBox Friend WithEvents lblRunning As Label + Friend WithEvents TimerUI As Timer + Friend WithEvents TimerProgBar As Timer End Class diff --git a/WebDoorCreator.CamSrv/ProcMan.resx b/WebDoorCreator.CamSrv/ProcMan.resx index e79a3b6..21c91b2 100644 --- a/WebDoorCreator.CamSrv/ProcMan.resx +++ b/WebDoorCreator.CamSrv/ProcMan.resx @@ -120,6 +120,12 @@ 17, 17 + + 134, 17 + + + 228, 17 + diff --git a/WebDoorCreator.CamSrv/ProcMan.vb b/WebDoorCreator.CamSrv/ProcMan.vb index f527f99..cc3811f 100644 --- a/WebDoorCreator.CamSrv/ProcMan.vb +++ b/WebDoorCreator.CamSrv/ProcMan.vb @@ -4,7 +4,23 @@ Imports System.Text Imports System.Threading Imports WebDoorCreator.SDK -Partial Public Class ProcMan +Public Class ProcMan + +#Region "Public Constructors" + + Public Sub New() + + ' This call is required by the designer. + InitializeComponent() + + ' Add any initialization after the InitializeComponent() call. + synchronizationContext = SynchronizationContext.Current + StartUiThread() + startAllThreads() + + End Sub + +#End Region #Region "Public Methods" @@ -18,6 +34,7 @@ Partial Public Class ProcMan Next sb.AppendLine() txtQueue.Text = sb.ToString() + txtQueue.Invalidate() End Sub Public Sub SetMaxCamInstances(value As Integer) @@ -28,36 +45,34 @@ Partial Public Class ProcMan #End Region - 'Private ReadOnly synchronizationContext As SynchronizationContext - - 'Private Sub performBarAdvance() - ' synchronizationContext.Post(New SendOrPostCallback(Function(o) - ' tsProgBar.PerformStep() - - ' If tsProgBar.Value >= tsProgBar.Maximum Then tsProgBar.Value = 0 End If - - ' tsProgBar.Invalidate() - ' End Function), "") - 'End Sub - #Region "Private Fields" + Private ReadOnly synchronizationContext As SynchronizationContext + ' caricamento del NEXT STACK da redis (come oggetto) PROD http : https://iis01.egalware.com/WDC/SRV/ ' DEV: https : https://localhost:7043/ Dim baseIp As String = "iis01.egalware.com" Dim baseUrl As String = "https://iis01.egalware.com/WDC/SRV/" - Dim codPost As String = "WRK001" ' nome macchina calcolo + + Dim codPost As String = "WRK001" + + ' nome macchina calcolo Dim currWDC As WDC = New WDC(baseIp, baseUrl, codPost) + Dim idxSim As Integer = 0 + Dim m_bExecutionThreadStoped As Boolean = False + Dim m_bStopProcess As Boolean = False + Dim m_ExecutionThread As Thread 'Private m_MaxCamInstances As Integer = 8 Private m_MaxCamInstances As Integer = 1 Dim risultatoPing As PingReply = Nothing + Dim ThreadList As Thread() #End Region @@ -179,7 +194,8 @@ Partial Public Class ProcMan If bOk Then bOk = currWDC.testAlive End If - Else bOk = True + Else + bOk = True End If ' se connessione non ok o processo fermato, fermo i thread If Not bOk OrElse bStopMainProcess Then @@ -229,6 +245,7 @@ Partial Public Class ProcMan n30SecCounter = 1 End If Thread.Sleep(1000) + End While End Sub @@ -247,6 +264,36 @@ Partial Public Class ProcMan Return bOk End Function + Private Sub performBarAdvance() + synchronizationContext.Post(New SendOrPostCallback( + Sub(o) + tsProgBar.PerformStep() + If tsProgBar.Value >= tsProgBar.Maximum Then + tsProgBar.Value = 0 + End If + + tsProgBar.Invalidate() + End Sub + ), "") + End Sub + + Private Sub performUpdateUI() + synchronizationContext.Post(New SendOrPostCallback( + Sub(o) + + lblRunning.Text = $"threads: {ThreadCount()}/{m_MaxCamInstances}" + lblRunning.Invalidate() + DisplayQueueStatus() + 'verifico thread attivi e se calano --> riavvia.. + If ThreadCount() <> m_MaxCamInstances Then + stopAllThreads() + Thread.Sleep(100) + startAllThreads() + End If + End Sub + ), "") + End Sub + Private Sub startAllThreads() m_bStopProcess = False ' fix colori @@ -261,6 +308,9 @@ Partial Public Class ProcMan ' avvio thread di gestione della macchina che avvia la connessione m_ExecutionThread.Start() + ' avvio il timer di refresh + TimerProgBar.Enabled = True + TimerProgBar.Start() #If False Then '' recupero Id dei DDF 'Dim sDdfRoot As String = "c:\EgtData\WebDoor\Ddf" @@ -494,6 +544,12 @@ Partial Public Class ProcMan startAllThreads() End Sub + Private Sub StartUiThread() + ' avvio timer progBar + TimerUI.Enabled = True + TimerUI.Start() + End Sub + Private Sub stopAllThreads() m_bStopProcess = True ' fix colori @@ -504,12 +560,29 @@ Partial Public Class ProcMan Thread.Sleep(10) End While m_ExecutionThread = Nothing + ' fix timer + TimerProgBar.Enabled = False + TimerProgBar.Stop() End Sub Private Sub StopProcess_Click(sender As Object, e As EventArgs) Handles StopProcess.Click stopAllThreads() End Sub + Private Function ThreadCount() As Integer + Dim numCount = 0 + If Not IsNothing(ThreadList) Then + ' conto i NON nulli + For Each Thread In ThreadList + If Not IsNothing(Thread) Then + numCount += 1 + End If + Next + End If + ' rendo + Return numCount + End Function + Private Sub ThreadFunction() Dim sExePath As String = "c:\EgtProg\EgtEngine\EgtEngineR32.exe" @@ -586,17 +659,25 @@ Partial Public Class ProcMan End Sub + Private Sub TimerProgBar_Tick(sender As Object, e As EventArgs) Handles TimerProgBar.Tick + ' esegue refresh prog bar + performBarAdvance() + End Sub + + Private Sub TimerUI_Tick(sender As Object, e As EventArgs) Handles TimerUI.Tick + ' effettuo refresh code e status thread + performUpdateUI() + End Sub + Private Sub txtNumThread_TextChanged(sender As Object, e As EventArgs) Handles txtNumThread.TextChanged Dim numReq As Integer = 1 Integer.TryParse(txtNumThread.Text, numReq) ' controllo se cambiato il num thread If numReq <> m_MaxCamInstances Then stopAllThreads() - startAllThreads() + SetMaxCamInstances(numReq) + 'startAllThreads() End If - - SetMaxCamInstances(numReq) - End Sub #End Region diff --git a/WebDoorCreator.SDK/WDC.cs b/WebDoorCreator.SDK/WDC.cs index 4197b18..9de45a3 100644 --- a/WebDoorCreator.SDK/WDC.cs +++ b/WebDoorCreator.SDK/WDC.cs @@ -63,11 +63,8 @@ namespace WebDoorCreator.SDK try { var rawData = callUrlGet(urlAlive); - if (rawData != "ERR") - { - string returnData = JsonConvert.DeserializeObject(rawData); - answ = returnData == "OK"; - } + string returnData = JsonConvert.DeserializeObject(rawData); + answ = returnData == "OK"; } catch { }