Torna al Thread
Public Class frmInvitiRevisioni
Inherits System.Windows.Forms.Form
Dim Elab As New claInvitiRevisioni()
Dim Processo As New System.Threading.Thread(AddressOf Elab.Elaborazione)
(...)
Private Sub ElaborazioneTerminata()
Try
MsgBox("Elaborazione Terminata")
Processo = Nothing
Elab = Nothing
Catch exc As Exception
meErrori.RegistrazioneErrore(Err, exc, True) <----- E' una classe che gestisce gli errori generando anche files di log
End Try
End Sub
(...)
Private Sub Estrapolazione(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
btnEstrapola.Click
Try
' Verifica in che stato ci si trova
Select Case btnEstrapola.Text
' Estrapolazione
Case cnsEstrapola <---- Il pulsante di avvio può avere 2 stati, uni per avviare il thread ed uno per interromperlo
If txtFileOrigine.Text <> "" And _
txtFileDestinazione.Text <> "" Then
If IO.File.Exists(txtFileOrigine.Text) Then
Elab = New claInvitiRevisioni()
Processo = New System.Threading.Thread(AddressOf Elab.Elaborazione)
' Cambia l'etichetta del pulsante
btnEstrapola.Text = cnsInterrompi <----- Cambia lo stato del pulsante nello stato di interruzione del Thread
' Associa questa finestra alla variabile form della classe del thread
Elab.parChiamante = Me <------ All'interno del thread viene passata la form chiamante per poter effettuare modifiche su di essa
' Aggancia l'evento Terminato alla procedura locale
AddHandler Elab.Terminato, AddressOf ElaborazioneTerminata
' Avvia il thread
Processo.Start()
Else
MsgBox("Il file di origine selezionato non esiste.", MsgBoxStyle.ApplicationModal + MsgBoxStyle.Information + MsgBoxStyle.OKOnly, "FILE MANCANTE")
End If
Else
MsgBox("Compilare tutti i campi.", MsgBoxStyle.ApplicationModal + MsgBoxStyle.Information + MsgBoxStyle.OKOnly, "VALORI MANCANTI")
End If
' Interruzione
Case cnsInterrompi <---- Stato del pulsante su interruzione
' Chiede se si vuole interrompere il thread
If MsgBox("Sicuri di voler interrompere l'esecuzione?", MsgBoxStyle.ApplicationModal + MsgBoxStyle.Question + MsgBoxStyle.YesNo, "INTERRUZIONE") = MsgBoxResult.Yes Then
' Esegue la procedura di chiusura del thread
Elab.Chiusura()
Processo.Abort()
Processo.Join()
ElaborazioneTerminata()
' Cambia la dicitura del pulsante
btnEstrapola.Text = cnsEstrapola <---- Il pulsante torna nello stato di avvio thread
End If
End Select
Catch exc As Exception
meErrori.RegistrazioneErrore(Err, exc, True)
End Try
End Sub