Click here to Skip to main content
15,886,199 members
Home / Discussions / C#
   

C#

 
GeneralRe: c# Pin
Abhinav S22-Aug-12 20:28
Abhinav S22-Aug-12 20:28 
AnswerOT Pin
Keith Barrow23-Aug-12 3:04
professionalKeith Barrow23-Aug-12 3:04 
QuestionHow do you offload a Dictionary? Pin
Xarzu22-Aug-12 14:36
Xarzu22-Aug-12 14:36 
AnswerRe: How do you offload a Dictionary? Pin
Wes Aday22-Aug-12 14:46
professionalWes Aday22-Aug-12 14:46 
AnswerRe: How do you offload a Dictionary? Pin
Niladri_Biswas22-Aug-12 17:36
Niladri_Biswas22-Aug-12 17:36 
AnswerRe: How do you offload a Dictionary? Pin
OriginalGriff22-Aug-12 20:24
mveOriginalGriff22-Aug-12 20:24 
AnswerRe: How do you offload a Dictionary? Pin
Abhinav S22-Aug-12 20:29
Abhinav S22-Aug-12 20:29 
QuestionTaskFactory AutoResetEvent DeadLock on Window Service Pin
d87c22-Aug-12 14:14
d87c22-Aug-12 14:14 
Hi all C# gurus I need advice on Deadlock

I have a code structure for Window Service, multithreading, task factory like

C#
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 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(() => startTypeB());
							}
							
							#endregion
							
							#region DataFeed Type C
							
							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(() => startTypeC());
							}
							
							#endregion
						}
					}
				}
				if (_TaskScheduler.NumberOfRemainingScheduledTasks == 0)
				{
					_BlockThreadDatafeedSourceTaskScheduler.Set();
				}
			}
			void startTypeA()
			{
				(In this method, it creates a object from another project class which calls a WCF Service and select data and insert into local db and run stored procedures)
			}
			void startTypeB()
			{
				(Same as startTypeA but calls a different WCF and insert into the same set of tables which startTypeA also insert into then also run a same set of stored procedures)
			}
			void startTypeC()
			{
				(Same as startTypeC but calls a different WCF and into different set of tables than startTypeA and startTypeB then run different set of stored procedures)
			}

    ...


I was able to grab all the data from all 3 set of WCF services but the problem is after Type A and Type B insert the data and both calls the same set of stored procedures a few Job Queue had Error 1205, deadlock. Then after service has stopped, I had to manually rerun the stored procedures to process those leftover.

Can anyone know how to fix the deadlock, also I use AutoResetEvent to keep a thread blocked while another thread is processing. It doesn't seem like its doing its job.

I was thinking for alternative way but I would like to keep the same logic, task factory...

My alternative way, non-tested with actual code, but I wrote a draft similar to what I needed, but I am unsure if deadlock will still exists

C#
class Program
    {
        static SemaphoreSlim _sem = new SemaphoreSlim(3);

        static void Main()
        {
            for (int i = 1; i <= 3; i++)
            {
                new Thread(RunThisMethod).Start(i);
            }
            Console.ReadLine();
        }

        static void RunThisMethod(Object id)
        {
            Console.WriteLine(id + " wants to enter");
            _sem.Wait();
            if (id.ToString() == "1")
            {
                Console.WriteLine("Start Type A");
                Thread.Sleep(3000);
                Console.WriteLine("End Type A");
            }
            else if (id.ToString() == "2")
            {
                Console.WriteLine("Start Type B");
                Thread.Sleep(6000);
                Console.WriteLine("End Type B");
            }
            else if (id.ToString() == "3")
            {
                Console.WriteLine("Start Type C");
                Thread.Sleep(9000);
                Console.WriteLine("End Type C");
            }
            _sem.Release();
        }
    }

Questionnecesito un ejemplo que convierta documentos a imagen utilizando c# Pin
david_33322-Aug-12 8:54
david_33322-Aug-12 8:54 
AnswerRe: necesito un ejemplo que convierta documentos a imagen utilizando c# Pin
Eddy Vluggen22-Aug-12 9:05
professionalEddy Vluggen22-Aug-12 9:05 
GeneralRe: necesito un ejemplo que convierta documentos a imagen utilizando c# Pin
OriginalGriff22-Aug-12 20:27
mveOriginalGriff22-Aug-12 20:27 
GeneralRe: necesito un ejemplo que convierta documentos a imagen utilizando c# Pin
Richard MacCutchan22-Aug-12 21:17
mveRichard MacCutchan22-Aug-12 21:17 
GeneralRe: necesito un ejemplo que convierta documentos a imagen utilizando c# Pin
OriginalGriff22-Aug-12 21:19
mveOriginalGriff22-Aug-12 21:19 
QuestionHow to write to Structured file? Pin
Ubun2OS22-Aug-12 7:39
Ubun2OS22-Aug-12 7:39 
AnswerRe: How to write to Structured file? Pin
Eddy Vluggen22-Aug-12 8:53
professionalEddy Vluggen22-Aug-12 8:53 
QuestionC# 2010 working with xml Pin
sc steinhayse22-Aug-12 3:57
sc steinhayse22-Aug-12 3:57 
AnswerRe: C# 2010 working with xml Pin
BobJanova22-Aug-12 4:32
BobJanova22-Aug-12 4:32 
GeneralRe: C# 2010 working with xml Pin
sc steinhayse22-Aug-12 5:57
sc steinhayse22-Aug-12 5:57 
GeneralRe: C# 2010 working with xml Pin
BobJanova22-Aug-12 6:17
BobJanova22-Aug-12 6:17 
GeneralRe: C# 2010 working with xml Pin
sc steinhayse22-Aug-12 16:37
sc steinhayse22-Aug-12 16:37 
GeneralRe: C# 2010 working with xml Pin
BobJanova22-Aug-12 23:44
BobJanova22-Aug-12 23:44 
QuestionHow to avoid flickering in a user control in .Net Compact Framework/C# ? Pin
topksharma198221-Aug-12 21:27
topksharma198221-Aug-12 21:27 
AnswerRe: How to avoid flickering in a user control in .Net Compact Framework/C# ? Pin
BobJanova22-Aug-12 0:43
BobJanova22-Aug-12 0:43 
GeneralRe: How to avoid flickering in a user control in .Net Compact Framework/C# ? Pin
topksharma198222-Aug-12 1:16
topksharma198222-Aug-12 1:16 
GeneralRe: How to avoid flickering in a user control in .Net Compact Framework/C# ? Pin
BobJanova22-Aug-12 2:10
BobJanova22-Aug-12 2:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.