Run a query every 2 seconds.

Discussion in 'Visual C# .Net' started by DN Admin, May 1, 2012.

  1. DN Admin

    Staff Member

    Joined:
    Aug 7, 2010
    Location:
    Joliet, IL U.S.A.
    Dear All,
    I have an application where I would like to run a query every seconds based on the current time. So I started a thread for that but I got stuck at the onStart and onStop method. I would like to check too if thread
    is still running then do not let next one run till the previous is done.
    Error<span class="x_Apple-tab-span" style="white-space:pre </span>1<span class="x_Apple-tab-span" style="white-space:pre
    </span>'win3.Form1.OnStart(string[])': no suitable method found to override<span class="x_Apple-tab-span" style="white-space:pre
    </span>C:Documents and SettingsnsMy DocumentsVisual Studio 2010Projectswin3win3Form1.cs<span class="x_Apple-tab-span" style="white-space:pre
    </span>25<span class="x_Apple-tab-span" style="white-space:pre </span>33<span class="x_Apple-tab-span" style="white-space:pre
    </span>win3
    Error<span class="x_Apple-tab-span" style="white-space:pre </span>2<span class="x_Apple-tab-span" style="white-space:pre
    </span>'win3.Form1.OnStop()': no suitable method found to override<span class="x_Apple-tab-span" style="white-space:pre
    </span>C:Documents and SettingsnsMy DocumentsVisual Studio 2010Projectswin3win3Form1.cs<span class="x_Apple-tab-span" style="white-space:pre
    </span>31<span class="x_Apple-tab-span" style="white-space:pre </span>33<span class="x_Apple-tab-span" style="white-space:pre
    </span>win3<br/>


    Below is my codes

    <pre class="prettyprint using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using MySql.Data.MySqlClient;
    using System.IO;
    using System.Threading;
    namespace win3
    {
    public partial class Form1 : Form
    {
    private Thread doWorkThread;

    private bool existThread = false;

    public Form1()
    {
    InitializeComponent();
    this.doWorkThread = new Thread(new ThreadStart(DoWork));
    }
    protected override void OnStart(string[] args)
    {

    this.doWorkThread.Start(); // Start The Thread
    }

    protected override void OnStop()
    {
    this.existThread = true;
    }
    private void DoWork()
    {
    MessageBox.Show("TEST");
    while (true)
    {

    if (this.existThread)
    return;
    //to run my query every 2 seconds
    Thread.Sleep(TimeSpan.FromSeconds(20));
    }
    }
    private void Form1_Load(object sender, EventArgs e)
    {

    }
    }
    </pre>
    <br/>



    View the full article
     
    #1

Share This Page