C# wpf redrawing canvas depeding on Timer event - c#

I'm trying to redraw a canvas on a Timer event. When the event is fired, the application crashes with an exception.
Picture of code of main procedure
Picture of code handling the timer event

Intead of the Timer class use the DispatcherTimer, It executes the tick on the original thread so it can access the UI

Please try to use the DispatcherTimer instead of the normal timer or you have to invoke to main thread for the hole event handler (OnTimedEvent)
You may use this Link

Related

Windows.Forms.Timers tick and button click at the same time in c#

I have a button and a Windows.Forms.Timers timer1. Both of them have some operations on database. Could couse any problem on Main thread if timer1 tick fired when button click is already operating ?
What is the Main thread response in this case ?
Both of them operates seperately or button click operation blocked by timer1 tick ?
If you aren't implementing any async operations, this cannot happen. When an event is raised on the UI thread, whether a Form Timer or a Button Press, the UI is locked for the duration (as the thread handling the rendering is busy doing something else). In the event where you click a button while a timer_tick is running, the UI won't begin processing the button_clicked event until the timer_tick event returns.
If you are implementing any await/async operations or running any background workers, then things get a little more complicated because the timer_tick event could be awaiting a result from a background thread and begin processing the button_clicked event before that has returned. However, unless you are implementing threading yourself this cannot happen.

Using a timer to call a method of a form control C#, threading issues

I am building a custom control for a few of my forms which is an "indictor light"; an edit control that just changed color.
I would like to set a timer and see what the "light" looks like when it switches from state to state.
I have been reviewing this link C# Elapsed Timer MSDN
Though this does not work for me. I think the issue has to do with the Timer executing on another Thread meaning I cannot interact with the this (this.editControl.[...]) within the OnTimedEvent method.
Is there a simple Timer I could use to just call a method of a forms controls every second or so?
Use the Timer control (the System.Windows.Forms.Timer class). You can find it in the Toolbox when on a designer canvas.
Set the Interval property to 1000 (1 second) and make sure its Enabled. The Tick event fires at every interval and is raised in the UI thread.

Passing parameters to timer event

I am trying to create an animation that moves some images across the canvas in my applications layout.
The canvas is named layout which belongs to the main window and the timer is calling the event Animation.Clouds(layout, 1). When attempting this I am getting an error regarding the object is owned by another thread. This has left me to believe that the cause is the timer not being able to pass the context of layout, causing the error.
How would I solve this issue and pass layout to the timer in order for the animation to work?
use DispatcherTimer instead, it fire a tick in dispatcher thread it is created
The problem is that you can ONLY update a UI element, when you are on the UI thread. The Timer event gets invoke on a different thread.
In Windows.Forms you can use BeginInvoke. I'm sure WPF has something similar.

How do I force and event to resolve from within another event

I have a Winforms App (.NET 3.X) That runs a method in a class to process some data. The method periodically raises a StatusUpdate event with a count of the number of items processed. I have a ToolStripStatuslabel on the Form that i would like to update with the count. The problem is that status label never updates with this count until the process is complete. Below is the code from the status update event handler
toolStripStatusLabel.Text = e.Count.ToString();
statusStrip.Refresh();
I think the problem is that the Refresh event is not firing because the processing method is being called from within a Button press event. I think there is a way to force the Refresh to process but I do not remember what it is.
My only other solution is to execute the processing in it's own thread.
Found the answer in another thread:
Call Application.DoEvents() after setting the label, but you should do all the work in a separate thread instead, so the user may close the window.
This is the command that I was thinking of...
Have you tried calling refresh on the label itself ?
toolStripStatusLabel.Refresh();

WPF Firing the RoutedEvents with a Delay

I am gathering all the routed events fired for the MouseRightDownButton and storing them in a Queue. I have the Sender object as well as the RoutedEventArgs.
Now, I need to fire those events one by one and with a little pause. I also want to update the UI as I fire each event.
Do this require the Timer class?
Yes it will require a timer of some sort. I would check out the DispatcherTimer class if you need to update objects on the UI.

Categories