Update MainWindow on Timer.Elapsed event - c#

I am writing a home WPF app that is obtaining a file from a server at a configured interval.
It's a basic window, with a couple of labels. I have the following
Start Time (reflects DateTime the "Start" event was hit
Duration (reflects the time the app has been running)
Speed (the download speed of the file)
I want to update the Duration on the Main window each second so I have the following code to do this (in a seperate class "RunDownloader.cs").
private void StartTickTimer()
{
const double interval = 1000;
if (_tickTimer == null)
{
_tickTimer = new Timer
{
Interval = interval
};
_tickTimer.Elapsed += _ticktimer_Elapsed;
}
_tickTimer.Start();
}
On _ticktimer_Elapsed I call a method in the main window _mainWindow.UpdateTicker();
This does the following.
public void UpdateTicker()
{
var timeStarted = lblTimeStarted.Content.ToString();
DateTime startTime = DateTime.Parse(timeStarted);
TimeSpan span = DateTime.Now.Subtract(startTime);
//ToDo: Output time taken here!
//lblTimeElapsed.Content =
}
I have two issues.
I have the following exception when calling lblTimeStarted.Content.ToString(); in UpdateTicker()
"The calling thread cannot access this object because a different thread owns it."
I dont quite know, how to show the duration correctly for lblTimeElapsed.Content from TimeSpan
Thanks in advance for any answers. :D

In WPF you cannot update UI objects (that get created on the UI thread) from threads other than the UI thread.
In order to update UI controls from some other thread (eg a timer thread) you need to use the Dispatcher to run your update code on the UI thread.
This Question/answer may help you or you will find plenty of info by googling "WPF Dispatcher".
An example dispatcher call - the lamda code will get posted off to run on the UI thread:
Dispatcher.BeginInvoke(new Action(() =>
{
text_box.AppendText(formated_msg);
text_box.ScrollToEnd();
}));
Alternatively you could replace your existing timer with a DispatchTimer - unlike the timer you are using it ensures that the timer callback is on the UI thread:
Reasons for using a DispatcherTimer opposed to a System.Timers.Timer are that the DispatcherTimer runs on the same thread as the Dispatcher and a DispatcherPriority can be set on the DispatcherTimer.

Your timer is running on its own thread and invoking the UpdateTicker() method from there. However, most UI frameworks, including WPF, prohibit accessing UI controls from threads other than the one which the respective control was created on (the latter is usually denoted as the "UI thread"). You have two main options here:
Use a DispatcherTimer. This will run on your UI thread and avoids any threading issues, but then, since your UpdateTicker() code also runs on this thread, your UI will be unresponsive while you are doing processing. This may or may not be an issue; if all you do is a couple of field/property changes, this is fine.
In your timer callback, use this.Dispatcher.Invoke() or this.Dispatcher.BeginInvoke() to call your UI update method once other processing is completed (example: this.Dispatcher.Invoke((Action) UpdateTicker)). This will "bump" the call to the proper thread for the UI update, while maintaining the asynchrony for data processing. In other words, this is a more effective approach.
The TimeSpan struct has a ToString() method that accepts formatting; or, if this is inconvenient, it has several helper properties (Days, Hours, Minutes, TotalDays, TotalHours, TotalMinutes etc.) that you can use for display purposes.

You can do it like:
In main windows:
public void ChangeTime(string time)
{
lblsb.Content = time;
}
And in RunDownloader:
class RunDownloader
{
Timer _tickTimer;
MainWindow window;
public RunDownloader(MainWindow window)
{
this.window = window;
}
private delegate void MyDel(string str);
public void StartTickTimer()
{
const double interval = 1000;
if (_tickTimer == null)
{
_tickTimer = new Timer
{
Interval = interval
};
_tickTimer.Elapsed += (object sender, ElapsedEventArgs e) =>
{
window.Dispatcher.BeginInvoke(new MyDel(window.ChangeTime), DateTime.Now.ToLongDateString());
};
}
_tickTimer.Start();
}
}

Related

C# Trouble Using Safe Thead or Background Worker

Fairly frustrating since this seems to be well documented and the fact that I accomplished this before, but can't duplicate the same success. Sorry, I'll try to relate it all clearly.
Visual Studio, C# Form, One Main Form has text fields, among other widgets.
At one point we have the concept that we are "running" and therefore gathering data.
For the moment, I started a one second timer so that I can update simulated data into some fields. Eventually that one second timer will take the more rapid data and update it only once per second to the screen, that's the request for the application right now we update at the rate we receive which is a little over 70 Hz, they don't want it that way. In addition some other statistics will be computed and those should be the field updates. Therefore being simple I'm trying to just generate random data and update those fields at the 1 Hz rate. And then expand from that point.
Definition and management of the timer: (this is all within the same class MainScreen)
System.Timers.Timer oneSecondTimer;
public UInt32 run_time = 0;
public int motion = 5;
private void InitializeTimers()
{
this.oneSecondTimer = new System.Timers.Timer(1000);
this.oneSecondTimer.Elapsed += new System.Timers.ElapsedEventHandler(oneSecondTimer_elapsed);
}
public void start_one_second_timer()
{
run_time = 0;
oneSecondTimer.Enabled = true;
}
public void stop_one_second_timer()
{
oneSecondTimer.Enabled = false;
run_time = 0;
}
Random mot = new Random();
private void oneSecondTimer_elapsed(object source, System.Timers.ElapsedEventArgs e)
{
run_time++;
motion = mot.Next(1, 10);
this.oneSecondThread = new Thread(new ThreadStart(this.UpdateTextFields));
this.oneSecondThread.Start();
}
private void UpdateTextFields()
{
this.motionDisplay.Text = this.motion.ToString();
}
motionDisplay is just a textbox in my main form. I get the Invalid Operation Exception pointing me towards the help on how to make Thread-Safe calls. I also tried backgroundworker and end up with the same result. The details are that motionDisplay is accessed from a thread other than the thread it was created on.
So looking for some suggestions as to where my mistakes are.
Best Regards. I continue to iterate on this and will update if I find a solution.
Use a System.Forms.Timer rather than a System.Timers.Timer. It will fire it's elapsed event in the UI thread.
Don't create a new thread to update the UI; just do the update in the elapsed event handler.
Try this
private void UpdateTextFields()
{
this.BeginInvoke(new EventHandler((s,e)=>{
this.motionDisplay.Text = this.motion.ToString();
}));
}
This will properly marshall a call back to the main thread.
The thing with WinForm development is that all the controls are not thread safe. Even getting a property such as .Text from another thread can cause these type of errors to happen. To make it even more frustrating is that sometimes it will work at runtime and you won't get an exception, other times you will.
This is how I do it:
private delegate void UpdateMotionDisplayCallback(string text);
private void UpdateMotionDisplay(string text) {
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.motionDisplay.InvokeRequired) {
UpdateMotionDisplayCallback d = new UpdateMotionDisplayCallback(UpdateMotionDisplay);
this.Invoke(d, new object[] { text });
} else {
this.motionDisplay.Text = text;
}
}
When you want to update the text in motionDisplay just call:
UpdateMotionDisplay(this.motion.ToString())

