|
本帖最后由 panhao1 于 2011-3-9 23:34 编辑
本来是grasshopper视屏教程要出的关于委托和多线程的内容,这里辉哥要得急,简单点说下
首先是要声明timer类 这里声明就有两种 先说第一种
直接声明 和C#一样
Private Sub RunScript(ByVal x As Boolean, ByVal y As Object, ByRef A As Object)
'your code goes here…
If s Then
timer = New System.Windows.Forms.Timer
timer.Interval = 100
AddHandler timer.Tick, AddressOf Timer_Tick
timer.Start()
s = False
End If
A = int
End Sub
'<Custom additional code>
Dim timer As System.Windows.Forms.Timer
Dim int As int32 = 0
Dim s As Boolean = True
Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
int += 1
If int > 100 Then timer.Stop():int = 0
owner.ExpireSolution(True)
End Sub
但是不幸的是C#可以直接+=来添加委托
而vb必须要AddHandler关键字
再就是第二种声明
Private Sub RunScript(ByVal x As Object, ByVal y As Object, ByRef A As Object)
If s Then
timer = New System.Windows.Forms.Timer
timer.Interval = 100
timer.Start()
s = False
End If
A = int
End Sub
'<Custom additional code>
Private WithEvents timer As System.Windows.Forms.Timer
Dim int As int32 = 0
Dim s As Boolean = True
Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles timer.tick
int += 1
If int > 100 Then timer.Stop():int = 0
owner.ExpireSolution(True)
End Sub
WithEvents 来声明一个带事件的类 然后用handles来接委托
看不懂没关系 复制代码就可以用
timer会从0跑到100停下来
timer。start和timer。stop可以自己设置条件 |
|