相关疑难解决方法(0)

System.Timers.Timer与System.Threading.Timer

我最近一直在查看一些可能的计时器,而Threading.TimerTimers.Timer对我来说是必要的(因为它们支持线程池).

我正在制作游戏,我计划使用不同类型的活动,间隔不同等.

哪个最好?

.net timer

541
推荐指数
6
解决办法
28万
查看次数

如何优雅地停止System.Threading.Timer?

我有一个用C#实现的Windows服务,需要经常做一些工作.我已经使用System.Threading.Timer带有回调方法的方法来实现它,该方法负责安排下一个回调.我无法正常停止(即处理)计时器.这是一些简化的代码,您可以在控制台应用程序中运行,以说明我的问题:

const int tickInterval = 1000; // one second

timer = new Timer( state => {
                       // simulate some work that takes ten seconds
                       Thread.Sleep( tickInterval * 10 );

                       // when the work is done, schedule the next callback in one second
                       timer.Change( tickInterval, Timeout.Infinite );
                   },
                   null,
                   tickInterval, // first callback in one second
                   Timeout.Infinite );

// simulate the Windows Service happily running for a while before the user tells it to stop
Thread.Sleep( tickInterval * 3 …
Run Code Online (Sandbox Code Playgroud)

.net c# multithreading

15
推荐指数
1
解决办法
2万
查看次数

在退出程序之前等待System.Threading.Timer回调完成

我有一个List<System.Threading.Timer>.每个Timer以可配置的间隔(默认为10分钟)触发.All调用相同的回调方法(使用不同的参数).回调方法可能需要几秒钟才能完成它的工作.

当程序终止时,看起来回调方法的执行会立即停止(我是否正确看到了?).

在退出程序之前,如何优雅地等待任何当前正在执行的回调方法?

c# multithreading timer

11
推荐指数
2
解决办法
9803
查看次数

如何在回调方法中停止System.Threading.Timer

我怎么能停止System.Threading.Timer它的回叫方法.我引用了MSDN,但找不到任何有用的东西.请帮忙.

c# multithreading timer

9
推荐指数
2
解决办法
3万
查看次数

c#System.Threading.Timer等待处理

我有一个使用Timer的类.这个类实现IDispose.我想在Dispose方法中等待,直到计时器不会再次触发.

我这样实现它:

private void TimerElapsed(object state)
{
    // do not execute the callback if one callback is still executing
    if (Interlocked.Exchange(ref _timerIsExecuting, 1) == 1) 
        return;

    try
    {
        _callback();
    }
    finally
    {
        Interlocked.Exchange(ref _timerIsExecuting, 0);
    }
}

public void Dispose()
{
    if (Interlocked.Exchange(ref _isDisposing, 1) == 1)
        return;

    _timer.Dispose();

    // wait until the callback is not executing anymore, if it was
    while (_timerIsExecuting == 0) 
    { }

    _callback = null;
}
Run Code Online (Sandbox Code Playgroud)

这个实现是否正确?我认为这主要取决于_ timerIsExecuting == 0是一个原子操作的问题.或者我必须使用 …

c# multithreading dispose timer

7
推荐指数
2
解决办法
7262
查看次数

计时器在C#命令行程序中经过两次事件触发

程序启动时,我的计时器'Elapsed'事件会触发两次.'Elapsed'事件处理程序的唯一赋值是'Main'方法.有什么我做错了吗?

//class level clock
public static System.Timers.Timer Clock;

static void Main(string[] args)
    {
       Clock = new System.Timers.Timer();
       Clock.Elapsed += new ElapsedEventHandler(Clock_Elapsed);
       Clock.AutoReset = false;
       Clock.Interval = timerInterval; //this needs to be in milliseconds!
       Clock.Enabled = true;

       //run infinite loop until q is pressed
       while (Console.Read() != 'q')
       {}
    }

static void Clock_Elapsed(object sender, ElapsedEventArgs e)
    {
    Clock.Stop();
    //do some stuff
    Clock.Start();             
     }
Run Code Online (Sandbox Code Playgroud)

更新:

@ fparadis2提供的AutoReset修复了两次射击.基本问题是我的计时器间隔设置为30毫秒而不是30000毫秒(30秒),因此事件是双击.

c# command-line timer c#-4.0

6
推荐指数
1
解决办法
5248
查看次数

我可以停止System.Threading.Timer

我有一个应用程序,它使用计时器偶尔在辅助线程上运行监视任务.其中一些清理任务需要花费大量时间,我希望能够在用户结束程序时中止这些任务(如果可能的话,优雅地).

是否有任何方法可以使用Thread.Abort()以编程方式中止线程,或者我是否必须在代码中添加一个标志以指示线程已完成并在启动的代码中的有害位置检查通过计时器?

c# multithreading timer

6
推荐指数
2
解决办法
1万
查看次数

禁用System.Threading.Timer的最佳方法

我有一个System.Threading.Timer将打开和关闭的.我知道有两种关闭计时器的方法:

  1. 呼叫 Timer.Change(-1,-1)
  2. 处理计时器,并在需要时创建一个新计时器

在资源和绩效方面哪一个更好?打电话Change(-1,-1)给CPU加热器?创建计时器是否昂贵?

.net timer

6
推荐指数
2
解决办法
3944
查看次数

标签 统计

timer ×7

c# ×6

multithreading ×5

.net ×3

c#-4.0 ×1

command-line ×1

dispose ×1