C# 根據自訂線程定時器 產生隨機訂單

來源:互聯網
上載者:User

標籤:

這個源之於一個朋友問我的一個問題,他說他們的需求是在一天之內隨機抽取資料產生訂單,還不能讓客戶看出來。

隨機產生的訂單還分機率抽取不一定的狀態值,那麼根據我之前寫的定時器線程執行器,我們設計需要一個定時器去執行。

那麼我們的定時器坑定需要一直運行,包括每天的情況。

建立 SecondsTimerTask 由於是測試條件下,我們聲明5秒運行一次

 

 1 /// <summary> 2     /// 每秒執行的任務 3     /// </summary> 4     public class SecondsTimerTask : TimerTaskBase 5     { 6         /// <summary> 7         /// 定義一秒執行一次的 8         /// </summary> 9         public SecondsTimerTask()10             : base(0, 1000, false)11         {12 13         }14 15         List<int> ints1 = new List<int>() { 1, 2, 3, 4, 5 };16         List<int> ints2 = new List<int>() { 6, 7, 8, 9, 10 };17 18         string _ActionDay = string.Empty;19         int _ActionCount = 0;20 21         public override void Run()22         {23             string day = DateTime.Now.ToString("yyyy/MM/dd");24             if (!day.Equals(_ActionDay))25             {26                 //如果是非本日情況,重設條件27                 //可以根據自身的條件完成28                 ints1 = new List<int>() { 1, 2, 3, 4, 5 };29                 ints2 = new List<int>() { 6, 7, 8, 9, 10 };30                 _ActionDay = day;31                 _ActionCount = 0;32             }33             if (_ActionCount == 5)34             {35                 Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss:ffff: ") + "隨機產生訂單 今日任務完成");36                 return;37             }38             int ran = new Random(DateTime.Now.Millisecond).Next(0, 100000);//用0到10萬為隨機界限標準39             if (ran < 70000)//70%的機率這次執行需要生產訂單40             {41                 _ActionCount++;42                 int ranNext = new Random(DateTime.Now.Millisecond).Next(0, 100000);//用0到10萬為隨機界限標準43                 if (ran > 40000)44                 {45                     //60%的機率46                     int index = new Random(DateTime.Now.Millisecond).Next(0, ints1.Count);47                     int item = ints1[index];48                     Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss:ffff: ") + "隨機產生訂單60%的機率: " + item);49                     ints1.RemoveAt(index);50                 }51                 else52                 {53                     ///40%的機率54                     int index = new Random(DateTime.Now.Millisecond).Next(0, ints2.Count);55                     int item = ints2[index];56                     Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss:ffff: ") + "隨機產生訂單40%的機率: " + item);57                     ints2.RemoveAt(index);58                 }59             }60             else61             {62                 Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss:ffff: ") + " 根據隨機情況不產生訂單");63             }64         }65     }

 

 

請結合定時器線程章節,

 1     class Program 2     { 3  4         static void Main(string[] args) 5         { 6             TimerThread timerThread = new TimerThread(); 7             timerThread.AddTask(new SecondsTimerTask()); 8             Console.ReadLine(); 9         }10     }

 

我們來看看結果

上面的運行結果,大家注意時間,我是在今日任務完成的情況下,我直接修改系統時間為第二天,然後繼續第二天的任務

這樣就保證了程式如果一直在啟動並執行情況下,完成每日的產生訂單的量。

但是需要注意的是,我沒有考慮如果程式重啟的情況,需要各位根據各自的情況進行修改~!

C# 根據自訂線程定時器 產生隨機訂單

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.