ms在数学中代表什么?
ms 代表毫秒ms毫秒
毫秒是一种较为微小的时间单位,是一秒的千分之一。
再看看别人怎么说的。
timer的具体含义
Timer.Tag 属性
注意:此属性在 .NET Framework 2.0 版中是新增的。
获取或设置一个任意字符串,表示某种类型的用户状态。
命名空间:System.Windows.Forms
程序集:System.Windows.Forms(在 system.windows.forms.dll 中)
Timer.Stop 方法
通过将 Enabled 设置为 false 停止引发 Elapsed 事件。
命名空间:System.Timers
程序集:System(在 system.dll 中)
Visual Basic(声明)
Public Sub Stop
Visual Basic(用法)
Dim instance As Timer
instance.Stop
public void Stop ()
public:
void Stop ()
public void Stop ()
JScript
public function Stop ()
还可以通过将 Enabled 设置为 false 停止计时。
Elapsed 事件在 ThreadPool 线程上引发,因此事件处理方法可以运行在一个线程上,同时 Stop 方法调用运行在另一个线程上。这可能导致在调用 Stop 方法后引发 Elapsed 事件。此主题的代码示例演示了一种防止争用条件的方法。
下面的代码示例演示了一种防止调用 Stop 方法的线程在当前正在执行的 Elapsed 事件结束之前继续进行,以及防止两个 Elapsed 事件同时执行事件处理程序(通常称为可重入性)的方法。
此示例执行 100 次测试运行。每次运行测试时,计时器以 150 毫秒的间隔启动。事件处理程序使用 Thread.Sleep 方法来模拟一个长度在 50 和 200 毫秒之间随机变化的任务。测试方法还启动一个控制线程,此线程等待一秒然后停止计时器。如果此控制线程停止计时器时有一个事件正被处理,则此线程必须等待此事件完成后才能继续进行。
Interlocked.CompareExchange(Int32,Int32,Int32) 方法重写用于避免可重入性和防止控制线程在正在执行的事件结束前继续进行。事件处理程序使用 CompareExchange(Int32,Int32,Int32) 方法将一个控制变量设置为 1,但仅在此变量的当前值为零时才进行此设置。这是一个原子操作。如果返回值为零,则此控制变量已被设置为 1 并且事件处理程序继续进行。如果返回值不为零,则只是丢弃此事件以避免可重入性。(如果有必要执行每一个事件,Monitor 类将是一个同步这些事件的更好方法。)事件处理程序在结束时将控制变量设置回零。此示例记录已执行的事件、由于可重入性而被丢弃的事件以及在调用 Stop 方法后发生的事件的总数。
控制线程使用 CompareExchange(Int32,Int32,Int32) 方法将控制变量设置为 -1(负一),但仅在此变量的当前值为零时才进行此设置。如果原子操作返回非零值,则当前有一个事件正在执行。控制线程等待并重试。此示例记录控制线程等待一个事件完成的次数。
Visual Basic 复制代码
Imports System
Imports System.Timers
Imports System.Threading
Public Module Test
' Change these values to control the behavior of the program.
Private testRuns As Integer = 100
' Times are given in milliseconds:
Private testRunsFor As Integer = 1000
Private timerInterval As Integer = 150
' Qualify the name to avoid confusion with the
' System.Threading.Timer class.
Private WithEvents Timer1 As New System.Timers.Timer
Private rand As New Random()
' This is the synchronization point that prevents events
' from running concurrently, and prevents the main thread
' from executing code after the Stop method until any
' event handlers are done executing.
Private syncPoint As Integer = 0
' Count the number of times the event handler is called,
' is executed, is skipped, or is called after Stop.
Private numEvents As Integer = 0
Private numExecuted As Integer = 0
Private numSkipped As Integer = 0
Private numLate As Integer = 0
' Count the number of times the thread that calls Stop
' has to wait for an Elapsed event to finish.
Private numWaits As Integer = 0
Sub Main()
Timer1.Interval = timerInterval
Console.WriteLine()
For i As Integer = 1 To testRuns
TestRun
Console.Write(vbCr & "Test {0}/{1}", i, testRuns)
Next
Console.WriteLine("{0} test runs completed.", testRuns)
Console.WriteLine("{0} events were raised.", numEvents)
Console.WriteLine("{0} events executed.", numExecuted)
Console.WriteLine("{0} events were skipped for concurrency.", numSkipped)
Console.WriteLine("{0} events were skipped because they were late.", numLate)
Console.WriteLine("Control thread waited {0} times for an event to complete.", numWaits)
End Sub
Sub TestRun()
' Set syncPoint to zero before starting the test
' run.
syncPoint = 0
Timer1.Enabled = True
' Start the control thread that shuts off the timer.
Dim t As New Thread(AddressOf ControlThreadProc)
t.Start()
' Wait until the control thread is done before proceeding.
' This keeps the test runs from overlapping.
t.Join()
End Sub
Private Sub ControlThreadProc()
' Allow the timer to run for a period of time, and then
' stop it.
Thread.Sleep(testRunsFor)
Timer1.Stop
' The 'counted' flag ensures that if this thread has
' to wait for an event to finish, the wait only gets
' counted once.
Dim counted As Boolean = False
' Ensure that if an event is currently executing,
' no further processing is done on this thread until
' the event handler is finished. This is accomplished
' by using CompareExchange to place -1 in syncPoint,
' but only if syncPoint is currently zero (specified
' by the third parameter of CompareExchange).
' CompareExchange returns the original value that was
' in syncPoint. If it was not zero, then there's an
' event handler running, and it is necessary to try
' again.
While Interlocked.CompareExchange(syncPoint, -1, 0) <> 0
' Give up the rest of this thread's current time
' slice. This is a fairly naive algorithm for
' yielding.
Thread.Sleep(0)
' Tally a wait, but don't count multiple calls to
' Thread.Sleep.
If Not counted Then
numWaits += 1
counted = True
End If
End While
' Any processing done after this point does not conflict
' with timer events. This is the purpose of the call to
' CompareExchange. If the processing done here would not
' cause a problem when run concurrently with timer events,
' then there is no need for the extra synchronization.
End Sub
' Event-handling methof for the Elapsed event.
Private Sub Timer1_ElapsedEventHandler( _
ByVal sender As Object, _
ByVal e As ElapsedEventArgs _
) Handles Timer1.Elapsed
numEvents += 1
' This example assumes that overlapping events can be
' discarded. That is, if an Elapsed event is raised before
' the previous event is finished processing, the second
' event is ignored.
'
' CompareExchange is used to take control of syncPoint,
' and to determine whether the attempt was successful.
' CompareExchange attempts to put 1 into syncPoint, but
' only if the current value of syncPoint is zero
' (specified by the third parameter). If another thread
' has set syncPoint to 1, or if the control thread has
' set syncPoint to -1, the current event is skipped.
' (Normally it would not be necessary to use a local
' variable for the return value. A local variable is
' used here to determine the reason the event was
' skipped.)
'
Dim sync As Integer = Interlocked.CompareExchange(syncPoint, 1, 0)
If sync = 0 Then
' No other event was executing.
' The event handler simulates an amount of work
' lasting between 50 and 200 milliseconds, so that
' some events will overlap.
Dim delay As Integer = 50 + rand.Next(150)
Thread.Sleep(delay)
numExecuted += 1
' Release control of syncPoint.
syncPoint = 0
Else
If sync = 1 Then numSkipped += 1 Else numLate += 1
End If
End Sub
End Module
' On a dual-processor computer, this code example produces
' results similar to the following:
'Test 100/100100 test runs completed.
'600 events were raised.
'488 events executed.
'112 events were skipped for concurrency.
'0 events were skipped because they were late.
'Control thread waited 73 times for an event to complete.
C# 复制代码
using System;
using System.Timers;
using System.Threading;
public class Test
{
// Change these values to control the behavior of the program.
private static int testRuns = 100;
// Times are given in milliseconds:
private static int testRunsFor = 1000;
private static int timerInterval = 150;
// Qualify the name to avoid confusion with the
// System.Threading.Timer class.
private static System.Timers.Timer Timer1 = new System.Timers.Timer();
private static Random rand = new Random();
// This is the synchronization point that prevents events
// from running concurrently, and prevents the main thread
// from executing code after the Stop method until any
// event handlers are done executing.
private static int syncPoint = 0;
// Count the number of times the event handler is called,
// is executed, is skipped, or is called after Stop.
private static int numEvents = 0;
private static int numExecuted = 0;
private static int numSkipped = 0;
private static int numLate = 0;
// Count the number of times the thread that calls Stop
// has to wait for an Elapsed event to finish.
private static int numWaits = 0;
[MTAThread]
public static void Main()
{
Timer1.Elapsed += new ElapsedEventHandler(Timer1_ElapsedEventHandler);
Timer1.Interval = timerInterval;
Console.WriteLine();
for(int i = 1; i <= testRuns; i++)
{
TestRun();
Console.Write("\rTest {0}/{1}", i, testRuns);
}
Console.WriteLine("{0} test runs completed.", testRuns);
Console.WriteLine("{0} events were raised.", numEvents);
Console.WriteLine("{0} events executed.", numExecuted);
Console.WriteLine("{0} events were skipped for concurrency.", numSkipped);
Console.WriteLine("{0} events were skipped because they were late.", numLate);
Console.WriteLine("Control thread waited {0} times for an event to complete.", numWaits);
}
public static void TestRun()
{
// Set syncPoint to zero before starting the test
// run.
syncPoint = 0;
Timer1.Enabled = true;
// Start the control thread that shuts off the timer.
Thread t = new Thread(ControlThreadProc);
t.Start();
// Wait until the control thread is done before proceeding.
// This keeps the test runs from overlapping.
t.Join();
}
private static void ControlThreadProc()
{
// Allow the timer to run for a period of time, and then
// stop it.
Thread.Sleep(testRunsFor);
Timer1.Stop();
// The 'counted' flag ensures that if this thread has
// to wait for an event to finish, the wait only gets
// counted once.
bool counted = false;
// Ensure that if an event is currently executing,
// no further processing is done on this thread until
// the event handler is finished. This is accomplished
// by using CompareExchange to place -1 in syncPoint,
// but only if syncPoint is currently zero (specified
// by the third parameter of CompareExchange).
// CompareExchange returns the original value that was
// in syncPoint. If it was not zero, then there's an
// event handler running, and it is necessary to try
// again.
while (Interlocked.CompareExchange(ref syncPoint, -1, 0) != 0)
{
// Give up the rest of this thread's current time
// slice. This is a fairly naive algorithm for
// yielding.
Thread.Sleep(0);
// Tally a wait, but don't count multiple calls to
// Thread.Sleep.
if (!counted)
{
numWaits += 1;
counted = true;
}
}
// Any processing done after this point does not conflict
// with timer events. This is the purpose of the call to
// CompareExchange. If the processing done here would not
// cause a problem when run concurrently with timer events,
// then there is no need for the extra synchronization.
}
// Event-handling method for the Elapsed event.
private static void Timer1_ElapsedEventHandler(
object sender,
ElapsedEventArgs e)
{
numEvents += 1;
// This example assumes that overlapping events can be
// discarded. That is, if an Elapsed event is raised before
// the previous event is finished processing, the second
// event is ignored.
//
// CompareExchange is used to take control of syncPoint,
// and to determine whether the attempt was successful.
// CompareExchange attempts to put 1 into syncPoint, but
// only if the current value of syncPoint is zero
// (specified by the third parameter). If another thread
// has set syncPoint to 1, or if the control thread has
// set syncPoint to -1, the current event is skipped.
// (Normally it would not be necessary to use a local
// variable for the return value. A local variable is
// used here to determine the reason the event was
// skipped.)
//
int sync = Interlocked.CompareExchange(ref syncPoint, 1, 0);
if (sync == 0)
{
// No other event was executing.
// The event handler simulates an amount of work
// lasting between 50 and 200 milliseconds, so that
// some events will overlap.
int delay = 50 + rand.Next(150);
Thread.Sleep(delay);
numExecuted += 1;
// Release control of syncPoint.
syncPoint = 0;
}
else
{
if (sync == 1) { numSkipped += 1; } else { numLate += 1; }
}
}
/* On a dual-processor computer, this code example produces
results similar to the following:
Test 100/100100 test runs completed.
600 events were raised.
488 events executed.
112 events were skipped for concurrency.
0 events were skipped because they were late.
Control thread waited 73 times for an event to complete.
*/left属性就是这个label左边的位置 初始是0
在timer过程中 left每个时段加20 一直加 所以你看到的就是label一直在向右移动
下面的判断就是如果这个label已经移动出屏幕(窗体的宽度form1.width)
那么就把这个控件移回去,并且让label的left为负(这个label的宽度-label1.width)
这样你看到的效果就是从屏幕外面一点一点移动进来,就相当于从右边移动过来一样!~展开全部
timer.Enable //是否启用该控件,
timer.Generatenumber//找不到此方法或属性
timer.Tag //返回或设置一个表达式用来存储程序中需要的额外数据。与其它属性不同,Tag 属性值不被使用;可以用该属性来标识对象。
timer.Stop();//停止计数器
timer.Enable=true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
private void timer1_Tick(object sender, EventArgs e)
//每5000毫秒要重复做的事
timer定为5000毫秒,表示每5000毫秒要重复做timer1_Tick()事件里的事,当你运行timer.Stop();时,无法确定是在哪个时候.
就如我是工头,工人10分钟内做完一个零件,我突然去检查,叫他不要做了,你能确定那个零件的完成程度吗?
分享或转载请并注明原出处:https://news.yaliaojie.com/news/473.html
本信息由鸭寮街发布,不代表鸭寮街立场和观点,如标记有误或者侵犯您的利益请联系我们删除。