本文整理汇总了C#中Timer.Change方法的典型用法代码示例。如果您正苦于以下问题:C# Timer.Change方法的具体用法?C# Timer.Change怎么用?C# Timer.Change使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Timer
的用法示例。
在下文中一共展示了Timer.Change方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
public static void Main() {
Console.WriteLine("Checking status every 2 seconds");
// Create the Timer ensuring that it never fires. This ensures that
// s_timer refers to it BEFORE Status is invoked by a thread pool thread
s_timer = new Timer(Status, null, Timeout.Infinite, Timeout.Infinite);
// Now that s_timer is assigned to, we can let the timer fire knowing
// that calling Change in Status will not throw a NullReferenceException
s_timer.Change(0, Timeout.Infinite);
Console.ReadLine(); // Prevent the process from terminating
}
示例2: ChangeTest
public async Task ChangeTest()
{
int callCount = 0;
//Run the timer for a second, then change it, and run it for another second to get a total count
//Expect 9 iterations
Timer timer = new Timer((s) => callCount++, null, 100, 100);
await Task.Delay(1000);
//Expect 4 iterations
timer.Change(200, 200);
await Task.Delay(1000);
timer.Dispose();
Assert.AreEqual(13, callCount, "Callcount was incorrect");
}
示例3: ChangeTest2
public async Task ChangeTest2()
{
int callCount = 0;
//Run the timer for a second, then change it, and run it for another second to get a total count
//Expect 9 iterations
Timer timer = new Timer((s) => callCount++, null, 100, 100);
await Task.Delay(1000);
//Expect no more iterations
timer.Change(Timeout.Infinite, Timeout.Infinite);
int lastCallCount = callCount;
await Task.Delay(1000);
timer.Dispose();
Assert.AreEqual(lastCallCount, callCount, "Timer continued to fire after being stopped");
}
示例4: StartNewDelayed
/// <summary>Creates a Task that will complete after the specified delay.</summary>
/// <param name="factory">The TaskFactory.</param>
/// <param name="millisecondsDelay">The delay after which the Task should transition to RanToCompletion.</param>
/// <param name="cancellationToken">The cancellation token that can be used to cancel the timed task.</param>
/// <returns>A Task that will be completed after the specified duration and that's cancelable with the specified token.</returns>
public static Task StartNewDelayed(this TaskFactory factory, int millisecondsDelay, CancellationToken cancellationToken)
{
// Validate arguments
if (factory == null) throw new ArgumentNullException("factory");
if (millisecondsDelay < 0) throw new ArgumentOutOfRangeException("millisecondsDelay");
// Check for a pre-canceled token
if (cancellationToken.IsCancellationRequested)
return factory.FromCancellation(cancellationToken);
// Create the timed task
var tcs = new TaskCompletionSource<object>(factory.CreationOptions);
var ctr = default(CancellationTokenRegistration);
// Create the timer but don't start it yet. If we start it now,
// it might fire before ctr has been set to the right registration.
var timer = new Timer(self =>
{
// Clean up both the cancellation token and the timer, and try to transition to completed
ctr.Dispose();
((Timer)self).Dispose();
tcs.TrySetResult(null);
});
// Register with the cancellation token.
if (cancellationToken.CanBeCanceled)
{
// When cancellation occurs, cancel the timer and try to transition to canceled.
// There could be a race, but it's benign.
ctr = cancellationToken.Register(() =>
{
timer.Dispose();
tcs.TrySetCanceled();
});
}
// Start the timer and hand back the task...
try { timer.Change(millisecondsDelay, Timeout.Infinite); }
catch(ObjectDisposedException) {} // in case there's a race with cancellation; this is benign
return tcs.Task;
}
示例5: ViewChanged
public override void ViewChanged(bool majorChange, BoundingBox extent, double resolution)
{
if (!Enabled) return;
if (DataSource == null) return;
if (!majorChange) return;
NewExtent = extent;
NewResolution = resolution;
if (IsFetching)
{
NeedsUpdate = true;
return;
}
if (StartFetchTimer != null) StartFetchTimer.Dispose();
if (_fetched)
{
StartFetchTimer = new Timer(StartFetchTimerElapsed, null, FetchingPostponedInMilliseconds, int.MaxValue);
}
else
{
StartFetchTimer = new Timer(StartFetchTimerElapsed, null, int.MaxValue, int.MaxValue);
StartFetchTimer.Change(0, int.MaxValue);
}
}
示例6: SessionManagement
private SessionManagement()
{
m_Timer = new Timer(this.TimerProc);
m_Timer.Change(0, TIMER_PERIOD);
}