【问题标题】:richtextbox for = i multiple functionrichtextbox for = i 多功能
【发布时间】:2020-05-14 23:09:07
【问题描述】:

我正在寻找我的问题的答案,这里我清楚地向你解释情况。

我有一个富文本框,包括 10 行。

第 1 行 线2 第 3 行 第 4 行 第 5 行 .. 第 10 行

我正在这样做;

For i = 0 to richtextbox.Lines.Count = - 1
    Button1_Click;
    Dim getlist as Process = Process.Start("cmd", "commands" + richtextbox.Lines(i))

    getlist.WaitForExit()
Next

我试过了,但是没用;

Dim getlist as Process = Process.Start("cmd", "commands" + richtextbox.Lines(i))
Dim getlist2 as Process = Process.Start("cmd", "commands" + richtextbox.Lines(i + 1))

当我这样做时,它会变成这样;

首先:richtextbox 第 0 行和第 1 行

getlist 开始 line1,getlist2 开始 line2

我希望它是这样的;

获取列表:line0 getlist2: line1

谁先完成,函数开始换行;

例如:

获取列表:line0-line2-line3-line5 getlist2: line1-line4-line6

我怎么能这样做?非常感谢所有答案!

【问题讨论】:

  • 也许这会有所帮助。 docs.microsoft.com/en-us/dotnet/api/…
  • 我没有在你的循环中点击一个按钮。您可以将按钮中的代码移动到它自己的 Sub 并从按钮和循环中调用它吗?

标签: vb.net


【解决方案1】:

为此,您需要使用某种基于线程的解决方案来并行执行。其中最直接的可能是使用基于Task 的异步方法。

这最终会看起来像这样:

Dim T1 = Task.Run(Sub()
                      Dim getlist as Process = Process.Start("cmd", "commands" & richtextbox.Lines(1))
                      getlist.WaitForExit()
                  End Sub)
Dim T2 = Task.Run(Sub()
                      Dim getlist as Process = Process.Start("cmd", "commands" & richtextbox.Lines(2))
                      getlist.WaitForExit()
                  End Sub)

然后,您将能够使用各种方法来等待一项或两项任务完成。如果将包含例程标记为Async,则可以AwaitT1T2。您也可以Await Task.WhenAny 在任一完成后立即继续。

我强烈建议阅读有关基于任务的异步的 MSDN 文档。特别是,您应该阅读“实现基于任务的异步模式”,因为有关交错和节流的部分可能适用于您想要做的事情。不幸的是,在我的在线帮助副本中,这些示例都是用 C# 编写的,但是这些材料应该可以直接翻译成 VB。

【讨论】:

  • 您好,首先感谢您的评论。我得到了Await Task.WhenAny,我把我的Private Sub 变成了Private Async Sub,这样我就可以使用Await,而且我不是一个完美的英语使用者,这就是为什么我不能正确获得MSDN 文档的原因。我真的很抱歉对你说,但是;
  • 哦抱歉,误按了输入 :D 现在是最近的代码For i = 0 To richbox.Lines.Count - 1 Dim T1 = Task.Run(Sub() Dim getlist As Process = Process.Start("cmd", richbox.Lines(0)) getlist.WaitForExit() End Sub) Await Task.WhenAny Next
  • 使用此代码,代码如何理解首先从 0 开始,然后在完成时使用 1 ?因为当我将进程暗淡更改为richbox.Lines(i) 时,使用 `for i = 0` 代码,它会有序地 0-1-2-3 所有行,但我希望它多个例如同时 2-3 个 cmds 或直到我可以多少,但是如果我将3行放入richbox,3行cmd必须同时启动,不应该尝试4(用于cpu使用)感谢您的帮助!
【解决方案2】:

如果我理解您的意图,这是线程的完美应用程序。这行得通 - 我只是写了它,编译它并运行它。要查看它在单独的线程中执行一些简单的任务,一次只运行 2 个线程,直到所有命令都已执行,只需在您的 Richtextbox1 中输入有效的 shell 命令即可。为了测试它,我使用了:

  • 回声 A
  • 回声 B
  • 回声 C
  • 回声 D

注意事项:

  • 我在 CMD 参数中添加了一个 PAUSE,因此您可以清楚地看到发生了什么。
  • 线程有很多细微差别,我无法在此处开始介绍,例如 ApartmentState,但您需要自学一下。
  • 我不是专家,我相信有一些更优雅的方法可以处理其中的一些问题 ?

希望这会有所帮助!

Imports System.Threading

Public Class Form1
    Dim Thread1 As Thread
    Dim Thread2 As Thread
    Dim Thread1cmd As String = ""
    Dim Thread2cmd As String = ""
    Dim Thread1Running As Boolean = False
    Dim Thread2Running As Boolean = False
    Dim LastCmd As Int32 = -1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        LastCmd = -1
        While LastCmd < RichTextBox1.Lines.Count
            If Thread1Running = False And (LastCmd + 1) < RichTextBox1.Lines.Count Then
                LastCmd += 1
                Thread1cmd = RichTextBox1.Lines(LastCmd)
                output.Text += LastCmd.ToString + " Thread 1: " + Thread1cmd + vbCrLf
                Thread1 = New Thread(AddressOf Thread1Helper)
                Thread1.IsBackground = True
                Thread1.Start()
            End If
            If Thread2Running = False And (LastCmd + 1) < RichTextBox1.Lines.Count Then
                LastCmd += 1
                Thread2cmd = RichTextBox1.Lines(LastCmd)
                output.Text += LastCmd.ToString + " Thread 2: " + Thread2cmd + vbCrLf
                Thread2 = New Thread(AddressOf Thread2Helper)
                Thread2.IsBackground = True
                Thread2.Start()
            End If
            'Note - there is debate around the DoEvents that follows, so experiment with it and see what works best for you
            Application.DoEvents()
            'the main thread now sleeps to give helper threads time to do some work:
            System.Threading.Thread.Sleep(200)
        End While
    End Sub
    Sub Thread1Helper()
        Thread1Running = True
        Dim p1 As System.Diagnostics.Process
        Try
            p1 = New System.Diagnostics.Process
            Dim MyCMD As String = Environment.GetFolderPath(Environment.SpecialFolder.System) + "\cmd"
            p1.StartInfo.FileName = MyCMD
            p1.StartInfo.Arguments = " /c " + """" + Thread1cmd + "&&PAUSE" + """"
            'MsgBox(p1.StartInfo.FileName.ToString)
            p1.Start()
            p1.WaitForExit()
            p1.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        Thread1Running = False
    End Sub
    Sub Thread2Helper()
        Thread2Running = True
        Dim p2 As System.Diagnostics.Process
        Try
            p2 = New System.Diagnostics.Process
            Dim MyCMD As String = Environment.GetFolderPath(Environment.SpecialFolder.System) + "\cmd"
            p2.StartInfo.FileName = MyCMD
            p2.StartInfo.Arguments = " /c " + """" + Thread2cmd + "&&PAUSE" + """"
            'MsgBox(p2.StartInfo.FileName.ToString)
            p2.Start()
            p2.WaitForExit()
            p2.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        Thread2Running = False
    End Sub

End Class

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多