Why is this not working?
What I'm trying to do: I need a way to run certain methods on a specific thread, which continues until the end of the program.
My other possible options: As I understand it, a possible way to do this is to implement the queue. In which I could use the methods that I want to run in a specific thread. In a specific thread, I would spin and sleep /monitor.pulse to see if there are delegates waiting to be started in the queue.
My goal: All difficulties should be avoided for creating a delegate queue, maintaining a lock, etc. It seems that a turnkey solution exists in the WPF world called Dispatcher. WPF controls are mainly inherited from DispatcherObject and somehow it all works. What do I need to do to get this job?
using System;
using System.Threading;
using System.Windows.Threading;
namespace ThreadingTrials
{
class Program
{
[STAThread]
static void Main(string[] args)
{
Thread.CurrentThread.Name = "mainThread";
Engine engine = new Engine();
Console.WriteLine("initializing SpecialEngine from {0}", Thread.CurrentThread.Name);
engine.initialize();
engine.doWork();
}
}
class Engine:DispatcherObject
{
private EventWaitHandle InitializationComplete;
private EventWaitHandle newWorkComplete;
public Engine()
{
}
public void initialize()
{
InitializationComplete = new EventWaitHandle(false, EventResetMode.ManualReset);
Thread thread = new Thread(new ParameterizedThreadStart((hwnd)=>
{
InitializeSpecialEngineObject();
while (true) ;
}));
thread.Name = "Special Engine Thread";
thread.SetApartmentState(ApartmentState.STA);
thread.Priority = ThreadPriority.Normal;
thread.Start();
Console.WriteLine("waiting for initialize at {0}", Thread.CurrentThread.Name);
InitializationComplete.WaitOne();
}
private void InitializeSpecialEngineObject()
{
Console.WriteLine("doing initialization at {0}", Thread.CurrentThread.Name);
Thread.Sleep(500);
InitializationComplete.Set();
}
internal void doWork()
{
newWorkComplete = new EventWaitHandle(false, EventResetMode.AutoReset);
Dispatcher.Invoke((SendOrPostCallback)delegate
{
Console.WriteLine("dispatched to {0}", Thread.CurrentThread.Name);
Thread.Sleep(500);
newWorkComplete.Set();
},DispatcherPriority.Background, null);
Dispatcher.Thread.Resume();
Console.WriteLine("waiting for new work to complete at {0}", Thread.CurrentThread.Name);
newWorkComplete.WaitOne();
}
private void doingWork()
{
Console.WriteLine("Doing work in {0}", Thread.CurrentThread.Name);
Thread.Sleep(500);
}
}
}
Thanks for the input. Fair. Actually, it was not enough to make a simple workflow that waits for an event indicating a new task in the void () delegate queue and starts them as they appear. I copied most of the code from a website on the Internet ... Sorry, lost the link. I did this that day and had to edit this post earlier.
using System;
using System.Threading;
using System.Collections.Generic;
class ProducerConsumerQueue : IDisposable
{
EventWaitHandle _wh = new AutoResetEvent(false);
Thread _worker;
readonly object _locker = new object();
Queue<Action> _tasks = new Queue<Action>();
public delegate void Action();
public ProducerConsumerQueue()
{
_worker = new Thread(Work);
_worker.Start();
}
public void EnqueueTask(Action work)
{
lock (_locker) _tasks.Enqueue(work);
_wh.Set();
}
public void Dispose()
{
EnqueueTask(null);
_worker.Join();
_wh.Close();
}
void Work()
{
while (true)
{
Action task = null;
lock (_locker)
if (_tasks.Count > 0)
{
task = _tasks.Dequeue();
if (task == null) return;
}
if (task != null)
{
task.Invoke();
}
else
_wh.WaitOne();
}
}
}
class Program
{
static void Main()
{
using (ProducerConsumerQueue q = new ProducerConsumerQueue())
{
q.EnqueueTask(delegate
{
Console.WriteLine("Performing task: Hello");
Thread.Sleep(1000);
});
for (int i = 0; i < 10; i++) q.EnqueueTask(delegate
{
Console.WriteLine("Performing task: "+ i);
Thread.Sleep(1000);
});
q.EnqueueTask(delegate
{
Console.WriteLine("Performing task: Goodbye!");
Thread.Sleep(1000);
});
}
}
}
schak source
share