Question

I have a form in a Windows Form application, I need to repeatedly poll a database. I'm here for asking, what is the best way to make this simple form in a poll service, avoiding deadlock and similar issues (I wouldn't use a Windows service)?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public PollingService(){
        // do some stuff every x seconds
    }
}

PS: I'm not asking for a ready-code. I just need to know how to organise this.

Was it helpful?

Solution

You can try this, if you still want to poll. You can use a timer.

example:

    private Timer _dbCheckTimer;

    public void InitTimer()
    {
        _dbCheckTimer = new Timer();
        _dbCheckTimer.Elapsed += DBCheckTimer_Elapsed;
        _dbCheckTimer.Interval = 10000; // 10 seconds
        _dbCheckTimer.Start();
    }

    public void DisposeTimer()
    {
        _dbCheckTimer.Dispose();
    }

    void DBCheckTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        _dbCheckTimer.Stop();
        try
        {
            // check DB
        }
        finally
        {
            _dbCheckTimer.Start();
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top