zoukankan      html  css  js  c++  java
  • 多线程Demo

      1 Public Class MainForm
      2 
      3     Dim t0 As System.Threading.Thread '监控
      4     Dim t1 As System.Threading.Thread 'immediate
      5     Dim t2 As System.Threading.Thread 'everyday
      6     Dim t3 As System.Threading.Thread 'everyweek
      7 
      8     Dim PermitNextTask As Boolean = True
      9     Dim IsTaskRunning As Boolean = False
     10     Dim FThreadsParameters As ThreadsParameters
     11 
     12     '==================================================================================================================================================
     13 
     14     Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     15         FThreadsParameters = New ThreadsParameters()
     16         CbBoxFreq.SelectedIndex = 0
     17     End Sub
     18 
     19     Private Sub StartBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartBTN.Click
     20 
     21         FThreadsParameters.AddLog(0, "Try Start Program. " & vbCrLf)
     22 
     23         If IsTaskRunning Then
     24             FThreadsParameters.AddLog(0, "Can't Start New Program,old task is still running")
     25             Return
     26         End If
     27 
     28         IsTaskRunning = True
     29 
     30         PermitNextTask = True
     31 
     32         'Terminate current T0 thread
     33         Try
     34             If t0.ThreadState = Threading.ThreadState.Running Then
     35                 Try
     36                     t0.Abort()
     37                 Catch ex As Exception
     38                     FThreadsParameters.AddLog(0, "Error: " & ex.ToString)
     39                 End Try
     40                 t0.Join()
     41             End If
     42         Catch ex As Exception
     43         End Try
     44 
     45         'Start a new T0 thread
     46         t0 = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf StartProgram))
     47         t0.IsBackground = True
     48         t0.Start()
     49 
     50     End Sub
     51 
     52     Private Sub StopBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopBTN.Click
     53         'On Error Resume Next
     54         PermitNextTask = False
     55 
     56         FThreadsParameters.AddLog(0, "Try to Stop the program. ")
     57     End Sub
     58 
     59     Private Sub StartProgram()
     60 
     61         If PermitNextTask Then
     62             'FThreadsParameters.AddLog(0, "▲ PermitNextTask is true ▲: " & PermitNextTask)
     63             'initial-------------------------------------------------------------------------
     64             '[Start T1 Thread]
     65             Dim FSubscribeSender1 As New SubscribeSender(FThreadsParameters)
     66             t1 = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf FSubscribeSender1.Start1))
     67             t1.IsBackground = False
     68             t1.Start()
     69             '[Start T2 Thread]
     70             Dim FSubscribeSender2 As New SubscribeSender(FThreadsParameters)
     71             t2 = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf FSubscribeSender2.Start2))
     72             t2.IsBackground = False
     73             t2.Start()
     74             '[Start T3 Thread]
     75             Dim FSubscribeSender3 As New SubscribeSender(FThreadsParameters)
     76             t3 = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf FSubscribeSender3.Start3))
     77             t3.IsBackground = False
     78             t3.Start()
     79 
     80             Dim WaitedMSeconds1 As Integer = 0
     81             Dim WaitedMSeconds2 As Integer = 0
     82             Dim WaitedMSeconds3 As Integer = 0
     83 
     84             Dim isT1WaitToBeNew As Boolean = False
     85             Dim isT2WaitToBeNew As Boolean = False
     86             Dim isT3WaitToBeNew As Boolean = False
     87 
     88             Do While PermitNextTask
     89 
     90                 If (isT1WaitToBeNew = False) And ((Not t1.IsAlive) Or (WaitedMSeconds1 > 120000)) Then
     91                     '[Terminate T1]
     92                     If t1.ThreadState = Threading.ThreadState.Running Then
     93                         FThreadsParameters.AddLog(0, "▲ Try to terminate T1 thread.")
     94                     End If
     95                     'FThreadsParameters.AddLog(0,"Current T1 Status 0: " & t1.ThreadState.ToString)
     96                     Try
     97                         t1.Abort()
     98                         t1.Join()
     99                     Catch ex As Exception
    100                         FThreadsParameters.AddLog(0, "Failed to Terminate T1 thread. (Error Message: " & ex.ToString & ")")
    101                     End Try
    102                     'FThreadsParameters.AddLog(0, "Current T1 Status: " & t1.ThreadState.ToString)
    103 
    104                     '[Re New T1]
    105                     isT1WaitToBeNew = True
    106                     WaitedMSeconds1 = 0
    107                 End If
    108                 If (isT1WaitToBeNew = True) And (WaitedMSeconds1 > 10000) Then
    109                     t1 = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf FSubscribeSender1.Start1))
    110                     t1.IsBackground = False
    111                     t1.Start()
    112 
    113                     isT1WaitToBeNew = False
    114                     WaitedMSeconds1 = 0
    115                 End If
    116 
    117                 If (isT2WaitToBeNew = False) And ((Not t2.IsAlive) Or (WaitedMSeconds2 > 120000)) Then
    118                     '[Terminate T2]
    119                     If t2.ThreadState = Threading.ThreadState.Running Then
    120                         FThreadsParameters.AddLog(0, "▲ Try to terminate T2 thread.")
    121                     End If
    122                     'FThreadsParameters.AddLog(0,"Current T2 Status 0: " & t2.ThreadState.ToString)
    123                     Try
    124                         t2.Abort()
    125                         t2.Join()
    126                     Catch ex As Exception
    127                         FThreadsParameters.AddLog(0, "Failed to Terminate T2 thread. (Error Message: " & ex.ToString & ")")
    128                     End Try
    129                     'FThreadsParameters.AddLog(0, "Current T2 Status: " & t2.ThreadState.ToString)
    130 
    131                     '[Re New T2]
    132                     isT2WaitToBeNew = True
    133                     WaitedMSeconds2 = 0
    134                 End If
    135                 If (isT2WaitToBeNew = True) And (WaitedMSeconds2 > 10000) Then
    136                     t2 = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf FSubscribeSender2.Start2))
    137                     t2.IsBackground = False
    138                     t2.Start()
    139 
    140                     isT2WaitToBeNew = False
    141                     WaitedMSeconds2 = 0
    142                 End If
    143 
    144                 If (isT3WaitToBeNew = False) And ((Not t3.IsAlive) Or (WaitedMSeconds3 > 120000)) Then
    145                     '[Terminate T3]
    146                     If t3.ThreadState = Threading.ThreadState.Running Then
    147                         FThreadsParameters.AddLog(0, "▲ Try to terminate T3 thread.")
    148                     End If
    149                     'FThreadsParameters.AddLog(0,"Current T3 Status 0: " & t3.ThreadState.ToString)
    150                     Try
    151                         t3.Abort()
    152                         t3.Join()
    153                     Catch ex As Exception
    154                         FThreadsParameters.AddLog(0, "Failed to Terminate T3 thread. (Error Message: " & ex.ToString & ")")
    155                     End Try
    156                     'FThreadsParameters.AddLog(0, "Current T3 Status: " & t3.ThreadState.ToString)
    157 
    158                     '[Re New T3]
    159                     isT3WaitToBeNew = True
    160                     WaitedMSeconds3 = 0
    161                 End If
    162                 If (isT3WaitToBeNew = True) And (WaitedMSeconds3 > 10000) Then
    163                     t3 = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf FSubscribeSender3.Start3))
    164                     t3.IsBackground = False
    165                     t3.Start()
    166 
    167                     isT3WaitToBeNew = False
    168                     WaitedMSeconds3 = 0
    169                 End If
    170 
    171                 '[T0 sleep]
    172                 Application.DoEvents()
    173                 Threading.Thread.Sleep(100)
    174                 WaitedMSeconds1 = WaitedMSeconds1 + 100
    175                 WaitedMSeconds2 = WaitedMSeconds2 + 100
    176                 WaitedMSeconds3 = WaitedMSeconds3 + 100
    177             Loop
    178         End If
    179 
    180         If Not PermitNextTask Then
    181             FThreadsParameters.AddLog(0, "▲ PermitNextTask is false ▲: " & PermitNextTask)
    182 
    183             'Terminate the exists T1 thread
    184             Try
    185                 If t1.ThreadState = Threading.ThreadState.Running Then
    186                     t1.Abort()
    187                     t1.Join()
    188                 End If
    189             Catch ex As Exception
    190             End Try
    191 
    192             'Terminate the exists T2 thread
    193             Try
    194                 If t2.ThreadState = Threading.ThreadState.Running Then
    195                     t2.Abort()
    196                     t2.Join()
    197                 End If
    198             Catch ex As Exception
    199             End Try
    200 
    201             'Terminate the exists T3 thread
    202             Try
    203                 If t3.ThreadState = Threading.ThreadState.Running Then
    204                     t3.Abort()
    205                     t3.Join()
    206                 End If
    207             Catch ex As Exception
    208             End Try
    209 
    210             FThreadsParameters.AddLog(0, "Stop OK. " & vbCrLf)
    211 
    212             IsTaskRunning = False
    213 
    214         End If
    215 
    216     End Sub
    217 
    218     Private Sub LogTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LogTimer.Tick
    219         LogTXT.Text = FThreadsParameters.LogInfos(FThreadsParameters.CurrentLogThreadID)
    220     End Sub
    221 
    222     Private Sub CbBoxFreq_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CbBoxFreq.SelectedIndexChanged
    223         FThreadsParameters.CurrentLogThreadID = CbBoxFreq.SelectedIndex
    224     End Sub
    225 
    226 End Class
  • 相关阅读:
    DateUtil(比较两个日期是否是同一天)
    用过的读写
    小笔记
    日志解析LogParse启动参数配置
    wow经典台词
    Quartz资源收藏
    Quartz Job基本示例
    已知两点坐标,及在从其中一点开始移动的距离,求移动到的坐标
    如何为SUSE配置IP地址,网关和DNS
    详解Linux Initrd
  • 原文地址:https://www.cnblogs.com/pyblogs/p/3501963.html
Copyright © 2011-2022 走看看