for循环完成后,计时器会打勾

时间:2013-09-05 12:20:11

标签: vb.net vb.net-2010

如何在for循环完成时勾选计时器?我不想每隔X分钟运行一次计时器,我想在点击按钮时运行计时器,然后在完成for循环时再次勾选计时器..

 For Each row As DataRow In MTProcessTable.Rows
  Try
            If checkKeyHelp(process.datain) Then
                msg = msgTable.Rows(1)(1).ToString()
                MsgBox(msg)
                writeMsg("1 MO help at ")

            ElseIf checkKeyInfo(process.datain) Then
                msg = msgTable.Rows(4)(1).ToString()
                MsgBox(msg)
                writeMsg("1 MO INFO at ")
            Else

                MOTable = selectMO(process.mo, process.mt)
                moRowNb = MOTable.Rows.Count()
                MO = New MO_class
                If moRowNb <> 0 Then
                    MOrow = MOTable.Rows(0)
                    MO.newuser = MOrow("newuser")
                    MO.sim_id = MOrow("sim_id")
                End If
       Catch ex As Exception
            logFile("executeTimer ----" & ex.Message)
            updateProcessed(process.id, ex.Message)
        Finally
            updateProcessed(process.id, msg)
        End Try
 Next row

Private Sub start_btn_Click(sender As System.Object, e As System.EventArgs) Handles start_btn.Click
    Timer1.Enabled = True
    tm.StartTimer()
End Sub

1 个答案:

答案 0 :(得分:1)

根据您的评论,听起来您想要编写一个程序来选择数据并对其进行处理。完成后,您需要通过选择新数据并处理该数据等来启动该过程。

如果你想使用计时器,我会按如下方式设置。它将使用一些共享项来启动或停止运行sub的计时器。一个问题是,当它运行时,程序将无响应。你可以“停止”程序的唯一时间是在两次运行之间。我将线程之间的时间设置为10秒(10000毫秒),但您可以使用任何值。

Imports System.Windows.Forms.Timer

Public Class Form1


    Private Sub RunProcess()
        'Add code to populate datatable
        For Each row As DataRow In MTProcessTable.Rows
            Try
                If checkKeyHelp(Process.datain) Then
                    msg = msgTable.Rows(1)(1).ToString()
                    MsgBox(msg)
                    writeMsg("1 MO help at ")

                ElseIf checkKeyInfo(Process.datain) Then
                    msg = msgTable.Rows(4)(1).ToString()
                    MsgBox(msg)
                    writeMsg("1 MO INFO at ")
                Else

                    MOTable = selectMO(Process.mo, Process.mt)
                    moRowNb = MOTable.Rows.Count()
                    MO = New MO_class
                    If moRowNb <> 0 Then
                        MOrow = MOTable.Rows(0)
                        MO.newuser = MOrow("newuser")
                        MO.sim_id = MOrow("sim_id")
                    End If
                End If
            Catch ex As Exception
                logFile("executeTimer ----" & ex.Message)
                updateProcessed(Process.Id, ex.Message)
            Finally
                updateProcessed(Process.Id, msg)
            End Try
        Next row

        Timer1.Enabled = True
    End Sub

    Private Timer1 As Timer 'Create timer

    Sub Timer1_Tick() 'Handle timer tick
        Timer1.Enabled = False
        RunProcess()
    End Sub

    Private blRunning As Boolean

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1 = New Timer 'Create instance of timer
        Timer1.Interval = 10000 'Time in MS before starting next process
        AddHandler Timer1.Tick, AddressOf Timer1_Tick 'Add tick handler to timer
        Timer1.Enabled = blRunning 'Enable/disable timer
    End Sub

    Private Sub start_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles start_btn.Click
        If blRunning = True Then
            blRunning = False
            start_btn.Text = "Start"
        Else
            blRunning = True
            start_btn.Text = "Stop"
        End If
        Timer1.Enabled = blRunning
    End Sub
End Class