|
|
6#

楼主 |
发表于 2012-7-31 17:43:57
|
只看该作者
现在发现VB.NET可以搞多线程(更重要的是不用去烦那么繁杂的API),所以就用VB。NET做了
可惜微软不给力,还要装那么大一个.NET
VB.NET的效率好像也不怎么高
源码:
- Option Explicit On
- Imports VB = Microsoft.VisualBasic
- Friend Class Form1
- Inherits System.Windows.Forms.Form
- Dim Td As System.Threading.Thread '定义一个线程
- 'Delegate Sub Dg(ByVal v As Int32) '声明一个委托,以后实现检测核心数量并自动生成线程就有用了
- Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
- Me.Label11.Text = "正在计算"
- Me.Label8.Text = ""
- Td = New System.Threading.Thread(AddressOf CPUTest)
- Td.Start()
- End Sub
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Td.Abort() '中止线程
- Me.Label11.Text = "计算被终止"
- Me.Label8.Text = ""
- End Sub
- Sub CPUTest()
- '这里定义了很多变量,但是,一旦线程结束(相当于Sub执行完成),内存便会被释放(因为变量生存期过了)
- Dim cs, jsw, i, StartTime, EndTime As Object
- Dim ii As Short
- jsw = Me.Text1.Text
- cs = CDbl(Me.Text2.Text) * 1000000
- Dim zfc As String
- zfc = "1**6789"
- Dim ranmod As String
- Dim temp4, temp2, temp, temp3, temp5 As Object
- Dim temp1 As Double
- Dim temp6, temp7 As String
- temp6 = cs
- temp6 = temp6 / 100
- temp7 = "0"
- CheckForIllegalCrossThreadCalls = False
- Me.ProgressBar1.Value = 0
- CheckForIllegalCrossThreadCalls = True
- StartTime = VB.Timer()
- For i = 1 To cs
- '简便打注释技巧:选中需要注释的区域,按Ctrl+K,再按一遍Ctrl+C就行了;如果要撤销注释,按Ctrl+K,再按一遍Ctrl+U就行了
- If temp7 = temp6 Then
- CheckForIllegalCrossThreadCalls = False
- Me.ProgressBar1.Value = Me.ProgressBar1.Value + 1
- CheckForIllegalCrossThreadCalls = True
- temp7 = "0"
- End If
- temp7 = temp7 + 1
- Randomize()
- ranmod = "0"
- For ii = 1 To jsw
- ranmod = ranmod & Mid(zfc, 1 + Int(Len(zfc) * Rnd()), 1)
- Next ii
- temp = ranmod ^ 2
- temp1 = System.Math.Sqrt(temp)
- temp2 = System.Math.Log(temp1)
- temp3 = System.Math.Exp(temp2)
- temp4 = System.Math.Atan(temp3)
- temp5 = System.Math.Tan(temp4)
- temp = temp + 1
- temp = temp * ranmod - 5
- Next
- EndTime = VB.Timer()
- CheckForIllegalCrossThreadCalls = False
- Me.Label8.Text = EndTime - StartTime
- Me.Label11.Text = "计算完成"
- CheckForIllegalCrossThreadCalls = True
- End Sub
- ' Sub Label()
- '现在我们用不到这个段,代码就没有
- '以后实现检测核心数量并自动生成线程就有用了
- ' End Sub
- End Class
复制代码
下载:
[ 本帖最后由 2011czmxbb52 于 2012-7-31 17:58 编辑 ] |
|