示例#1
0
        public void Producer()
        {
            string dir = string.Format("{0}\\{1}", RandomGeneratorFilePath, "Send");

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            while (true)
            {
                if (queue.Count() > RandomQueueLen)
                {
                    IsFull = true;
                    break;
                }
                string rndCode    = Prefix + (rnd.NextDouble() * int.MaxValue).ToString();
                int    rndCodeLen = rndCode.Length;
                if (rndCodeLen > RandomCodeLen)
                {
                    rndCode = rndCode.Substring(0, RandomCodeLen);
                }
                if (rndCodeLen < RandomCodeLen)
                {
                    rndCode = rndCode.PadRight(RandomCodeLen, char.Parse(rnd.Next(10).ToString()));
                }

                bool     isExistCode = false;
                string   filePath    = string.Format("{0}\\{1}", dir, "" + DateTime.Now.ToString("yyyyMM") + ".txt");
                string[] files       = Directory.GetFiles(RandomGeneratorFilePath);
                if (files != null && files.Length > 0)
                {
                    foreach (string file in files)
                    {
                        IEnumerable <string> codes = File.ReadLines(file);
                        if (codes.Any(s => s == rndCode))
                        {
                            isExistCode = true;
                            break;
                        }
                    }
                }

                if (!isExistCode)
                {
                    var task = Task.Factory.StartNew(() => WriteToFile(filePath, rndCode));
                    task.Wait();
                    queue.Enqueue(rndCode);
                    BLL.RandomGenerator rgBll = new BLL.RandomGenerator();
                    rgBll.Insert(rndCode);

                    break;
                }
            }
        }
示例#2
0
        public string Consumer()
        {
            BLL.RandomGenerator rgBll = new BLL.RandomGenerator();
            string rndCode            = rgBll.ReceiveFromQueue(20);

            string dir = string.Format("{0}\\{1}", RandomGeneratorFilePath, "Receive");

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            string filePath = string.Format("{0}\\{1}", dir, "" + DateTime.Now.ToString("yyyyMMdd") + ".txt");
            var    task     = Task.Factory.StartNew(() => WriteToFile(filePath, rndCode));

            task.Wait();

            return(rndCode);
        }