Esempio n. 1
0
            private void FetchTask()
            {
                var tasks = new List <Task>();

                while (Exit == false || TodoCount != 0)
                {
                    Message element = null;
                    lock (TodoLock)
                    {
                        element = Todo.Count > 0 ? Todo[0] : null;
                        if (element != null)
                        {
                            Todo.Remove(element);
                        }
                    }

                    if (element != null)
                    {
                        var cachedElement = _gmailCache.Get(element.Id);
                        if (cachedElement != null)
                        {
                            lock (DoneLock)
                                Done.Add(cachedElement);
                        }
                        else
                        {
                            var request = new UsersResource.MessagesResource.GetRequest(clientService, "me", element.Id);

                            var fetchTask = Task.Run(() =>
                            {
                                var responseMessage = request.Execute();
                                var mess            = new MessageFromItem()
                                {
                                    Id   = responseMessage.Id,
                                    From = responseMessage.Payload.Headers.Single(x => x.Name.Equals("From", StringComparison.OrdinalIgnoreCase)).Value,
                                };
                                lock (DoneLock)
                                {
                                    Done.Add(mess);
                                }

                                _gmailCache.AddItem(mess);
                            });
                            tasks.Add(fetchTask);
                        }
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(100);
                    }
                }

                Task.WhenAll(tasks).Wait();
            }
Esempio n. 2
0
        public void AddItem(MessageFromItem msg)
        {
            var coll = Database.GetCollection <MessageFromItem>(CollectionName);

            coll.Upsert(msg);
        }