Get a background task to constantly update a textbox

I am very new to all this so please bear with me! I am writing a small app to control my telescope, at the moment I can connect to it and tell it where to point. I want to have a couple of text boxes, or labels that constantly update with the telescopes position - T is the telescope object and I am calling T.Altitude, T.Azimuth, T.RightAscention and T.Declination and I want these values to update the four labels every half second or so. I assume I need to use a background worker but am I correct? Will I be able to access the Telescope object since it was created on the main thread? And how exactly do I do it all! This is what I have so far (and it aint much!)...
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
string Az = T.Azimuth.ToString();
string Alt = T.Altitude.ToString();
string Dec = T.Declination.ToString();
string Ra = T.RightAscension.ToString();
System.Threading.Thread.Sleep(500);
}
In your case you should consider using one of the Timer classes. Those classes call a given delegate in specified intervals.
The Timer class from Windows.Forms namespace calls a delegate in UI thread, so you will not have to bother with dispatching or anything, but it might make UI less responsive if you call it too often.
Other Timers use separate threads, so you will need to use either Dispatcher object or SynchronizationContext object to modify UI values. You can read more about those on msdn.
The easiest way is probably as suggested to use a Windows.Forms.Timer to periodically update the Gui with current values from your Telescope (object).
As a side note, the Background Worker is kind of obsolete in C# 5.0 since it is much easier to use async/await (see this thread about async/await vs BackgroundWorker).
Here is an example implementation in WinForms which refreshes a set of labels every 500 milliseconds.
public partial class MyForm : Form
{
private readonly Timer _timer = new Timer();
public MyForm()
{
InitializeComponent();
_timer.Interval = 500;
_timer.Tick += TimerTick;
_timer.Enabled = true;
}
void TimerTick(object sender, EventArgs e)
{
_labelAzimuth.Text = T.Azimuth.ToString();
_labelAltitude.Text = T.Altitude.ToString();
_labelDeclination.Text = T.Declination.ToString();
_labelRightAscension.Text = T.RightAscension.ToString();
}
}

