Oppure

Loading
09/03/08 8:09
Il Totem
Allora possiamo fare in questo modo. Ammettiamo che le operazioni debbano essere svolte su un file:

'Questo delegate esprime una procedura che opera su un file
Public Delegate Sub DoAction(ByVal FileName As String)

'Questa struttura memorizza l'operazione da eseguire
Public Structure FileAction
    Private _Action As DoAction
    Private _FileName As String

    Public Property Action() As DoAction
        Get
            Return _Action
        End Get
        Set(ByVal value As DoAction)
            If value IsNot Nothing Then
                _Action = value
            End If
        End Set
    End Property

    Public Property FileName() As String
        Get
            Return _FileName
        End Get
        Set(ByVal value As String)
            _FileName = value
        End Set
    End Property

    Sub New(ByVal A As DoAction, ByVal F As String)
        Me.Action = A
        Me.FileName = F
    End Sub
End Structure

'Questa lista rappresenta l'elenco di tutte le operazioni
Public AllActions As New List(Of FileAction)

Private Sub CreateNewFile(ByVal F As String)
    IO.File.Create(F).Close()
End Sub

Private Sub Form4_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Shown
    'Qua carichi la lista come vuoi, ad esempio ci puoi mettere questi valori:
    With AllActions
        'Elimina un file
        .Add(New FileAction(AddressOf IO.File.Delete, "C:\file.txt"))
        'Crea un nuovo file vuoto
        .Add(New FileAction(AddressOf Me.CreateNewFile, "C:\newfile.txt"))
    End With
    'Quindi attiva il timer
    Timer1.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    'Esegue la prima azione
    If AllActions.Count > 0 Then
        AllActions(0).Action.Invoke( _ 
        AllActions(0).FileName)
        'Dopo averla eseguita, la elimina dalla lista
        AllActions.RemoveAt(0)
    End If
End Sub
aaa