Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Threading Problem

Threading Problem

Scheduled Pinned Locked Moved C#
helpdata-structures
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ACorbs
    wrote on last edited by
    #1

    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)
    		{
    
    S 1 Reply Last reply
    0
    • A ACorbs

      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)
      		{
      
      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      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

      A 1 Reply Last reply
      0
      • S S Senthil Kumar

        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

        A Offline
        A Offline
        ACorbs
        wrote on last edited by
        #3

        I guess I thought Join(500) would kill the thread after 500ms. I must have misread that somewhere.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups