C# 初始化倒计时线程,然后执行操作
我在Silverlight的C#工作 我需要一种基于秒(或其他时间单位)的值初始化倒计时的方法,并且在倒计时结束时,我需要执行一些方法C# 初始化倒计时线程,然后执行操作,c#,silverlight,C#,Silverlight,我在Silverlight的C#工作 我需要一种基于秒(或其他时间单位)的值初始化倒计时的方法,并且在倒计时结束时,我需要执行一些方法 倒计时必须与主应用程序分开。使用System.Timers.Timer对象。订阅经过的事件,然后调用Start using System.Timers; ... some method { ... Timer t = new Timer(duration); t.Elapsed += new ElapsedEventHandler(handler
倒计时必须与主应用程序分开。使用System.Timers.Timer对象。订阅经过的事件,然后调用Start
using System.Timers;
...
some method {
...
Timer t = new Timer(duration);
t.Elapsed += new ElapsedEventHandler(handlerMethod);
t.AutoReset = false;
t.Start();
...
}
void handlerMethod(Object sender, ElapsedEventArgs e)
{
...
}
默认情况下(如上所示),计时器将使用
ThreadPool
触发事件。这意味着handlerMethod将不会与应用程序在同一线程上运行。它可以与线程池中的其他线程竞争,但不能与池外的线程竞争。您可以设置SynchronizingObject来修改此行为。特别是,如果已用事件调用Windows窗体控件中的方法,则必须在创建控件的同一线程上运行,将SynchronizingObject设置为控件将完成此操作。使用System.Timers.Timer对象。订阅经过的事件,然后调用Start
using System.Timers;
...
some method {
...
Timer t = new Timer(duration);
t.Elapsed += new ElapsedEventHandler(handlerMethod);
t.AutoReset = false;
t.Start();
...
}
void handlerMethod(Object sender, ElapsedEventArgs e)
{
...
}
默认情况下(如上所示),计时器将使用
ThreadPool
触发事件。这意味着handlerMethod将不会与应用程序在同一线程上运行。它可以与线程池中的其他线程竞争,但不能与池外的线程竞争。您可以设置SynchronizingObject来修改此行为。特别是,如果已用事件调用Windows窗体控件中的某个方法,则必须在创建该控件的同一线程上运行,将SynchronizingObject设置为该控件将完成此操作。在调用事件处理程序时,不应阻止它们,它们应立即返回。您应该通过计时器、BackgroundWorker或线程(按照优先顺序)来实现这一点
参考:
using System;
using System.Windows.Forms;
class MyForm : Form {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.Run(new MyForm());
}
Timer timer;
MyForm() {
timer = new Timer();
count = 10;
timer.Interval = 1000;
timer.Tick += timer_Tick;
timer.Start();
}
protected override void Dispose(bool disposing) {
if (disposing) {
timer.Dispose();
}
base.Dispose(disposing);
}
int count;
void timer_Tick(object sender, EventArgs e) {
Text = "Wait for " + count + " seconds...";
count--;
if (count == 0)
{
timer.Stop();
}
}
}
调用事件处理程序时,不应阻止它们,它们应立即返回。您应该通过计时器、BackgroundWorker或线程(按照优先顺序)来实现这一点 参考:
using System;
using System.Windows.Forms;
class MyForm : Form {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.Run(new MyForm());
}
Timer timer;
MyForm() {
timer = new Timer();
count = 10;
timer.Interval = 1000;
timer.Tick += timer_Tick;
timer.Start();
}
protected override void Dispose(bool disposing) {
if (disposing) {
timer.Dispose();
}
base.Dispose(disposing);
}
int count;
void timer_Tick(object sender, EventArgs e) {
Text = "Wait for " + count + " seconds...";
count--;
if (count == 0)
{
timer.Stop();
}
}
}
使用任务。延迟
static void SomeMethod()
{
Console.WriteLine("Thread ID = " + Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("performing an action...");
}
static void Main(string[] args)
{
int someValueInSeconds = 5;
Console.WriteLine("Thread ID = " + Thread.CurrentThread.ManagedThreadId);
Task.Delay(TimeSpan.FromSeconds(someValueInSeconds)).ContinueWith(t => SomeMethod());
// Prevents the app from terminating before the task above completes
Console.WriteLine("Countdown launched. Press a key to exit.");
Console.ReadKey();
}
请注意,您唯一关心的代码行是带有Task.Delay
的代码行。我已经包括了所有其他内容,以演示在倒计时之后以及在另一个线程上按照您的要求执行操作
Aviod使用Timer类,新任务。*API提供了与更简单的代码相同的灵活性。使用
Task.Delay
static void SomeMethod()
{
Console.WriteLine("Thread ID = " + Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("performing an action...");
}
static void Main(string[] args)
{
int someValueInSeconds = 5;
Console.WriteLine("Thread ID = " + Thread.CurrentThread.ManagedThreadId);
Task.Delay(TimeSpan.FromSeconds(someValueInSeconds)).ContinueWith(t => SomeMethod());
// Prevents the app from terminating before the task above completes
Console.WriteLine("Countdown launched. Press a key to exit.");
Console.ReadKey();
}
请注意,您唯一关心的代码行是带有Task.Delay
的代码行。我已经包括了所有其他内容,以演示在倒计时之后以及在另一个线程上按照您的要求执行操作
Aviod使用Timer类,新任务。*API提供了同样的灵活性,代码更简单。如果您只想让它启动一次(这似乎是这里的要求),请确保在它第一次启动后停止它。这如何回答“倒计时必须与主应用程序分开”OP中的语句?我假设“与主应用程序分离”是指不同的线程。这很接近,但如何使此方法在单独的线程上运行?请注意,.Net 4.5提供了Task.Delay,这大大简化了代码。请参阅我的答案以了解如何执行此操作。如果您只想让它触发一次(这似乎是此处的要求),请确保在它第一次触发后停止。这如何回答“倒计时必须与主应用程序分开。”OP中的语句?我假设为“与主应用程序分开”它们意味着不同的线程。这很接近,但是我如何让这个方法在一个单独的线程上运行呢?请注意.NET4.5提供了Task.Delay,这大大简化了代码。请参阅我的答案,了解如何执行此操作。带计时器的反向线程可以执行此操作,您正在使用哪一位?初始化线程,然后在该线程上启动计时器。我是否会创建一个新线程,并执行一个新的操作,以赋予包含计时器逻辑的线程?带计时器的反向线程将执行此操作,您正在处理哪一位?嗯,初始化线程,然后在该线程上重新启动计时器。我是否会创建一个新线程并执行一个新操作,以赋予包含计时器逻辑的线程?没有访问Dispatchermer的权限。和System.Timers.Timer没有。tick您使用的是什么版本的os silverlight?silverlight 3、4和5支持最新和最好的my FriendDispatcher。您的项目中是否有对System.Windows.dll的引用(请认为这就是它所在的位置)。另外,添加使用System.Windows.Threading…啊,罗杰,我试图避免包括DLLDont访问Dispatchermer。和System.Timers.Timer没有。tick您使用的是什么版本的os silverlight?silverlight 3、4和5支持最新和最好的my FriendDispatcher。您的项目中是否有对System.Windows.dll的引用(请认为这就是它所在的位置)。另外,添加使用System.Windows.Threading…啊Rodger,我试图避免包含DLLSystem.Timers.Timer没有的内容。tick@jordan.peoples他正在使用System.Windows.Forms.Timer,它确实有一个
勾号事件。我试图避免使用该系统。Windows dllSystem.Timers.Timer没有。tick@jordan.peoples他正在使用System.Windows.Forms.Timer,它确实有一个“勾号”
event.im试图避免使用System.Windows dllIn我的项目中我只有Task.WaitAny和Task.WaitAll在我的项目中我只有Task.WaitAny和Task.WaitAll