fork download
  1. using System;
  2. using System.Threading;
  3.  
  4. public class Test
  5. {
  6. private static Random rand = new Random();
  7.  
  8. public static void Main()
  9. {
  10. int Count;
  11. double[] Results = new double[100];
  12. var TimeSec = 3;
  13. var ret = GetData(TimeSec, out Count, ref Results);
  14.  
  15. for (var i = 0; i < Count; i++)
  16. {
  17. Console.WriteLine(string.Format("Result[{0}] = {1}", i, Results[i]));
  18. }
  19. }
  20.  
  21. private static double[] GetRandomData(int maxLength)
  22. {
  23. int length = rand.Next(maxLength) % maxLength;
  24. var randData = new double[length];
  25. for (var i = 0; i < length; i++)
  26. {
  27. randData[i] = rand.Next(100);
  28. }
  29.  
  30. return randData;
  31. }
  32.  
  33. public static int GetData(int Time_s, out int Count, ref double[] Results)
  34. {
  35.  
  36. var lengthIncrement = 100;
  37. Count = 0;
  38.  
  39. try
  40. {
  41. DateTime timeout = DateTime.UtcNow.AddSeconds(Time_s);
  42. double[] values;
  43. while (DateTime.UtcNow < timeout)
  44. {
  45. values = GetRandomData(10);
  46.  
  47. //Before copying over value, make sure we won't overflow
  48. //If we will, extend array
  49. if (Count + values.Length > Results.Length) {
  50. var temp = new double[Results.Length + lengthIncrement];
  51. Array.Copy(Results, temp, Count);
  52. Results = temp;
  53. }
  54.  
  55. Array.Copy(values, 0, Results, Count, values.Length);
  56. Count += values.Length;
  57. Thread.Sleep(500);
  58. }
  59. }
  60. catch (Exception ) { }
  61.  
  62. return 0;
  63. }
  64. }
  65.  
Success #stdin #stdout 0.04s 33944KB
stdin
Standard input is empty
stdout
Result[0] = 10
Result[1] = 18
Result[2] = 77
Result[3] = 90
Result[4] = 29
Result[5] = 35
Result[6] = 89
Result[7] = 16
Result[8] = 96
Result[9] = 72
Result[10] = 86
Result[11] = 96
Result[12] = 58
Result[13] = 35
Result[14] = 84
Result[15] = 65