TPL Task scheduled to run on UI thread vs UI Thread - task-parallel-library

What is the difference between code running in a task, scheduled to run on synchronization context of the UI thread vs code running directly on a message pump.
I've noticed a number of behavioural differences.

Not sure what you mean by "code running directly on a message pump", but looking at the reference source, you can see that basically
WindowsFormsSynchronizationContext.Send = Control.Invoke
WindowsFormsSynchronizationContext.Post = Control.BeginInvoke
DispatcherSynchronizationContext.Send = Dispatcher.Invoke
DispatcherSynchronizationContext.Post = Dispatcher.BeginInvoke
So when the task scheduler schedules a task on the UI thread it should be equivalent to you doing so

Related

Does Go scheduler also manages non-Go Thread created in runtime?

As far as I know, Go runtime scheduler manages some number of OS Threads(probably more than GOMAXPROCS?) and Go routines by assigning Go routines to OS Threads continuously.
So this basically means that the execution of Go routines, including main goroutine , are managed by both of go scheduler and OS' thread scheudling.
Now here's my questions..
Does the execution of goroutine fully managed by OS' thread scheduling if I call runtime.LockOSThread() at the start of that goroutine?
Does the execution of non-Go thread also fully managed by OS' thread scheduling? In other words, if I create a non-Go Thread by CreateThread function (Windows), then the management non-Go Thread's execution is out of scope of Go's runtime scheduler?
What if I launch another goroutine with go func() in that non-Go Thread? How that non-Go Thread and goroutine's execution is managed?
Currently, I'm writing a program in Golang which runs a windows message loop in main() function of go program.
Most of the time it worked well, but sometimes the message loop get blocked and resumed after few seconds and then large amount of old messages get pumped.
(My another question: Windows Message Loop is getting blocked and resumed intermittently (golang))
I had no idea why it occurs, so I suspected main goroutine's OS Thread switch by go scheduler. So I added runtime.LockOSThread() at the start of main() function to ensure windows message loop always run in the same thread.
However, the problem still occured!
I still have no idea why it occurs, but I'm suspecting this is because of Go scheduler because the same logic written in Python 3.4 didn't make any problems like this.
So what I'm trying now is creating a new Windows Thread (non-Go Thread) by calling CreateThread(...) function, and running windows message loop in that thread.
But I'm curious that whether this approach is different with calling runtime.LockOSThread() in main goroutine running windows message loop from Go runtime scheduler's perspective.
So my question is, 'If I create a new non-Go Thread with CreateThread(...) function and run windows message loop in that thread, then does execution of that thread not affected by Go's runtime scheduler?'
Any helps or ideas will be greatly appreciated.
Thanks.
If you run a new OS thread using the CreateThread() routine, Go's scheduler will not touch the thread. However, you will then have to implement a way for that thread to communicate with Goroutines. You can't, for instance, call a Go method directly from the thread created by CreateThread(). Instead, you will have to use some C-based system to poll events from a Goroutine.
As an aside, if you want to run the loop from the main OS thread, you should call LockOSThread() in init() rather than in main(). See https://github.com/golang/go/wiki/LockOSThread:
func init() {
runtime.LockOSThread();
}
func main() {
// Run loop here.
}

Sleep(suspend) and Resuming windows form starts program on worker thread instead of main thread

The windows form I am working on subscribes to Microsoft.Win32.SystemEvents.PowerModeChanged and on Suspend it runs the Close() method on the form. On Resume it runs the Run() function like it would on initial load. The problem is that when the computer is woken from sleep mode the PowerModeChanged event is triggered on a worker thread named ".Net SystemEvents" and when Run() is called it recreates the form on this worker thread instead of the main thread.
This form is a project I inherited from another developer and I am new to windows form programming. I am wondering if there is a better way to handle the sleep/wake process rather than closing the forms and recreating them on wake or a way to force the creation onto the main thread. Note: If I remove the code and have it do nothing when the computer is slept (suspended) and nothing when it wakes (resumes) then the program continues to work correctly (running on the main thread). Any help will be appreciated. Thanks all.
Capture the UI thread SynchronizationContext in a static field, and Post() on it to go back to the UI thread.

Windows task scheduler not reporting errors?

We have a "service" that is actually nothing more than a C# console app running periodically as a Windows Scheduled Task.
On purpose, we would let the exceptions, if any, bubble up to the main course execution of the program, so that the Windows Task Scheduler could detect this and provide detailed information for the people in charge of monitoring the servers. The application itself is not complex, and does, in fact, make sense to halt all its execution when an error is encountered.
However, we have just found an error because of its side effects of the service not running, and trying to troubleshoot, the people in charge of these servers told me that the only information that they get on this task is its start and end time, no extra information about how did it run.
Is this correct? Or am I supposed to do anything else than just bubble up the exception so that WTS can get detailed information of the task result?

Is it possible to run a quartz.net job on the UI thread?

I'm using Watin for browser testing, which has to run on the UI thread.
I want to schedule a job using Quartz.NET but can't work out a way to run it on the UI thread (using a WPF application)
Any ideas?
I'm not sure how you're running watin but you could:
1. Start the scheduler from inside wherever you are running watin and then connect to it via remoting from the UI thread to schedule the job.
2. Start the scheduler as a windows service and then connect to it via remoting from the UI thread to schedule the job.
3. Write a simple console app that starts the scheduler and exposes it via remoting. Then connect to it from the UI thread to schedule your job.
Take a look at this answer I wrote up earlier with some code samples:
https://stackoverflow.com/questions/1356789/quartz-net-with-asp-net/. Hopefully it will be useful.
I am not familiar with Quartz.NET but the Java version gives ThreadPool interface which can be implemented to make custom threadpool implementations. I don't understand why you would want to run it in a UI thread which is already dedicated to another task.

Is the StateMachine on Windows WorkFlow Thread Safe?

I am planning to use the State Machine WorkFlow of Windows Workflows.
The state machine will be receiving events from two separate threads, the state machine of course will both change its state and execute actions based on its current state and the event that came in.
My question is, is the state machine of windows workflow thread safe, meaning that it will guarantee the correct state change when two threads access it at the same time?
Workflow execution follows single-threaded apartment conventions - that is, one particular instance of a workflow can only be executed by one thread at a time within any runtime. This is by design.
The workflow runtime uses an internal scheduling queue to execute operations for workflow instances, so two threads invoking operations on the same workflow instance will be serialized to the scheduler queue first, then invoked in sequence either by a new thread scheduled by the runtime (default scheduling) or by the thread donated by the calling context for each operation (manual scheduling).
When using the persistence service, the workflow runtime also ensures that the database version is synchronized as well - another workflow runtime running on another process / machine cannot load the same workflow instance from persistence if it is currently open by another workflow runtime.
This means that you don't have to be concerned with thread-safety on code executing within a workflow model (eg you don't have to lock property setters), and you don't have to be concerned with race conditions.
What's your interpretation of this kind of thing in the Microsoft Documentation for (for example) the State Activity CLass in System.Workflow.Activitie:
Thread Safety
Any public static (Shared in Visual
Basic) members of this type are thread
safe. Any instance members are not
guaranteed to be thread safe.
Similar passages are given on many relevent classes. My inference is "no" not thread safe for the usage you're intending.

Resources