我此刻已經得到的是一個觸發間隔爲5000毫秒計時:創建文件皮卡過程與封閉集合
static Timer _aTimer = new System.Timers.Timer();
static void Main(string[] args)
{
_aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
_aTimer.Interval = 5000;
_aTimer.Enabled = true;
Console.WriteLine("Press \'q\' to quit the sample.");
while (Console.Read() != 'q') ;
}
在火就接着設置隊列用於處理文件:
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
// stop the timer so we dont reprocess files we already have in the queue
StopTimer();
// setup a list of queues
var lists = new List<IncomingOrderQueue>();
//get the accounts in which the files we are looking in
var accounts = new List<string>() { "Account1", "Account2" };
//loop through the accounts and set up the queue
foreach (var acc in accounts)
{
// create the queue
var tmp = new IncomingOrderQueue();
// for each file in the folders add it to be processed in the queue
foreach (var orderFile in OrderFiles(acc))
{
tmp.EnqueueSweep(new QueueVariables() { Account = acc, File = orderFile });
}
// add the queue to the list of queues
lists.Add(tmp);
}
// for each of the queues consume all the contents of them
Parallel.ForEach(lists, l => l.Consume());
//start the timer back up again because we have finished all the files we have in the current queue
StartTimer();
}
public static void StopTimer()
{
Console.WriteLine("Stop Timer");
_aTimer.Stop();
_aTimer.Enabled = false;
}
public static void StartTimer()
{
Console.WriteLine("Start Timer");
_aTimer.Enabled = true;
_aTimer.Start();
}
阻塞隊列自身:
public class IncomingOrderQueue
{
BlockingCollection<QueueVariables> _orderQ = new BlockingCollection<QueueVariables>();
public void EnqueueSweep(QueueVariables incoming)
{
// add items to the queue
_orderQ.Add(incoming);
}
public void Consume()
{
// stop anything been adding to the queue
_orderQ.CompleteAdding();
// consume all the objects in the blocking collection
Parallel.ForEach(_orderQ.GetConsumingEnumerable(), Processor.Order.Object);
}
public int QueueCount
{
get
{
return _orderQ.Count;
}
}
}
我有什麼工作應該如何,啓動定時器 - >停止添呃 - >觸發收集文件夾內所有文件的過程 - >處理所有文件 - >重新啓動計時器。
我不能幫助,但認爲這是一個更好的辦法做即時通訊做特別是當那些即將在帳戶創建隊列數量爲200 - 400
感謝
這是一個很好的解決方案,但是一旦消費者完成了,他們就完全停止。我不熟悉任務的運作方式,我怎麼能夠無限期地踢這個?謝謝 – Houlahan
您可以讓它們繼續運行,直到應用程序關閉,然後調用'_orderQ.CompleteAdding()',這將允許它們終止。或者,如果你創建一個新的'BlockingCollection',你可以再次調用'StartConsumers'。 –
當然感謝您的幫助! – Houlahan