Calling a method in a GUI thread from a timer thread

In my application, I use a timer to check for updates in the RSS feed if new items are detected. I bring up a user dialog to inform the user. When I run the scan manually, everything works fine, but when the automatic scan runs in the Elapsed Event timer, the user dialog is not displayed.

First of all, is this a thread issue? (I assume this is due to the fact that both manual and automatic validation use the same code).

When I run automatic validation, should I call a method that starts validation from an event handler with an expired timer?

Is there something I need to do in my dialog class?

Edit: this is a winforms application.

Here is an example of what the code looks like. (Please do not point out syntax errors in this code example, this is just a simple example, not real code).

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 my class, // but shows that the main form can call Update for a manual check. private void Button_Click(object sender, System.EventArgs e) { MyObjectThatDoesWork.Update(); } //The rest of MainForm with boring main form stuff } public class ObjectThatDoesWork { System.Timers.Timer timer; public ObjectThatDoesWork() { timer = new System.Timers.Timer(); timer.Interval = 600000; timer.AutoReset = true; timer.Elapsed += new new System.Timers.ElapsedEventHandler(TimeToWork); timer.Start(); } private void TimeToWork(object sender, System.Timers.ElapsedEventArgs e) { Update(); } public void Update() { //Check for updates and raise an event if new items are found. //The event is consumed by the main form. OnNewItemsFound(this); } public delgate void NewItemsFoundEventHandler(object sender, System.EventArgs e); public event NewItemsFoundEventHandler NewItemsFound; protected void OnNewItemsFound(object sender) { if(NewItemsFound != null) { NewItemsFound(sender, new System.EventArgs()); } } } 

After reading some comments and answers, I think my problem is that I am using System.Timers.Timer not a System.Windows.Forms.Timer .

EDIT:

After switching to the initial testing, Forms.Timer looks good (but there are no new elements, so the user dialog has not been seen). I added some code to output the stream identifier to the file when calling the update method. Using Timers.Timer the thread id was not a GUI thread, but using Forms.Timer the thread id is the same as the GUI.

+7
multithreading c # timer
source share
4 answers

Here you should use Forms.Timer or use other types of timers, serialize calls to the interface with .Invoke()

+1
source share

Is your application a WPF application? If so, you should delegate the work from the background thread to the Dispatcher associated with the user interface thread.

Put some code so you can get the best help and take a look at the Dispatcher class http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.invoke.aspx

0
source share
  private static System.Threading.SynchronizationContext _UI_Context; //call this function once from the UI thread internal static void init_CallOnUIThread() { _UI_Context = System.Threading.SynchronizationContext.Current; } public static void CallOnUIThread(Action action, bool asynchronous = false) { if (!asynchronous) _UI_Context.Send((o) => { action(); }, null); else _UI_Context.Post((o) => { action(); }, null); } 
0
source share

All Articles