- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在System.Timers.Timer之后,我创建了一个控制台应用程序来进行练习:
public class Program
{
//private static System.Timers.Timer aTimer;
public static void Main()
{
short_running_method();
Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine();
}
static void short_running_method()
{
// Normally, the timer is declared at the class level,
// so that it stays in scope as long as it is needed.
// If the timer is declared in a long-running method,
// KeepAlive must be used to prevent the JIT compiler
// from allowing aggressive garbage collection to occur
// before the method ends. You can experiment with this
// by commenting out the class-level declaration and
// uncommenting the declaration below; then uncomment
// the GC.KeepAlive(aTimer) at the end of the method.
System.Timers.Timer aTimer;
// Create a timer with a ten second interval.
aTimer = new System.Timers.Timer(10000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// Set the Interval to 2 seconds (2000 milliseconds).
aTimer.Interval = 2000;
aTimer.Enabled = true;
// If the timer is declared in a long-running method, use
// KeepAlive to prevent garbage collection from occurring
// before the method ends.
//GC.KeepAlive(aTimer);
}
// Specify what you want to happen when the Elapsed event is
// raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
}
}
最佳答案
问题出在从事件源到事件监听器的强引用中:
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Stop();
You can also stop timing by setting Enabled to false.
The signal to raise the Elapsed event is always queued for execution on a ThreadPool thread, so the event-handling method might run on one thread at the same time that a call to the Stop method runs on another thread. This might result in the Elapsed event being raised after the Stop method is called. The code example in the next section shows one way to work around this race condition.
关于.net - 多线程环境中的对象生存期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13105908/
我正在使用身份服务器 3 保护 Web 应用程序。我的应用程序分为 2 个 oidc 客户端,一个 ASP.Net MVC 客户端和一个使用 oidc-client javascript 库的 jav
我正在使用 Silex 构建一个小型后台项目,我正在使用 PdoSessionHandler 将 session 存储在数据库中。我已经成功地将 session 存储在数据库中,但似乎将 cookie
我是一名优秀的程序员,十分优秀!