Tag: 计时器

从定时器线程调用GUI线程上的方法

在我的应用程序中,我使用计时器来检查RSS提要中的更新,如果找到新项目,我会弹出一个自定义对话框来通知用户。 当我手动运行检查时,一切都很好,但是当自动检查在计时器Elapsed事件中运行时,不会显示自定义对话框。 首先是这是一个线程问题? (我假设这是因为手动和自动检查都使用相同的代码)。 当我运行自动检查时,是否必须从Timers Elapsed事件处理程序调用运行检查的方法? 我的自定义对话框类中是否需要执行某些操作? 编辑:这是一个winforms应用程序。 以下是代码的示例。 (请不要在此代码示例中指出语法错误,这只是一个简单的示例,而不是真正的代码)。 public class MainForm : System.Windows.Forms.Form { //This is the object that does most of the work. ObjectThatDoesWork MyObjectThatDoesWork = new ObjectThatDoesWork(); MyObjectThatDoesWork.NewItemsFound += new NewItemsFoundEventHandler(Found_New_Items); private void Found_New_Items(object sender, System.EventArgs e) { //Display custom dialog to alert user. } //Method that doesn’t really exist in […]

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

我有一个List 。 每个Timer以可配置的间隔(默认为10分钟)触发。 全部调用相同的回调方法(使用不同的参数)。 回调方法可能需要几秒钟才能完成它的工作。 当程序终止时,看起来回调方法的执行会立即停止(我是否正确看到了?)。 在退出程序之前,如何优雅地等待任何当前正在执行的回调方法?

在WPF中使用计时器刷新UI(使用BackgroundWorker?)

我们在WPF中有一个应用程序,通过ObservableCollection显示数据。 5分钟后,我想刷新数据。 我以为我可以将System.Timers.Timer对象用于其Elapsed事件,然后调用BackgroundWorker来调用启动作业的方法。 该方法位于ViewModel类上。 但似乎线程存在问题。 所以我尝试了Dispatcher,但同样的事情。 这是我的(简化和未优化)代码: /// /// Initializes a new instance of the class. /// public ApplicationController() { CreateDefaultTabs(); Timer timer = new Timer(20000); //20 secs for testing purpose. timer.AutoReset = true; timer.Enabled = true; timer.Elapsed += new ElapsedEventHandler(OnTimeBeforeRefreshElapsed); timer.Start(); } private void OnTimeBeforeRefreshElapsed(object sender, ElapsedEventArgs e) { Dispatcher.CurrentDispatcher.Invoke(new Action(() => { RefreshData(); […]

为什么System.Threading.Timer自行停止?

在Windows服务项目中使用System.Threading.Timer之前,我正在做一个小型测试项目。 它工作得非常好,但计时器会在一两分钟后自行停止。 测试项目的完整来源是: using System; using System.Windows.Forms; using System.Threading; namespace studyTimers { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { TimerCallback timerDelegate = new TimerCallback(tick); System.Threading.Timer testTimer = new System.Threading.Timer(timerDelegate, null, 1000, 1000); } void tick(Object obj) { if (label1.InvokeRequired) { label1.Invoke(new MethodInvoker(() => […]

哪个C#计时器?

我正在写一个包含一个计时器的类(关键时刻可能不会在0处初始化,它可能已经开始),该类将包括启动,暂停,恢复和停止/完成的方法。 我知道我可以使用C#中的一些定时器,即System.Timers.Timer,但是我不确定这个是否允许我以预定义的经过时间的数字启动定时器。 这种情况最好的是什么?

从其他线程调用主线程中的方法

我试图在C#应用程序中同时运行3个级别的计时器,例如: T1将在应用程序的开始运行,然后在其Tick事件中,T2将启动,然后在T2的tick事件上,T3将启动。 最后,在T3的tick事件中,应该在应用程序的主线程中完成某些操作 我的问题似乎是主线程中的代码在被其他线程调用时不起作用 我应该怎么做让主线程通过其他线程的调用来运行它的函数?

System.Timers.Timer是否在独立的线程中运行?

我试图了解System.Timers.Timer何时引发已发生的事件,是否在独立线程中引发? 我的下面的例子似乎暗示三个计时器在他们自己的线程中独立运行: class Program { static System.Timers.Timer timer = new System.Timers.Timer(); static System.Timers.Timer timer2 = new System.Timers.Timer(); static System.Timers.Timer timer3 = new System.Timers.Timer(); static void Main(string[] args) { timer.Elapsed += new System.Timers.ElapsedEventHandler( timer_Elapsed); timer2.Elapsed += new System.Timers.ElapsedEventHandler( timer2_Elapsed); timer3.Elapsed += new System.Timers.ElapsedEventHandler( timer3_Elapsed); timer.Interval = 1000; timer2.Interval = 1000; timer3.Interval = 1000; timer.Start(); timer2.Start(); timer3.Start(); […]

如何在循环等待另一个时间结果时让定时器继续运行?

我的代码中有以下问题,我在定时器上运行这个循环(这只是在大定时器上运行的循环的一小部分), 在那个大计时器内(他每1秒打一次)我有1个方法需要等待5秒然后继续其余的循环代码,但我希望它不会卡住代码并且计时器将继续每1秒运行一次并且不会等待那些5秒。 我做了什么我添加一个新的计时器(timer_deva),每隔5秒打勾并完成其中的所有检查,然后计时器停止。 所以我的问题是我需要等待5秒来检索一个值来完成我的代码,但我需要我的主计时器将继续同时运行,当他得到他的结果时,他将需要完成他离开的代码背后。 提前致谢, else if (mobID.Equals(Convert.ToInt32(txtDeva))) { //START CHECK WITH TIMER timer_deva.Start(); //Methods inside timer_deva update the winnerNation //END CHECK TIMER – GET RESULT winner(zoneId, winnerNation, humansKills, orcKills); }

WPF进度条

在我的WPF应用程序中,我必须在计时器刻度事件中显示进度条进度,我写的如下, System.Windows.Forms.Timer timer; public MainWindow() { timer = new System.Windows.Forms.Timer(); timer.Interval = 1000; this.timer.Tick += new System.EventHandler(this.timer_Tick); } 加载事件如下 private void Window_Loaded(object sender, RoutedEventArgs e) { progressBar1.Minimum = 0; progressBar1.Value = DateTime.Now.Second; progressBar1.Maximum = 700; timer.Start(); } 最后在tick事件中, private void timer_Tick(object sender, EventArgs e) { Duration duration = new Duration(TimeSpan.FromSeconds(20)); //progress bar animation System.Windows.Media.Animation.DoubleAnimation doubleanimation […]

每隔几分钟在global.asax中执行一次方法

每隔x分钟在Global.asax文件中执行方法的最有效方法是什么? 有没有办法让ASP.NET服务器运行一个后台线程,在一段经过的时间后触发一个tick事件?