This question already has answers here:
Background timer to update UI?
(4 answers)
Closed 9 years ago.
I need to count down time and the ticks must be in synch with time.
First I tried DispatcherTimer. It interacts fine with UI but it lags.I found a lag of almost 5 seconds after two minutes count down.
Then I switched to System.Timers.Timer.This one seems to be more in synch with real time but if I fire events from it which are caught by UI thread I am getting errors.Also inside Timer Elapsed event handler I can't interact with UI elements either.Being WPF amateur my question is how to use it with UI thread to avoid this sort of anomalies?
You can use Dispatcher.BeginInvoke() method. It will schedule delegate to UI thread. Dispatcher is a property of every control.
control.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => { ... } ));
Related
This question already has answers here:
Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on
(22 answers)
Closed 2 years ago.
I'm a beginner with C# so maybe I don't really understand why I have this error.
System.InvalidOperationException: Cross-thread operation not valid:
Control 'lstb_files_r' accessed from a thread other than the thread it
was created on.
This is what I'm doing:
I drop X csv.
I read the first one (lstb_files_r.SelectedIndex = i;)
after finishing I would like to read the 2nd (i++). But I have this error...
Can someone explain to me how can I solve that please ?
When I asked my question I continued to look all the links and I've found something (maybe it's not the best way but I will find an other solution)
I use delegate
lstb_files_r.Invoke((MethodInvoker)delegate
I put all the code inside and it works.
You are performing the cross threaded opertaion which means you are in one thread (which is your algorithm) and want to perform something in another thread (here GUI is another thread) which is not allowed. For that you have to call GUI thread and perform the function. In C# the easiest way is to use backgraoudworker Use of backgroudworker can see here how to use background worker.
In backgroundworker use progressreport to perform the GUI operations. If you need further help I can help you.
This question already has answers here:
How do I run my c# program as a scheduled task
(10 answers)
Closed 4 years ago.
I have a long-running .exe that I would like executed at 1:30am the morning after a user clicks a button on a webpage. It has two args and will be run infrequently. How do you create a one time scheduled task to run an executable with two args? Thanks!
EDIT: The user clicks a button that enables the .exe to run at 1:30am.
This question is different. I am wanting to know how to CODE the scheduling of a task.
Your can use background scheduler libraries
I would suggest to use Hangifre, its easy to use and can do what you need easily
BackgroundJob.Schedule(
() => your action here,
TimeSpan.FromDays(1));
TimeSpan.FromDays(1) => this you have to calcualte from the time the button is clicked to midnight, and pass the timespan in there, the task will be executed at midnight.
This question already has answers here:
Detecting whether on UI thread in WPF and Winforms
(12 answers)
Closed 4 years ago.
The title says it all. I would like to be able to simply detect if work is being executed on the UI thread. Also, if the method is not running on the UI/main thread, can I determine what other thread that it might be? I would like to use this information for debugging purposes. Thanks!
In a UWP app, you can check and store the value of the Environment.CurrentManagedThreadId property when your app starts and then compare the current value of the same property with the stored value to determine whether you are on the same thread.
Or use the CoreDispatcher.HasThreadAccess property to determine whether the current thread can safely update the UI.
This question already has answers here:
Is it wrong to use the Dispatcher within my ViewModel?
(3 answers)
Closed 6 years ago.
There is a background thread in the viewmodel. It is trying to set a usercontrol property there which is being binded to the view. In simpler terms, accessing a UI thread from a non UI thread. What are the ways to establish this?
Thanks in advance.
in WPF:
Application.Current.Dispatcher.BeginInvoke(
DispatcherPriority.Background, () => { /* UI WORK HERE */ });
This question already has answers here:
What is the difference between ManualResetEvent and AutoResetEvent in .NET?
(11 answers)
Closed 9 years ago.
I read about event, which allow me to wait for other thread: AutoResetEvent and ManualResetEvent.
What are the differences between these two classes? Which class is better for a highly concurrent program?
The difference is in what happens when the event is signaled (set).
the manual-reset event will stay signaled until your explicitly reset it again
the auto-reset event will automatically get reset (unsignaled) once the first thread waiting for it gets awaken
In general I find it easier to work with manual reset events because in most cases it is a bit more straight-forward to determine the state of the event at any given time.
That said there are cases when the behavior of the auto-reset event lends itself better to achieving synchronization because you are guaranteed that only one of the waiting threads will be signaled. So if you have a producer/multiple-consumers scenario where any, but only, one consumer should be signaled you should consider the auto-reset event.