这样的循环如何才能放到另一个线程里,结果在RichTextBox1中显示。
上一个问题问的是列举出指定位数所有的数字和字母组合的问题,在此感谢高手们的解答。
新问题出现了,我想把这个循环的过程放到一个线程里,最后将结果显示到RichTextBox1控件中。在循环的过程中在进度条控件显示进度。
否则循环过程中,窗体处于冻结状态,无法响应操作。循环代码如下:
Dim Letter As String() = {" ", "0", "1", "2", "3", "4", _
"5", "6", "7", "7", "9", "a", _
"b", "c", "d", "e", "f", "i", _
"j", "k", "l", "m", "n", "o", _
"p", "q", "r", "s", "t", "u", _
"v", "w", "x", "y", "z"}
Dim aaa As String
Dim query = From x In Letter From y In Letter _
From z In Letter Where x + y + z <> " " + (x + y + z).Trim() Select (x + y + z).Trim
For Each s In query
aaa = aaa & "|" & s.ToString
Next
Me.RichTextBox1.Text = aaa
"5", "6", "7", "7", "9", "a", _
"b", "c", "d", "e", "f", "i", _
"j", "k", "l", "m", "n", "o", _
"p", "q", "r", "s", "t", "u", _
"v", "w", "x", "y", "z"}
Dim aaa As New System.Text.StringBuilder
Dim query = From x In Letter From y In Letter _
From z In Letter Where x + y + z <> " " + (x + y + z).Trim() Select (x + y + z).Trim
Dim n As Long = query.Count
Dim i As Long = 0
For Each s In query
i += 1
aaa = aaa.Append(s & "
[解决办法]
")
bgWorker.ReportProgress(i / n * 100)
Next
Return aaa.ToString
End Function
顺便指出下,大型循环中采用 aaa = aaa & "
[解决办法]
" & s.ToString 直接导致效率低下,改用Stringbuilder可大幅度提高效率
[解决办法]
不好意思,上述代码中aaa = aaa.Append(s & "
[解决办法]
")应为aaa.Append(s & "
[解决办法]
")
[解决办法]
BackgroundWorker后台采用的还是Threading,如果使用Threading的话,必须自己编写委托实现跨线程的调用。如果仅仅是为了显示进度和返回信息,BackgroundWorker够用了