using System;
using System.Collections;
using System.Threading;
using OpenNETCF.Windows.Forms;
namespace Mobile.Sync
{
/// <summary>
/// Summary description for SyncWorkItemQueue.
/// </summary>
public class SyncWorkItemQueue
private object _syncRoot;
private System.Collections.Queue _queue;
private bool _run;
public const int QUEUE_THRESHOLD = 50;
public const int CATCH_UP_THRESHOLD = 8;
public SyncWorkItemQueue()
_syncRoot = new object();
_queue = new Queue();
_run = true;
}
/// Default to true
public bool Run
get
lock(_syncRoot)
return _run;
set
_run = value;
ApplicationEx.DoEvents();
/// Get the number of SyncWorkItem s in the queue
public int ItemCount
return _queue.Count;
/// (Synchronized) Add an item to the end of the queue
/// <param name="workItem"></param>
public void AddToQueue(SyncWorkItem workItem)
Console.WriteLine("Adding: " +workItem);
_queue.Enqueue(workItem);
public void StartProcessing()
ThreadStart threadStart = new ThreadStart(StartWork);
Thread workerThread = new Thread(threadStart);
workerThread.Start();
protected void StartWork()
while(Run)
System.Windows.Forms.Application.DoEvents();
if (_queue.Count > 0 )
Console.WriteLine("Processing Item, Count=" + _queue.Count);
SyncWorkItem workItem = (SyncWorkItem)_queue.Dequeue();
try
workItem.Updater.UpdateTable();
catch(Exception ex)
Console.WriteLine(ex);
Console.WriteLine("Error processing work item");
Console.WriteLine(workItem.Updater);
Console.WriteLine("Processed: " + workItem.Updater);
//Allow everything to update
//Now check for greater than threshold amount, to let everything catch up
if (_queue.Count > QUEUE_THRESHOLD)
Console.WriteLine("Catching up");
for(int i = 0; i < CATCH_UP_THRESHOLD; ++i)
Console.WriteLine("CATCH_UP_THRESHOLD item processed, Count=" + _queue.Count);
Console.WriteLine("Ending Startwork");
while (_syncQueue.ItemCount > 0)
Thread.Sleep(10);
_syncQueue.Run = false;