有一文本变化事件如下:
Private Sub OnTextChanged(ByVal sender As Object, ByVal e As EventArgs)
If Not mOnReplace Then
Dim pos As Integer = mControl.SelectionStart + mControl.SelectionLength
Dim args As New IntelliSenseTextChangedEventArgs( _
IntelliSenseHelper.FindPattern(mControl.Text, pos) _
, mControl.PointToScreen(mControl.GetPositionFromCharIndex(pos - 1)))
RaiseEvent TextChanged(Me, args)
End If
End Sub
现在想实现:用Threading.Timer的定时器在文本连续变化的时候,计算时间间隔,假如第二次文本变化的时间在这个范围之内 ,则取消前一次的操作,只执行第二次的运算。
Private Sub OnTextChanged(ByVal sender As Object, ByVal e As EventArgs)
If Not mOnReplace Then
Dim pos As Integer = mControl.SelectionStart + mControl.SelectionLength
Dim args As New IntelliSenseTextChangedEventArgs( _
IntelliSenseHelper.FindPattern(mControl.Text, pos) _
, mControl.PointToScreen(mControl.GetPositionFromCharIndex(pos - 1)))
RaiseEvent TextChanged(Me, args)
End If
End Sub
现在想实现:用Threading.Timer的定时器在文本连续变化的时候,计算时间间隔,假如第二次文本变化的时间在这个范围之内 ,则取消前一次的操作,只执行第二次的运算。
{
public static System.Threading.Timer stateTimer = null;
public static int Count;
protected static void Main()
{
Count= 0;
System.Threading.AutoResetEvent autoEvent = new System.Threading.AutoResetEvent(false);
System.Threading.TimerCallback timerDelegate = new System.Threading.TimerCallback(timerCall);
stateTimer = new System.Threading.Timer(timerDelegate, autoEvent, 0, 1000);
} private void timerCall(object arts)
{
Count++;
}
}
}
MSDN资料: 语法
C#
public Timer(
TimerCallback callback,
Object state,
int dueTime,
int period
)
参数
callback
类型:System.Threading.TimerCallback 一个 TimerCallback 委托,表示要执行的方法。 state
类型:System.Object 一个包含回调方法要使用的信息的对象,或者为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing)。 dueTime
类型:System.Int32 调用 callback 之前延迟的时间量(以毫秒为单位)。指定 Timeout.Infinite 可防止启动计时器。指定零 (0) 可立即启动计时器。 period
类型:System.Int32 调用 callback 的时间间隔(以毫秒为单位)。指定 Timeout.Infinite 可以禁用定期终止。
附上相关代码:
Private Sub OnTextChanged(ByVal sender As Object, ByVal e As EventArgs)
dim mTimer As Threading.Timer
dim Const PERIOD As Integer = 200 '200ms
mTimer = New Threading.Timer(AddressOf TimerProc)
mTimer.Change(0, PERIOD)
End Sub Private Sub TimerProc(ByVal state As Object)
If mControl.InvokeRequired Then
mControl.BeginInvoke(New Action(AddressOf ChangeText))
mTimer.Dispose()
Else
ChangeText()
mTimer.Dispose()
End If
End Sub Private Sub ChangeText()
If Not mOnReplace Then
Dim pos As Integer = mControl.SelectionStart + mControl.SelectionLength
Dim args As New IntelliSenseTextChangedEventArgs( _
IntelliSenseHelper.FindPattern(mControl.Text, pos) _
, mControl.PointToScreen(mControl.GetPositionFromCharIndex(pos - 1)))
RaiseEvent TextChanged(Me, args)
End If
End Sub