Example #1
0
        private IList<IJob> SyncDBWithInMemoryCollection( )
        {
            
            IList<IJob> deleteJobs   = new List<IJob>();
            IList<IJob> currentQueue = new List<IJob>();

            lock( JobQueue )
            {
                foreach( IJob job in JobQueue )
                {
                    currentQueue.Add( job );

                    if( job.CurrentCommand == JobCommand.None )
                        deleteJobs.Add(job);
                }
            }

            using( DatabaseDataContext db = new DatabaseDataContext() )
            {
            	var jobsStatus = new List<uint>{0, 0, 0, 0, 0};
				var jobsTotalProgress = 0d;

                foreach( IJob job in currentQueue )
                {
                	jobsStatus[(int)job.CurrentCommand]++;
					jobsTotalProgress += job.TotalProgress;

                    db.Job_Update(job); // Slows down the Lock, if inside!
                }

            	Trace.Write(
            		string.Format(
						"Thread: {0}\tTotal: {1}({2})\tExecute: {3}\tCommit: {4}\tRollback: {5}\tFinalize: {6}\tNone: {7}\n",
            			Thread.CurrentThread.ManagedThreadId, currentQueue.Count,
											(currentQueue.Count == 0 ? 0 : jobsTotalProgress / currentQueue.Count).ToString("F"),
            								jobsStatus[(int) JobCommand.Execute],
											jobsStatus[(int)JobCommand.Commit],
											jobsStatus[(int)JobCommand.Rollback],
											jobsStatus[(int)JobCommand.Finalize],
											jobsStatus[(int)JobCommand.None]));
            }

            return deleteJobs;
        }