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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. TaskFactory AutoResetEvent DeadLock on Window Service

TaskFactory AutoResetEvent DeadLock on Window Service

Scheduled Pinned Locked Moved C#
csharpdatabasedata-structures
1 Posts 1 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.
  • D Offline
    D Offline
    d87c
    wrote on last edited by
    #1

    Hi all C# gurus I need advice on Deadlock I have a code structure for Window Service, multithreading, task factory like

    public partial class rs : ServiceBase
    {
    private Queue jobQueue = Queue.Synchronized(new Queue());
    private Queue DatafeedQueue = Queue.Synchronized(new Queue());
    private Dictionary<...> ScheduleJobs = new Dictionary<...>();
    private AutoResetEvent _BlockThreadTaskScheduler = new AutoResetEvent(true);
    private AutoResetEvent _BlockThreadDatafeedSourceTaskScheduler = new AutoResetEvent(false);
    private LimitedConcurrencyLevelTaskScheduler _TaskScheduler;
    private LimitedConcurrencyLevelTaskScheduler _DatafeedSourceTaskScheduler;

        public rs()
        {
    		InitializeComponent();
    		\_timer = new System.Timers.Timer(POLL\_INTERVAL\_MINUTE \* 60 \* 1000);
    		\_timer.Elapsed += new System.Timers.ElapsedEventHandler(\_timer\_Elapsed);
    		\_TaskScheduler = new LimitedConcurrencyLevelTaskScheduler(2);
    		\_DatafeedSourceTaskScheduler = new LimitedConcurrencyLevelTaskScheduler(1);
      
    		protected void \_timer\_Elapsed(object sender, ElapsedEventArgs e)
    		{
    			try
    			{
    				DatafeedQueue.Clear();
    				try
    				{
    					(Execute a stored procedure here)
    					while(xxx.Read())
    					{
    						DatafeedObj dfo = new DatafeedObj();
    						...
    						DatafeedQueue.Enqueue(dfo);
    					}
    				}
    				catch()...
    			
    			TaskFactory DatafeedSourceFactory = new TaskFactory(\_DatafeedSourceTaskScheduler);
    			// DatafeedQueue.Count = 3
    			for (int i = 0; i < DatafeedQueue.Count; i++)
    			{
    				DatafeedSourceFactory.StartNew(() => processDatafeedQueue());
    			}
    
    			\_BlockThreadDatafeedSourceTaskScheduler.Set();
    
    			}
    			catch()...
    		}
    		private void processDatafeedQueue()
    		{
    			\_BlockThreadDatafeedSourceTaskScheduler.WaitOne();
    			try
    			{
    				lock (DatafeedQueue.SyncRoot)
    				{
    					if (DatafeedQueue.Count > 0)
    					{
    						DatafeedObj dfo = ((DatafeedObj)(DatafeedQueue.Dequeue()));
    						
    						#region DataFeed Type A
    						
    						ScheduleJobs = (Calls a static class, static method);
    						jobQueue = (Calls a static class, static method turn ScheduleJobs  dictionary into Queue);
    						var factory = new TaskFactory(\_TaskScheduler);
    						for (int i = 0; i < jobQueue.Count; i++)
    						{
    							factory.StartNew(() => startTypeA());
    						}
    						
    						#endregion
    						  
    						#region DataFeed Type B
    						
    						ScheduleJobs = (Calls a stat
    
    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