How dispose timer?
How dispose System.Threading.Timer correctly?
I have got this result:
pub_MyEvent1
pub_MyEvent1
pub_MyEvent1
pub_MyEvent1
new pub_MyEvent
pub_MyEvent1
new pub_MyEvent
Code:
class Program
{
static void Main(string[] args)
{
Publisher pub = new Publisher();
pub.MyEvent += new EventHandler(pub_MyEvent1);
Console.ReadLine();
pub = new Publisher();
pub.MyEvent += new EventHandler(pub_MyEvent);
Console.ReadLine();
}
static void pub_MyEvent(object sender, EventArgs e)
{
Console.WriteLine("new pub_MyEvent");
}
static void pub_MyEvent1(objec开发者_如何学编程t sender, EventArgs e)
{
Console.WriteLine("pub_MyEvent1");
}
}
public class Publisher : IDisposable
{
private System.Threading.Timer _timer;
private EventHandler _myEvent;
public event EventHandler MyEvent
{
add { _myEvent += value; }
remove { _myEvent -= value; }
}
protected void OnMyEvent(Object state)
{
if (_myEvent != null)
{
foreach (EventHandler handler in _myEvent.GetInvocationList())
{
handler(this, new EventArgs());
}
}
}
public Publisher()
{
_timer = new System.Threading.Timer(new System.Threading.TimerCallback(OnMyEvent), null, 1000, 1000);
}
public void Dispose()
{
if (_timer != null)
_timer.Dispose();
}
}
You should dispose of each publisher separately.
static void Main(string[] args)
{
using (Publisher pub = new Publisher())
{
pub.MyEvent += new EventHandler(pub_MyEvent1);
Console.ReadLine();
}
using (Publisher pub = new Publisher())
{
pub.MyEvent += new EventHandler(pub_MyEvent);
Console.ReadLine();
}
}
You could use
using(Publisher pub = new Publisher();)
{
pub.MyEvent += new EventHandler(pub_MyEvent1);
Console.ReadLine();
pub = new Publisher();
pub.MyEvent += new EventHandler(pub_MyEvent);
Console.ReadLine();
}
which is slightly safer than calling pub.Dispose()
after your Console.ReadLine();
because the user could close the application instead of hitting enter. Also if any exception occurs, the using statement would catch the exception, dispose the timer and rethrow the exception back to you.
精彩评论