1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
'Projet Winform pour le ThreadPool
Public Class frmMDIPoolThread
'à virer
'Dim frmP1, frmP2, frmP3, frmP4, frmP5, frmP6, frmP7, frmP8, frmP9, frmP10, frmP11, _
' frmP12, frmP13, frmP14, frmP15, frmP16, frmP17, frmP18, frmP19, frmP20, frmP21 As New frmMDIPoolThread
'Instance d'une forme sur les 21
Dim mdiEnfantForm As Form
'Tableau pour les 21 formes
Dim tableauForm() As Form
'Classe associe à l'etat de chaque tache
Dim EtatPT As pointeuseEtat
'Tableau pour les 21 instances de la classe pointeuseEtat
Dim tableEtatTache() As pointeuseEtat
'Classe de Synchronisation qui sert à un thread pour informer
' qu'il est termine
Private Shared AsyncOperationTermine As New _
System.Threading.AutoResetEvent(False)
'Nbre de taches
Dim nbTache As Integer = 21
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
'--------------Cree les 21 forms avec un controle Label------------
ReDim tableauForm(nbTache - 1)
For I As Integer = 0 To nbTache - 1
mdiEnfantForm = New Form
mdiEnfantForm.MdiParent = Me
Dim objLabel As New Label
objLabel.Name = "Label1"
objLabel.Text = ""
objLabel.AutoSize = False
objLabel.Size = New Size(100, 100)
objLabel.Location = New Point(50, 50)
objLabel.BackColor = Color.LightSkyBlue
'ajoute le label à form
mdiEnfantForm.Controls.Add(objLabel)
tableauForm(I) = mdiEnfantForm
mdiEnfantForm.Name = "frmP" & (I + 1).ToString
mdiEnfantForm.Text = mdiEnfantForm.Name & " ->" & " Thread " & (I + 1).ToString
Next
End Sub
Private Sub frmMDIPoolThread_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Affiche les 21 forms
For I As Integer = 0 To nbTache - 1
tableauForm(I).Show()
Next
Me.LayoutMdi(MdiLayout.TileHorizontal)
End Sub
Private Sub MosaiqueHorizontaleToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MosaiqueHorizontaleToolStripMenuItem.Click
Me.LayoutMdi(MdiLayout.TileHorizontal)
End Sub
Private Sub BtnDemarreToutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDemarreToutToolStripMenuItem.Click
'frmP1.pause = False
'' ..........
'frmP21.pause = False
'frmP1.BGWorker.RunWorkerAsync()
'' ..........
'frmP21.BGWorker.RunWorkerAsync()
'appele sub de demarrage du pool de Taches
Call demarreTaches()
End Sub
'Sub charge du Demarrage des 21 threads dans un Pool
Private Sub demarreTaches()
ReDim tableEtatTache(nbTache - 1)
'Met en Queue chaque tache
For I As Integer = 0 To nbTache - 1
EtatPT = New pointeuseEtat
EtatPT.r_point = I + 1
EtatPT.StrNumThread = "Thread" & (I + 1).ToString
EtatPT.RetVal = ""
tableEtatTache(I) = EtatPT
Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback _
(AddressOf read_pointeuse), EtatPT)
'WaitOne =methode appele lorsque ce thread à termine.
'execute lorsqu'un thread a appele AsyncOperationTermine.Set())
'Afficher un message -immediatement- dans le controle Label de la forme concernee
AsyncOperationTermine.WaitOne()
'Acceder à l'UI par Classe pointeuseEtat
MsgBox(EtatPT.StrNumThread & " est termine....")
mdiEnfantForm = tableauForm(I)
mdiEnfantForm.Controls(0).Text = EtatPT.RetVal
Next
End Sub
'Sub "read pointeuese" modifie pour gerer les informations sur l'etat de chaque tache
Public Sub read_pointeuse(ByVal objEtat)
'If CZKEM1.Connect_Net(myip, 4370) Then
' If CZKEM1.ReadGeneralLogData(r_point) Then
' While CZKEM1.GetGeneralLogDataStr(r_point, dwEnrollNumber, dwVerifyMode, dwInOutMode, timeStr)
'----------virer ci-dessous-----------
'Application.DoEvents()
' Console.WriteLine(r_point & " ... " & dwEnrollNumber '& " ... " & dwVerifyMode & " ... " & dwInOutMode & " ... " & date1) 'remplacant
' End While
' End If
'End If
Dim Cond1 As Boolean = True
Dim Cond2 As Boolean = True
Dim Limit As Integer = 1000
Dim J As Integer = 1
If Cond1 Then
If Cond2 Then
Do While J < Limit
J = J + 1
Loop
End If
End If
objEtat.RetVal = "Tache terminee : " & J.ToString & " ... " & Date.Now
' Signale que ce thread est termine au pool.
AsyncOperationTermine.Set()
End Sub
End Class
'Classe information sur Tache =>
'stocke les informations sur l'etat de chaque tache "readpointeuse()"
Public Class pointeuseEtat
Public r_point As Integer
Public StrNumThread As String
Public RetVal As String
Public Sub New()
End Sub
End Class |