ediblecode
ediblecode

Reputation: 11971

Calling a Stored Procedure every n seconds

I don't have much experience with multi threading so I am wondering how to refresh my Data Grid View every, for example, two seconds.

Basically, when the user is on a certain tab, and they have selected 'Currently Importing', it should call a method GetNotImportedFiles() every n seconds, the method then calls the SP and binds the DataSet.

Obviously, I need a timer which will do this every n seconds but I need it to execute the method as a background worker but communicate with the UI thread to update the DataGridView.

If you need any code, please ask.

UPDATE: I have implemented a timer but it never seems to hit the method for timerTick?

In my designer I have the code:

this.refreshTimer.Interval = 1000;
this.refreshTimer.Tick += new System.EventHandler(this.refreshTimer_Tick);

My understanding is that refreshTimer_Click should be called every 1 second. But I have a breakpoint in the code that is never hit?

private void refreshTimer_Tick(object sender, EventArgs e)
    {
        if (searchComboBox.Text.Equals("Currently Importing"))
        {
            try
            {
                DataSet temp = EngineBllUtility.GetNotImportedFiles(connectionString);
                if (temp != null)
                    importFileGridView.DataSource = temp.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

Upvotes: 2

Views: 2353

Answers (2)

CodeZombie
CodeZombie

Reputation: 5377

Use a Timer to start the operation every n seconds and a BackgroundWorker for the async call to the SP. Using only the Timer control does not create a background thread operation. This is where the BackgroundWorker comes in. It allows very easy access to a background thread without the need of complex synchronization.

Upvotes: 0

Pranay Rana
Pranay Rana

Reputation: 176896

Mske use of : Timer in C# which allow to query data after every timer tick...you can visite link for more detail about timer.

Timer myTimer = new Timer(500);
timer1.Interval = 5000;
timer1.Enabled = true;
timer1.Tick += new System.EventHandler (OnTimerEvent);

Write the event handler

This event will be executed after every 5 secs.

public static void OnTimerEvent(object source, EventArgs e)
{
m_streamWriter.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),DateTime.Now.ToLongDateString());
m_streamWriter.Flush();
}

Upvotes: 5

Related Questions