Threading Problem
-
I am attempting to write a command queue. It is important that each command is executed in order (FIFO). This command queue will be running functions that will have to interact with controls using
Control.Invoke
. The problem I am having has to do with threading and is harder to describe then to show. Code Follows:public struct CommandContainer { public System.Delegate method; public object\[\] parameters; }
CommandQueue.cs -------------------------
using System;
using System.Threading;
using System.Windows.Forms;namespace CommandQueue
{public delegate void CommandFunction(); /// /// Summary description for CommandQueue. /// public class CommandQueue:System.Collections.Queue { private ManualResetEvent m\_ManualResetEvent; public CommandQueue() { //m\_ManualResetEvent = new ManualResetEvent(true); } public override void Clear() { lock(this.SyncRoot) { // TODO: Add CommandQueue.Clear implementation base.Clear (); } } public override object Dequeue() { lock(this.SyncRoot) { // TODO: Add CommandQueue.Dequeue implementation return base.Dequeue (); } } public override object Peek() { lock(this.SyncRoot) { // TODO: Add CommandQueue.Peek implementation return base.Peek (); } } public override void Enqueue(object obj) { lock(this.SyncRoot) { if(obj is CommandContainer) { base.Enqueue (obj); } else { //TODO: Provide error code here } } } public void Enqueue(CommandFunction target) { lock(this.SyncRoot) { CommandContainer t\_CommandContainer; t\_CommandContainer.method = target; t\_CommandContainer.parameters = null; base.Enqueue (t\_CommandContainer); } } public void Enqueue(System.Delegate method, object\[\] parameters) { lock(this.SyncRoot) { CommandContainer t\_CommandContainer; t\_CommandContainer.method = method; t\_CommandContainer.parameters = parameters; base.Enqueue (t\_CommandContainer); } } /// /// Executes all commands in the que /// public void Flush() { Thread t\_FlushThread = new Thread(new ThreadStart(FlushThreadStart)); t\_FlushThread.Name = "Flush Thread"; t\_FlushThread.Start(); t\_FlushThread.Join(); } private void FlushThreadStart() { lock(this.SyncRoot) {
-
I am attempting to write a command queue. It is important that each command is executed in order (FIFO). This command queue will be running functions that will have to interact with controls using
Control.Invoke
. The problem I am having has to do with threading and is harder to describe then to show. Code Follows:public struct CommandContainer { public System.Delegate method; public object\[\] parameters; }
CommandQueue.cs -------------------------
using System;
using System.Threading;
using System.Windows.Forms;namespace CommandQueue
{public delegate void CommandFunction(); /// /// Summary description for CommandQueue. /// public class CommandQueue:System.Collections.Queue { private ManualResetEvent m\_ManualResetEvent; public CommandQueue() { //m\_ManualResetEvent = new ManualResetEvent(true); } public override void Clear() { lock(this.SyncRoot) { // TODO: Add CommandQueue.Clear implementation base.Clear (); } } public override object Dequeue() { lock(this.SyncRoot) { // TODO: Add CommandQueue.Dequeue implementation return base.Dequeue (); } } public override object Peek() { lock(this.SyncRoot) { // TODO: Add CommandQueue.Peek implementation return base.Peek (); } } public override void Enqueue(object obj) { lock(this.SyncRoot) { if(obj is CommandContainer) { base.Enqueue (obj); } else { //TODO: Provide error code here } } } public void Enqueue(CommandFunction target) { lock(this.SyncRoot) { CommandContainer t\_CommandContainer; t\_CommandContainer.method = target; t\_CommandContainer.parameters = null; base.Enqueue (t\_CommandContainer); } } public void Enqueue(System.Delegate method, object\[\] parameters) { lock(this.SyncRoot) { CommandContainer t\_CommandContainer; t\_CommandContainer.method = method; t\_CommandContainer.parameters = parameters; base.Enqueue (t\_CommandContainer); } } /// /// Executes all commands in the que /// public void Flush() { Thread t\_FlushThread = new Thread(new ThreadStart(FlushThreadStart)); t\_FlushThread.Name = "Flush Thread"; t\_FlushThread.Start(); t\_FlushThread.Join(); } private void FlushThreadStart() { lock(this.SyncRoot) {
It simply means that Invoke took more than 500 ms and so the FlushThreadStart continued to execute other functions while Fctn1 was waiting for the Invoke to complete. I don't understand why you are running each command in a separate thread. You said that you want to execute them in the same order, so instead of starting a thread and then waiting for it to complete, why not just execute it synchronously? Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
It simply means that Invoke took more than 500 ms and so the FlushThreadStart continued to execute other functions while Fctn1 was waiting for the Invoke to complete. I don't understand why you are running each command in a separate thread. You said that you want to execute them in the same order, so instead of starting a thread and then waiting for it to complete, why not just execute it synchronously? Regards Senthil _____________________________ My Blog | My Articles | WinMacro