jueves, 18 de julio de 2019

lista de procesos

Imports System.IO



Public Class Form1

    Public t As Timer

    Private mtimers() As Timer


    Private sc1 As New Object
    Private sc2 As New Object

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim max As Integer = 2

        ReDim mtimers(max)
        For i As Integer = 0 To max
            t = New Timer

            If i = 0 Then
                t.Interval = 100 * 1
            ElseIf i = 1 Then
                t.Interval = 1000 * 1
            ElseIf i = 2 Then
                t.Interval = 1000 * 30
            End If

            t.Enabled = True
            t.Tag = i + 1
            AddHandler t.Tick, AddressOf Timer_Tick
            mtimers(i) = t
        Next

    End Sub

    Private Sub Timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
        Dim timer As Timer = DirectCast(sender, Timer)
        Select Case timer.Tag
            Case "1"

                Dim th1 As Threading.Thread
                'th1 = New Threading.Thread(AddressOf function1)
                th1 = New Threading.Thread(Sub() Me.function1(7))
                th1.Start()

                'Dim sw As New StreamWriter("test1.txt", True)
                'sw.WriteLine(Now.ToString("HH:mm:ss"))
                'sw.Close()

                'While True
                'End While

                'timer.Stop()
            Case "2"
                'MessageBox.Show("TAG 2")
                Dim th2 As Threading.Thread
                th2 = New Threading.Thread(AddressOf function2)
                th2.Start()


                'Dim sw As New StreamWriter("test2.txt", True)
                'sw.WriteLine(Now.ToString("HH:mm:ss"))
                'sw.Close()


            Case 3
                Dim th3 As Threading.Thread
                th3 = New Threading.Thread(AddressOf function3)
                th3.Start()


        End Select
    End Sub


    Private Sub function1(ByVal id As Integer)

        SyncLock sc1

            'If id = 1 Then
            '    Exit Sub
            'End If

            'While True
            '    Threading.Thread.Sleep(1000)
            'End While


            Dim sw As New StreamWriter("test1.txt", True)
            sw.WriteLine(Now.ToString("HH:mm:ss"))
            sw.Close()

        End SyncLock
    End Sub

    Private Sub function2()
        SyncLock sc1
            Dim sw As New StreamWriter("test1.txt", True)
            'sw.WriteLine(Now.ToString("HH:mm:ss"))
            sw.WriteLine("hola!")
            sw.Close()

        End SyncLock
    End Sub


    Private Sub function3()
        Dim sw As New StreamWriter("test3.txt", True)
        sw.WriteLine(Now.ToString("HH:mm:ss"))
        sw.Close()
    End Sub


    Private Sub btn_cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_cancel.Click
        For Each t As Timer In mtimers
            t.Stop()
        Next
    End Sub

End Class
' https://www.dreamincode.net/forums/topic/140657-how-to-use-the-timer-as-array/
' http://vbcity.com/forums/t/36812.aspx
' https://social.msdn.microsoft.com/Forums/vstudio/en-US/ce7214c3-2116-4cfa-bb70-58ba2997cea7/stop-all-timers-at-once?forum=vbgeneral

No hay comentarios.:

Publicar un comentario