Thread and Database read suggestion
I have an issue with my current .net application and is the follow, when a database row is updated or a new row is inserted, my .net service has to read those changes and submit an print order.
I'd implemented the follow functions :
public void OnStart()
{
ThreadMgmt pthread = new ThreadMgmt();
pthread.printNotification += new pthread.DatabaseChanged();
pthread.frequency = 2000;
ThreadStart ts = new ThreadStart(pthread.Wait);
Thread t = new Thread(ts);
t.Start();
}
void ReadDataBase() {...}
void Printing(){...}
public class ThreadMgmt
{
public delegate void UpdateDelegate();
public event UpdateDe开发者_JAVA技巧legate Notify;
private int frequency {set;get;}
public void Wait()
{
for (int i = 0; i <= Freq; i++)
{
Thread.Sleep(this.frequncy);
Notify();
}
}
}
But I need some help about concepts about threading and looping events, I would like to know if anybody has to face the same problem and how to resolve it...
Thanks
Here's a simple solution.
private bool m_stop;
public void Stop ()
{
lock (this)
m_stop = true;
}
public void Wait()
{
for (int i = 0; i <= Freq; i++)
{
lock (this)
{
if (m_stop)
return;
}
Thread.Sleep(this.frequncy);
if (Notify != null)
Notify();
}
}
If you want shutdown to occur quicker, you can use Monitor.Wait and Monitor.PulseAll, but you should read up on these before you decide to do it.
精彩评论