直播中
using System;
using System.Threading ;
namespace testall
{
/// <summary>
/// 一個web job的示例類
/// </summary>
/// <remarks>符合design pattern的singleton模式</remarks>
public class TestStatic
{
/// <summary>
/// 定時間隔
/// </summary>
/// <remarks>通過修改這個常量決定間隔多長時間做某件事</remarks>
const int DELAY_TIMES = 1000 ;
/// <summary>
/// 一個計數(shù)器
/// </summary>
private int m_intCounter = 0;
/// <summary>
/// 是否退出
/// </summary>
private bool m_bCanExit = false ;
/// <summary>
/// 線程
/// </summary>
private Thread thread ;
/// <summary>
/// 自身實例
/// </summary>
/// <remarks>注意,這是實現(xiàn)singleton的關(guān)鍵</remarks>
private static TestStatic instance = new TestStatic() ;
public int Counter
{
get
{
return this.m_intCounter ;
}
set
{
this.m_intCounter = value ;
}
}
public bool CanExit
{
set
{
this.m_bCanExit = value ;
}
}
/// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
public TestStatic()
{
//
// TODO: Add constructor logic here
//
this.m_intCounter = 0 ;
Console.WriteLine("constructor is running") ;
this.thread = new Thread(new ThreadStart(ThreadProc)) ;
thread.Name = "online user" ;
thread.Start() ;
Console.WriteLine("完畢") ;
}
/// <summary>
/// 實現(xiàn)singleton的關(guān)鍵
/// </summary>
/// <returns>類本身的一個實例</returns>
/// <remarks>唯一的全局切入點</remarks>
public static TestStatic GetInstance()
{
return instance ;
}
/// <summary>
/// 線程工作函數(shù)
/// </summary>
/// <remarks>想做什么寫在這兒</remarks>
private void ThreadProc()
{
while(!this.m_bCanExit)
{
this.m_intCounter ++ ;
Console.WriteLine(this.m_intCounter.ToString()) ;
Thread.Sleep(DELAY_TIMES) ;
}
}
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
Console.WriteLine(TestStatic.GetInstance().Counter.ToString()) ;
Console.Read() ;
TestStatic.GetInstance().CanExit = true ;
}
}
}