private async void ProcessQueue() { // Lock this up so that one thread at a time can get through here. // Others will do an async await until it is their turn. await asyncLock.WaitAsync(); try { // Offload this to a background thread (so that the UI is not affected) var queueProcessingTask = Task.Run(() => { var processingStuck = false; while (myQs.Count >= 1 && !processingStuck) { // Get the next item var queueItem = myQs.Peek(); // Try to process this one. (ie DoStuff) App.Current.Dispatcher.Invoke((Action) delegate() { InputBlock2.Text = InputBlock2.Text + Environment.NewLine + "Processing:." + queueItem.number + " " + " Processing this: " + queueItem.comm + " " + DateTime.Now.ToString(); Scroller.ScrollToBottom(); InputBlock2.ScrollToEnd(); }); processingStuck = Send(queueItem.mid, queueItem.message, queueItem.number, queueItem.comm); // If we processed successfully, then we can dequeue the item if (!processingStuck) { myQs.Dequeue(); App.Current.Dispatcher.Invoke((Action) delegate() { InputBlock.Text = InputBlock2.Text + Environment.NewLine + "Removed from queue:." + queueItem.number + " " + " Processing this: " + queueItem.comm + " " + DateTime.Now.ToString(); Scroller.ScrollToBottom(); InputBlock2.ScrollToEnd(); }); } } }); Task.WaitAll(queueProcessingTask); } finally { asyncLock.Release(); } }
public void send() { Console.WriteLine("sending the messsages"); App.Current.Dispatcher.Invoke((Action) delegate() { InputBlock.Text = InputBlock.Text + Environment.NewLine + "sending the messsages" + " " + DateTime.Now.ToString(); }); _networkList = new ObservableCollection <Network>(App.am.Networks); foreach (Network n in _networkList) { if (n.Status == "connected") { Console.WriteLine(" we are sending the messsages to " + n.Names + " Network:"); App.Current.Dispatcher.Invoke((Action) delegate() { InputBlock.Text = InputBlock.Text + Environment.NewLine + " we are sending the messsages to " + n.Names + " Network:" + " " + DateTime.Now.ToString(); Scroller.ScrollToBottom(); InputBlock.ScrollToEnd(); }); _prefixList = new ObservableCollection <Prefix>(App.am.Prefixs); foreach (Prefix p in _prefixList) { if (p.Provider == n.Names) { _messageList = new ObservableCollection <Message>(App.am.Messages); if (_messageList.Where(y => y.Sent == "F").Count() > 0) { Console.WriteLine("sending for :" + n.Names); foreach (Message u in _messageList) { if (u.Sent == "F" && u.Numbers.Contains(p.Pre)) { // myQs.Enqueue(n.Comm,u.Numbers,u.Messages); App.Current.Dispatcher.Invoke((Action) delegate() { InputBlock2.Text = InputBlock2.Text + Environment.NewLine + "Number: " + u.Numbers + " : " + u.Messages + " " + DateTime.Now.ToString(); Scroller.ScrollToBottom(); InputBlock2.ScrollToEnd(); }); try { //Sender(DBObject parent, string id, string message, string number, string network) // if (Messenger.SendUpdate(App.am, u.Id, u.Messages, u.Numbers, n.Names,n.Comm)) //{ if (Messenger.Sender(App.am, u.Id, u.Messages, u.Numbers, n.Names)) { App.Current.Dispatcher.Invoke((Action) delegate() { InputBlock2.Text = InputBlock2.Text + Environment.NewLine + "Message sent:... " + u.Numbers + " " + DateTime.Now.ToString(); Scroller.ScrollToBottom(); InputBlock2.ScrollToEnd(); }); } else { App.Current.Dispatcher.Invoke((Action) delegate() { InputBlock2.Text = InputBlock2.Text + Environment.NewLine + " Message not sent:... " + u.Numbers + " " + DateTime.Now.ToString(); Scroller.ScrollToBottom(); InputBlock2.ScrollToEnd(); }); } Console.WriteLine("Sending process:-number:" + u.Numbers + " : " + u.Messages); } catch { App.Current.Dispatcher.Invoke((Action) delegate() { InputBlock.Text = InputBlock2.Text + Environment.NewLine + "there is an error somewhere....." + DateTime.Now.ToString(); Scroller.ScrollToBottom(); InputBlock2.ScrollToEnd(); }); } } } } } } } } }