How do I update GUI after each iteration?

I have a label and canvas representing a coordinate system. In the canvas there is a node, which should change it's place every time a calculation is made. This is done until a specific stopping criteria is meet.
I also have a label containing coordinate info for the note. I want to be able to see the node and label update on every iteration(every time the calculation is made).
I have looked a the dispatcher, but my GUI will only update when the calculations are done. Below is what I have tried doing with the Dispatcher for the label.
While(notDone = true){
//Calculations made here
Application.Current.Dispatcher.BeginInvoke(
DispatcherPriority.Background,
new Action(() => this.aLabel.Text = aString ));
}
I have been looking at solutions for similar problems and some solutions involve threads. Do I have to use threads to update my GUI?
.Net has a class exactly for you, my friend!
BackgroundWorker
Do all the heavy lifting (your loop) in the DoWork method and update your GUI in ReportProgress.
See the documentation for help.
Just create delegate and event for handling information changes
public delegate void SomeInfoChangeDelegate(object o);
public event SomeInfoChangeDelegate InfoChangeEvent = null;
Assign listener to event and start thread w/ calculations
InfoChangeEvent += new SomeInfoChangeDelegate(OnInfoChanged);
Thread t = new Thread(new ParameterizedThreadStart(SomeWorkerThread));
t.Start();
Make calculation thread and event listener
void OnInfoChanged(object o)
{
// Update label
}
protected void SomeWorkerThread(object o)
{
int i = 0;
while(true)
{
if( i % 10 == 0 )
{
if(InfoChangeEvent != null)
{
InfoChangeEvent(i);
}
}
Thread.Sleep(1000);
i++;
}
}
And don't forget to stop thread :)
If you're performing calculations in UI (main) thread, it can't process messages until they're done. Move calculations to another thread and notify main thread in each iteration.
If you use WPF/Silverlight make class with INotifyPropertyChanged implemented and bind element positions to properties. On RaisePropertyChanged("PropName") position 'll be changed.

How to asynchronously wait for x seconds and execute something then?

