1. On Saturday September 3RD, 2016 this community will be shutdown permanently.

    The time has come to close this community. I no longer have any interest in Visual Studio programming and my efforts to find someone to take over the community has failed.

    If anyone is interested in acquiring the database contact me and I will make it available for you.

Run a query every 2 seconds.

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

  1. DN Admin

    DN Admin Administrator
    Staff Member

    Country:
    United States
    Joined:
    Aug 7, 2010
    Messages:
    12,048
    Likes Received:
    258
    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 1<span class="x_Apple-tab-span" style="white-space:pre
    'win3.Form1.OnStart(string[])': no suitable method found to override<span class="x_Apple-tab-span" style="white-space:pre
    C:Documents and SettingsnsMy DocumentsVisual Studio 2010Projectswin3win3Form1.cs<span class="x_Apple-tab-span" style="white-space:pre
    25<span class="x_Apple-tab-span" style="white-space:pre 33<span class="x_Apple-tab-span" style="white-space:pre
    win3
    Error<span class="x_Apple-tab-span" style="white-space:pre 2<span class="x_Apple-tab-span" style="white-space:pre
    'win3.Form1.OnStop()': no suitable method found to override<span class="x_Apple-tab-span" style="white-space:pre
    C:Documents and SettingsnsMy DocumentsVisual Studio 2010Projectswin3win3Form1.cs<span class="x_Apple-tab-span" style="white-space:pre
    31<span class="x_Apple-tab-span" style="white-space:pre 33<span class="x_Apple-tab-span" style="white-space:pre
    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
     

Share This Page