For循环:X秒后跳至下一行

时间:2014-01-08 17:52:59

标签: vb.net for-loop terminate

有人可以帮我解决这段代码:

我有一个包含2列的dataGrid:
My grid

我想要做的是使用PStools的Psloggedon cmd给我登录的每个人的名字,并将结果附加到“LOGGED_IN”列,但是如果没有用户登录到PC,会发生什么,该过程需要5分钟才能发布错误消息。

现在,我想要做的是,如果.5秒已经忘记了当前正在查询的行并转到列中的下一行?

这里是我想要关注的vb.net代码:

  Dim RowCount As Integer = datagridView1.RowCount  
  For i = 0 To RowCount - 2
        'PERFORM PSLOGGEDON ROUTINE
        Dim Proc1 As New Process
        Proc1.StartInfo = New ProcessStartInfo("psloggedon")
        Proc1.StartInfo.Arguments = "-l \\" & datagridView1.Rows(i).Cells(0).Value & ""
        Proc1.StartInfo.RedirectStandardOutput = True
        Proc1.StartInfo.UseShellExecute = False
        Proc1.StartInfo.CreateNoWindow = True
        Proc1.Start()

        'INSERT RESULTS IN LOGGEN_IN COLUMN
        datagridView1.Rows(i).Cells(1).Value = Proc1.StandardOutput.ReadToEnd
    Next

有人可以告诉我如何编写代码来完成这项工作吗?

1 个答案:

答案 0 :(得分:3)

使用Process.WaitForExit(int milliseconds)方法。

  

指示Process组件等待指定的毫秒数,以便关联的进程退出。

     

返回值
  键入:System.Boolean

  true如果相关流程已退出;否则,false

如果在给定时间内没有退出,您可以使用Process.Kill来终止进程。

这样的东西
Dim RowCount As Integer = datagridView1.RowCount  
For i = 0 To RowCount - 2
    'PERFORM PSLOGGEDON ROUTINE
    Dim Proc1 As New Process
    Proc1.StartInfo = New ProcessStartInfo("psloggedon")
    Proc1.StartInfo.Arguments = "-l \\" & datagridView1.Rows(i).Cells(0).Value & ""
    Proc1.StartInfo.RedirectStandardOutput = True
    Proc1.StartInfo.UseShellExecute = False
    Proc1.StartInfo.CreateNoWindow = True
    Proc1.Start()

    If Not Proc1.WaitForExit(5000) Then
        Proc1.Kill()
    End If

    'INSERT RESULTS IN LOGGEN_IN COLUMN
    datagridView1.Rows(i).Cells(1).Value = Proc1.StandardOutput.ReadToEnd
Next