开发者

execute 2 MySql query using timer in C#

开发者 https://www.devze.com 2023-04-10 05:52 出处:网络
How can i execute 2 mysql query using ThreadingTimer?I want 1st timer update data in db and 2nd timer count how much data are updated and also show in label in windows form.Here is my code,

How can i execute 2 mysql query using ThreadingTimer?I want 1st timer update data in db and 2nd timer count how much data are updated and also show in label in windows form.Here is my code,

   void PrepareTimers(List<int> _dataValues)
   {
        foreach (int da开发者_运维技巧taValue in _dataValues)
        {
            timer = new ThreadingTimer (new TimerCallback(TimerAction), dataValue, 0, 2000);
            timer1 = new ThreadingTimer(new TimerCallback(TimerAction1), dataValue, 0, 2000);

            //Console.WriteLine("Timer " + dataValue + " created.");


        }
    }
    void TimerAction(object flag)
    {
        //Console.WriteLine("Timer start "+ flag.ToString());
        string myconstring = "SERVER=localhost;" + "DATABASE=alicosms;" + "UID=root;" + "PASSWORD=;";
        MySqlConnection mycon = new MySqlConnection(myconstring);
        string u = "UPDATED";
        mycon.Open();
        //Console.WriteLine("Timer " + flag.ToString() + " : " + "UPDATE sms_data_bankasia set flag= @flag * 2 , sendingstatus = '" + u + "' WHERE flag = " + flag.ToString() + "  LIMIT 1");
        MySqlCommand cmd = new MySqlCommand("UPDATE sms_data_bankasia set flag= @flag * 2 , sendingstatus = '" + u + "' WHERE flag = " + flag.ToString() + " LIMIT 1", mycon);
        MySqlParameter param = new MySqlParameter();
        param.ParameterName = "@flag";
        param.Value = flag;
        cmd.Parameters.Add(param);
        cmd.ExecuteNonQuery();
        mycon.Close();                                  
    }

    void TimerAction1(object flag)
    {
        string myconstring = "SERVER=localhost;" + "DATABASE=alicosms;" + "UID=root;" + "PASSWORD=;";
        MySqlConnection mycon = new MySqlConnection(myconstring);
        string sql = "SELECT count(flag) FROM sms_data_bankasia where sendingstatus='UPDATED' group by flag";
        MySqlCommand comd = mycon.CreateCommand();
        comd.CommandText = sql;
        mycon.Open();
        MySqlDataReader dtr = comd.ExecuteReader();
        try
        {
            while (dtr.Read())
            {
                dict[timer] = label;
                dict[timer].Text = dtr[0].ToString() + " program Updated";
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        mycon.Close();
    } 

It provide error,"cross thread operation not valid". What should i do? Any good suggestion?


In Windows forms, you cannot do anything in the UI from any other thread than the UI-thread. Your timer is running your code on a seperate thread.

You could try to use the System.Windows.Forms.Timer instead, as it will marshal the event handling back to the UI thread for you.


Here you can read about the solution

Just to test if it fixes your issue try wrapping your MessageBox call in code like this:

public delegate void StringDelegate(string message);

private void ShowMessageBox( string message )
{
    if (this.InvokeRequired)
    {
        this.Invoke( new StringDelegate( ShowMessageBox, new object[] { message } ));
        return;
    }

    MessageBox.Show( message );
}

This is written partly from memory, so it might vary a little.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号