I know there is Thread.Sleep and System.Windows.Forms.Timer and Monitor.Wait in C# and Windows Forms. I just can't seem to be able to figure out how to wait for X seconds and then do something else - without locking the thread.
I have a form with a button. On button click a timer shall start and wait for 5 seconds. After these 5 seconds some other control on the form is colored green. When using Thread.Sleep, the whole application would become unresponsive for 5 seconds - so how do I just "do something after 5 seconds"?
(transcribed from Ben as comment)
just use System.Windows.Forms.Timer. Set the timer for 5 seconds, and handle the Tick event. When the event fires, do the thing.
...and disable the timer (IsEnabled=false) before doing your work in oder to suppress a second.
The Tick event may be executed on another thread that cannot modify your gui, you can catch this:
private System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
private void StartAsyncTimedWork()
{
myTimer.Interval = 5000;
myTimer.Tick += new EventHandler(myTimer_Tick);
myTimer.Start();
}
private void myTimer_Tick(object sender, EventArgs e)
{
if (this.InvokeRequired)
{
/* Not on UI thread, reenter there... */
this.BeginInvoke(new EventHandler(myTimer_Tick), sender, e);
}
else
{
lock (myTimer)
{
/* only work when this is no reentry while we are already working */
if (this.myTimer.Enabled)
{
this.myTimer.Stop();
this.doMyDelayedWork();
this.myTimer.Start(); /* optionally restart for periodic work */
}
}
}
}
Just for completeness: with async/await, one can delay execute something very easy (one shot, never repeat the invocation):
private async Task delayedWork()
{
await Task.Delay(5000);
this.doMyDelayedWork();
}
//This could be a button click event handler or the like */
private void StartAsyncTimedWork()
{
Task ignoredAwaitableResult = this.delayedWork();
}
For more, see "async and await" in MSDN.
more completeness:
Depending on your Framework, there is a good chance you will have DispatcherTimer class that can handle the invocation internally (WPF-variants). (finde details in ms docs)
Have you tried
public static Task Delay(
int millisecondsDelay
)
You can use like this:
await Task.Delay(5000);
reference: https://msdn.microsoft.com/en-us/library/hh194873(v=vs.110).aspx
You can start an asynchronous task that performs your action:
Task.Factory.StartNew(()=>
{
Thread.Sleep(5000);
form.Invoke(new Action(()=>DoSomething()));
});
[EDIT]
To pass the interval in you simply have to store it in a variable:
int interval = 5000;
Task.Factory.StartNew(()=>
{
Thread.Sleep(interval);
form.Invoke(new Action(()=>DoSomething()));
});
[/EDIT]
You can wait UI thread the way you want it to work.
Task.Factory.StartNew(async() =>
{
await Task.Delay(2000);
// it only works in WPF
Application.Current.Dispatcher.Invoke(() =>
{
// Do something on the UI thread.
});
});
if you're using .Net Framework 4.5 or higher version, you can use Task.Run instead of Task.Factory.StartNew just like below.
int millisecondsDelay = 2000;
Task.Run(async() =>
{
await Task.Delay(millisecondsDelay);
// it only works in WPF
Application.Current.Dispatcher.Invoke(() =>
{
// Do something on the UI thread.
});
});
You are looking at it wrong.
Click the button, it kicks off a timer with an interval of x seconds. When those are up it's eventhandler executes the task.
So what don't you want to happen.
While the x seconds are elapsing.?
While The task is executing?
If for instance it's you don't want the button to be clicked until delay and task are done. Disable it in the button click handler, and enable it on task completion.
If all you want is a five second delay prior to the task, then you should pass the start delay to the task and let it take care of it.
your application hangs because you are invoking the 5 second sleep/wait on the main UI thread. put the sleep/wait/whatever action in a separate thread (actually System.Windows.Forms.Timer should do that for you) and when it completes invoke the action that turns some control green. remember to check InvokeRequired. here's a short sample (SetText can be called from another thread, if it is the call will instead be invoked on the main UI thread where the textbox is on):
private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = text;
}
}
I took the sample from here (well worth a read!).
#eFloh in the post marked as answer said:
The Tick event may be executed on another thread that cannot modify
your gui, you can catch this ...
That is not what the docs say.
You are using a System.Windows.Forms.Timer in your example code.
That is a Forms.Timer.
According to the C# docs the Timer events are raised on the UI thread.
This Windows timer is designed for a single-threaded environment where
UI threads are used to perform processing. It requires that the user
code have a UI message pump available and always operate from the same
thread ...
Also see stackoverflow post here

WPF / XAML: How do I execute threaded processes and prevent the main UI from being busy / freezing?

