C # generate random orders based on the custom thread timer,

Source: Internet
Author: User

C # generate random orders based on the custom thread timer,

This is because a friend asked me a question. He said that their requirement is to randomly extract data within one day to generate orders, which cannot be seen by the customer.

The randomly generated order also extracts not certain State values based on the probability. According to the timer thread executor I wrote earlier, we need a timer to execute it.

So we need to keep running timer traps, including daily conditions.

Creating SecondsTimerTask is declared to run once every 5 seconds because it is a test condition.

 

1 /// <summary> 2 /// task executed per second 3 /// </summary> 4 public class SecondsTimerTask: timerTaskBase 5 {6 /// <summary> 7 // defines the 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 // if it is not the current day, reset condition 27 // you can complete 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:") + "randomly generated orders completed today"); 36 return; 37} 38 int ran = new Random (DateTime. now. millisecond ). next (0, 100000); // use 0 to 0.1 million as the random boundary Standard 39 if (ran <70000) // 70% probability this execution requires the production order 40 {41 _ ActionCount ++; 42 int ranNext = new Random (DateTime. now. millisecond ). next (0, 100000); // use 0 to 0.1 million as the random boundary Standard 43 if (ran> 40000) 44 {45 // probability of 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% probability of randomly generated orders:" + item); 49 ints1.RemoveAt (index ); 50} 51 else52 {53 // probability of 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% probability of randomly generated orders:" + item); 57 ints2.RemoveAt (index ); 58} 59} 60 else61 {62 Console. writeLine (DateTime. now. toString ("yyyy/MM/dd HH: mm: ss: ffff:") + "do not generate an order based on random conditions"); 63} 64} 65}

 

 

In conjunction with the timer thread section,

 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     }

 

Let's take a look at the results.

In the preceding running result, you should pay attention to the time. When my task is completed today, I directly modify the system time to the next day, and then continue the task for the next day.

In this way, if the program is always running, the daily order generation is completed.

However, it should be noted that if the program restarts, You need to modify it based on your own situation ~!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.