ASP.NET----タイマコールバック方法の再入力

13958 ワード


  
    
using System;
using System.Collections.Generic;
using System.Text;

namespace NET.MST.Sixth.Reenter
{
class Reenter
{
//
private static int TestInt1 = 0 ;
private static int TestInt2 = 0 ;

private static object locko = new object ();

static void Main( string [] args)
{
Console.WriteLine(
" System.Timers.Timer : " );
TimersTimerReenter();
//
System.Threading.Thread.Sleep( 2 * 1000 );
Console.WriteLine(
" System.Threading.Timer : " );
ThreadingTimerReenter();
Console.Read();
}
/// <summary>
/// System.Timers.Timer
/// </summary>
static void TimersTimerReenter()
{
System.Timers.Timer timer
= new System.Timers.Timer();
timer.Interval
= 100 ; // 100
timer.Elapsed += TimersTimerHandler;
timer.Start();
System.Threading.Thread.Sleep(
2 * 1000 ); // 2
timer.Stop();
}
/// <summary>
/// System.Threading.Timer
/// </summary>
static void ThreadingTimerReenter()
{

// 100
using (System.Threading.Timer timer = new System.Threading.Timer
(
new System.Threading.TimerCallback(ThreadingTimerHandler), null , 0 , 100 ))
{
System.Threading.Thread.Sleep(
2 * 1000 ); // 2
}
}
/// <summary>
/// System.Timers.Timer
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private static void TimersTimerHandler( object sender,EventArgs args)
{
lock (locko)
{
Console.WriteLine(
" " + TestInt1.ToString());
// 10 ,
System.Threading.Thread.Sleep( 300 );
TestInt1
++ ;
Console.WriteLine(
" 1 : " + TestInt1.ToString());
}
}
/// <summary>
/// System.Threading.Timer
/// </summary>
/// <param name="state"></param>
private static void ThreadingTimerHandler( object state)
{
lock (locko)
{
Console.WriteLine(
" " + TestInt2.ToString());
// 10 ,
System.Threading.Thread.Sleep( 300 );
TestInt2
++ ;
Console.WriteLine(
" 1 : " + TestInt2.ToString());
}
}
}
}