I have a XAML application that serves as the UI for an automation. The entire automation can take anywhere from 20-30 hours to fully execute so I created a Task class object that essentially wraps Thread methods (Start/Stop/Reset).
However, when I run the automation method under the Task object, the XAML UI is busy and I cannot interact with the other controls, including the Pause button which toggles the Thread.Set() flag.
There is another post
Prevent UI from freezing without additional threads
where someone recommended the BackgroundWorker class this MSDN article mentions it is a bad idea to use this when if it manipulates objects in the UI, which mine does for purposes of displaying status counts:
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
Any idea around this?
private void OnButtonStartAutomationClick(object sender, RoutedEventArgs e)
{
btnPauseAutomation.IsEnabled = true;
Automation.Task AutomationThread = new Automation.Task(RunFullAutomation);
}
private void RunFullAutomation()
{
// do stuff that can take 20+ hours
// threaded so I can utilize a pause button (block)
}
class Task
{
private ManualResetEvent _shutdownFlag = new ManualResetEvent(false);
private ManualResetEvent _pauseFlag = new ManualResetEvent(true);
private Thread _thread;
private readonly Action _action;
public Task(Action action)
{
_action = action;
}
public void Start()
{
ThreadStart ts = new ThreadStart(DoDelegatedMethod);
_thread = new Thread(ts);
_thread.Start();
_thread.Priority = ThreadPriority.Lowest;
}
public void Resume()
{
_pauseFlag.Set();
}
public void Stop()
{
_shutdownFlag.Set();
_pauseFlag.Set();
_thread.Join();
}
private void DoDelegatedMethod()
{
do
{
_action();
}
while (!_shutdownFlag.WaitOne(0));
}
}
where someone recommended the BackgroundWorker class this MSDN article mentions it is a bad idea to use this when if it manipulates objects in the UI, which mine does for purposes of displaying status counts
BackgroundWorker is actually ideal for this, as it was designed for this type of scenario. The warning is that you shouldn't change UI elements inside of DoWork, but rather via ReportProgress and the ProgressChanged event.
The reason the warning exists is "DoWork" is executed on a background thread. If you set a UI element value from there, you'll get a cross threading exception. However, ReportProgress/ProgressChanged automatically marshals the call back into the proper SynchronizationContext for you.
Take a look at the Dispatcher object in WPF. You can, and should in your scenario, run the long running tasks on a background thread and the BackgroundWorker is a good way to do it. When you need to update the UI you need to verify access to the UI thread and if you don't have it use the dispatcher to invoke an update method on the UI thread.
There are two possible causes here: first, that the blocking task is blocking the UI thread rather than running on a background thread, and second, that the background thread is starving the UI thread so that it never gets the chance to respond to input. You need to find out which of these is the case. A crude way to do this is, in your Click handler, Debug.WriteLine the current thread ID (Thread.CurrentThread.ManagedThreadId), and do the same in the RunFullAutomation callback.
If these print the same number, then you have the first problem. Reed and TheZenker have provided solutions to this.
If these print different numbers, then you are already on a worker thread, and you have the second problem. (BackgroundWorker may get you to the worker thread more elegantly, and will help with updating the UI, but it won't stop starvation.) In this case the simplest fix is probably to set _thread.Priority = ThreadPriority.BelowNormal; before starting the worker thread.
By the way, your code never appears to actually call AutomationThread.Start, which means the RunFullAutomation callback isn't even executed. Is this just a typo?
I'd advise against rolling out your own Task class given that .NET 4 has full support for running tasks asynchronously in the background using the Task Parallel Library
That said,you can do what Reed suggests and use a BackgroundWorker which is ideal or if you prefer more control over the nature of how the task si executing, you could use the Task class from System.Threading.Tasks and implement something like so:
public partial class MainWindow : Window
{
CancellationTokenSource source = new CancellationTokenSource();
SynchronizationContext context = SynchronizationContext.Current;
Task task;
public MainWindow()
{
InitializeComponent();
}
private void DoWork()
{
for (int i = 0; i <= 100; i++)
{
Thread.Sleep(500); //simulate long running task
if (source.IsCancellationRequested)
{
context.Send((_) => labelPrg.Content = "Cancelled!!!", null);
break;
}
context.Send((_) => labelPrg.Content = prg.Value = prg.Value + 1, null);
}
}
private void Start_Click(object sender, RoutedEventArgs e)
{
task = Task.Factory.StartNew(DoWork, source.Token);
}
private void Cancel_Click(object sender, RoutedEventArgs e)
{
source.Cancel();
}
}
In DoWork() you use the WPF SynchronizationContext and post messages to update the UI wiget you need.
The example has a progress bar and a label control that is updated on each iteration of the for loop.Cancellation is supported using CancellationTokenSource which is checked in each iteration.
Hope this helps.

Categories