Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 4th, 2012  |  syntax: None  |  size: 3.87 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Using a Thread Multiple Times
  2. Thread ask = new Thread (new ThreadStart (MathQuiz.prompt));
  3. ask.Start();
  4. ask.Join(30000);
  5. if (answer == 4)
  6. {
  7.     score = score+1;
  8.     answer = 0;
  9. }
  10. Console.WriteLine();
  11. Console.WriteLine("Press any key to move on to the next question!");
  12. Console.WriteLine();
  13. Console.ReadKey(true);
  14. Console.WriteLine("What is 15 / 3?");
  15. Console.WriteLine();
  16. ask.Start();
  17. ask.Join(30000);
  18. if (answer == 5)
  19. {
  20.     score = score+1;
  21.     answer = 0;
  22. }
  23.        
  24. static void prompt()
  25.   {
  26.     preanswer = (Console.ReadLine());
  27.     if (!decimal.TryParse(preanswer, out answer))
  28.         {
  29.             Console.WriteLine("That wasn't even a number or decimal!");
  30.         }
  31.     else
  32.         {
  33.             answer = decimal.Parse(preanswer);
  34.         }
  35.   }
  36.        
  37. private static AutoResetEvent answered = new AutoResetEvent(false);
  38. private static Func<string, bool> questionCorrect = null;
  39. private static bool? correct;
  40.  
  41. static void Main(string[] args)
  42. {
  43.     int score = 0;
  44.     AskQuestion(ref score,
  45.                 "What is 15 / 3?",
  46.                 TimeSpan.FromSeconds(5),
  47.                 answer =>
  48.                     {
  49.                         decimal value;
  50.                         if (!decimal.TryParse(answer, out value))
  51.                         {
  52.                             Console.WriteLine(
  53.                                 "That was not a valid number");
  54.                             return false;
  55.                         }
  56.  
  57.                         return (value == 15/3);
  58.  
  59.                     });
  60.  
  61.  
  62.     AskQuestion(ref score,
  63.                 "What is 20  * 2 ?",
  64.                 TimeSpan.FromSeconds(5),
  65.                 answer =>
  66.                     {
  67.                         decimal value;
  68.                         if (
  69.                             !decimal.TryParse(answer,
  70.                                               out value))
  71.                         {
  72.                             Console.WriteLine(
  73.                                 "That was not a valid number");
  74.                             return false;
  75.                         }
  76.  
  77.                         return (value == 20*2);
  78.  
  79.                     });
  80.  
  81.  
  82.     Console.WriteLine("Done. Score: {0}", score);
  83.     Console.ReadLine();
  84. }
  85.  
  86. private static void AskQuestion(ref int score, string question, TimeSpan duration, Func<string, bool> validator)
  87. {
  88.     // Setup
  89.     questionCorrect = validator;
  90.     correct = null;
  91.     answered.Reset();
  92.  
  93.     // Ask
  94.     Console.WriteLine(question);
  95.     Thread thread = new Thread(GetQuestion);
  96.     thread.Start();
  97.  
  98.     // Wait
  99.     answered.WaitOne(duration);
  100.     thread.Abort();
  101.     thread.Join();
  102.  
  103.     Console.WriteLine(); // Write empty line, otherwise this overwrites the answer.
  104.  
  105.     // Validate);
  106.     if (correct.HasValue && correct.Value == true)
  107.     {
  108.         score++;
  109.         Console.WriteLine("Correct");
  110.     }
  111.     else if (correct.HasValue)
  112.     {
  113.         Console.WriteLine("Incorrect");
  114.     }
  115.     else
  116.     {
  117.         Console.WriteLine("Timeout");
  118.     }
  119.  
  120. }
  121.  
  122. private static void GetQuestion()
  123. {
  124.     try
  125.     {
  126.         List<char> captured = new List<char>();
  127.         bool answerCaptured = false;
  128.         while (!answerCaptured)
  129.         {
  130.             while (Console.KeyAvailable)
  131.             {
  132.  
  133.                 var key = Console.ReadKey();
  134.                 if (key.KeyChar == 'r' || key.KeyChar == 'n')
  135.                 {
  136.                     answerCaptured = true;
  137.                     break;
  138.                 }
  139.  
  140.                 if (key.KeyChar == 'b' && captured.Count > 0)
  141.                 {
  142.                     captured.RemoveAt(captured.Count - 1);
  143.                 }
  144.                 else
  145.                 {
  146.                     captured.Add(key.KeyChar);
  147.                 }
  148.             }
  149.             Thread.Sleep(50);
  150.         }
  151.         string answer = new string(captured.ToArray());
  152.  
  153.         correct = questionCorrect.Invoke(answer);
  154.         answered.Set();
  155.     }
  156.     catch (ThreadAbortException)
  157.     {
  158.         // will be thrown when the thread times out.
  159.     }
  160. }