OldSchoolHack

Registrieren / Anmelden Deutsch

[VB08] Timer Frage

icon Thema: [VB08] Timer Frage

Anmeldungsdatum: Apr 2010

Beiträge: 168

1. kein Mensch coded mit VB ^^

2. Bau dir eine Class "Timer" und die hat dann ein 2. Thread mit ner schleife und eine variable mit der Timer-Zeit und dem Pointer auf die Func die es am ende aufrufen soll, bzw. den Code den es ausführen soll (delegate)

3. VB SUX


Edit: wenn du lieb fragst kriegst ein beispiel in C#

Hier "Marke EIGENBAU" vor ein paar Jahren

Spoiler

TEXT Code:
  1.  
  2. // < ------------------------------------------------- >
  3. // < XLib.Timer.cs / C#.Net / 18.03.2007 / ZeRoKiLLeR >
  4. // < ------------------------------------------------- >
  5.  
  6. using System;
  7. using System.Threading;
  8.  
  9. namespace XLib
  10. {
  11. public class Timer
  12. {
  13. /// <summary>
  14. /// Duration in ms
  15. /// </summary>
  16. private Double m_Duration;
  17.  
  18. /// <summary>
  19. /// Time left in ms
  20. /// </summary>
  21. private Double m_TimeLeft;
  22.  
  23. /// <summary>
  24. /// Target method/function after timer finished
  25. /// </summary>
  26. private TargetFunction m_Target;
  27.  
  28. /// <summary>
  29. /// Tick thread
  30. /// </summary>
  31. private Thread m_TickThread;
  32.  
  33. /// <summary>
  34. /// State if timer is running
  35. /// </summary>
  36. private Boolean m_Running;
  37.  
  38. /// <summary>
  39. /// Creates a new timer instance
  40. /// </summary>
  41. /// <param name="duration">Duration in ms</param>
  42. /// <param name="target">Target method/function</param>
  43. public Timer(Double duration, TargetFunction target)
  44. {
  45. try
  46. {
  47. // Set values
  48. this.m_Duration = duration;
  49. this.m_TimeLeft = 0;
  50. this.m_Target = target;
  51. this.m_TickThread = null;
  52. this.m_Running = false;
  53. }
  54. catch (Exception exc)
  55. {
  56. // Handle Errors here
  57. }
  58. }
  59.  
  60. /// <summary>
  61. /// Starts the timer
  62. /// </summary>
  63. public void Start()
  64. {
  65. try
  66. {
  67. // If timer is not running
  68. if (!this.m_Running)
  69. {
  70. this.m_TickThread = new Thread(new ThreadStart(this.Tick));
  71. this.m_TickThread.Name = "XLib.Timer | Tick-Thread";
  72. this.m_TickThread.Start();
  73. this.m_Running = true;
  74. }
  75. }
  76. catch (Exception exc)
  77. {
  78. // Handle Errors here
  79. }
  80. }
  81.  
  82. /// <summary>
  83. /// Stopps the timer
  84. /// </summary>
  85. public void Stop()
  86. {
  87. try
  88. {
  89. // If timer is running
  90. if (this.m_Running)
  91. {
  92. // this.m_TickThread.Abort(); -> Throw exception "AbortException" ...
  93.  
  94. this.m_Running = false; // <- FIX
  95.  
  96. }
  97. }
  98. catch (Exception exc)
  99. {
  100. // Handle Errors here
  101. }
  102. }
  103.  
  104. /// <summary>
  105. /// Pause the imer
  106. /// </summary>
  107. public void Pause()
  108. {
  109. try
  110. {
  111. // If timer is running
  112. if (this.m_Running)
  113. {
  114. this.m_Running = false;
  115. }
  116. }
  117. catch (Exception exc)
  118. {
  119. // Handle Errors here
  120. }
  121. }
  122.  
  123. /// <summary>
  124. /// Tick method
  125. /// </summary>
  126. private void Tick()
  127. {
  128. try
  129. {
  130. // Endless loop
  131. while (true)
  132. {
  133. // If timer is running
  134. if (this.m_Running)
  135. {
  136. if (this.m_TimeLeft < this.m_Duration)
  137. {
  138. this.m_TimeLeft += 100;
  139. }
  140. else
  141. {
  142. this.m_Target.Invoke();
  143. this.m_Running = false;
  144. }
  145. }
  146.  
  147. // Pause thread for 100ms
  148. Thread.Sleep(TimeSpan.FromMilliseconds(100));
  149. }
  150. }
  151. catch (Exception exc)
  152. {
  153. // Handle Errors here
  154. }
  155. }
  156.  
  157. public delegate void TargetFunction();
  158. }
  159. }
  160.  

Und wenn du das nicht kapierst dann schau mal bei MSDN -> dotNet Timer. Evtl. kannst den ja gebrauchen

__________________

mfg ZeRoKiLLeR

*Hax? Nur solange man mit Hack besser als ohne ist * :schaden:

Um Links zu sehen, musst du dich registrieren

Let's Rock ! :schaden: