In my Windows Phone 8.1 app I need to create a new Dispatcher
for a Thread
. Usually, we can make it this way:
Dispatcher dispatcher = null;
ManualResetEvent dispatcherReadyEvent = new ManualResetEvent(false);
new Thread(new ThreadStart(() =>
{
dispatcher = Dispatcher.CurrentDispatcher;
dispatcherReadyEvent.Set();
Dispatcher.Run();
})).Start();
dispatcherReadyEvent.WaitOne();
dispatcher.Invoke(...);
The problem is that using Windows Phone 8.1, I haven't access to the Thread
class. I don't understand how I can create a Thread and extract a Dispatcher from it. Thank you in advance.
At a certain point I wanna stop parallelism and I want to make the calls sequential (following their order). I'd like to create a sort of channel. To do so I need a thread called, maybe, ChannelThread. If I had the dispatcher of that thread I can just call the functions on that dispatcher. So, I'll be sure the calls happen sequentially.
First, stop thinking in terms of threads. At this point in software development, threads are an implementation detail. I recommend adopting the highest-level solution available.
Two that come immediately to mind are Reactive Extensions (Rx) and TPL Dataflow. Since there isn't much context in your question, it's difficult to say how easily Rx would integrate with your solution. TPL Dataflow provides a simple FIFO buffer that you can use like this:
var fifo = new ActionBlock<Action>(action => action());
By default, dataflow blocks execute on the thread pool and limit their concurrency to one-at-a-time, so that's all you need.
To queue work to the block:
fifo.Post(() => { /* work */ });