大家帮忙看看,为什么我的程序只能运行一次,timer不起作用
------------------------------------------------------------------------
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
// 
// timer1
// 
this.timer1.Interval = 1000;
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
}
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void timer1_Tick(object sender, System.EventArgs e)
{ string connection = ConfigurationSettings.AppSettings["GetConncetion"].ToString();
string extno1 = ConfigurationSettings.AppSettings["extno1"].ToString();
string uid = ConfigurationSettings.AppSettings["uid"].ToString();
string pswd = ConfigurationSettings.AppSettings["pswd"].ToString();
SqlConnection myconnection = new SqlConnection(connection); 
myconnection.Open(); 
        string selectstring = "select  top 2 * from TSMS where MobileNum between '130' and '133' order by MobileID"; 
SqlDataAdapter mycommand = new SqlDataAdapter(selectstring, myconnection);
DataTable dt = new DataTable();
mycommand.Fill(dt);
HttpWebResponse res = null;
for(int i=0;i<dt.Rows.Count;i++)
            { string strmsg = System.Web.HttpUtility.UrlEncode((string)dt.Rows[i]["Content"],System.Text.Encoding.GetEncoding("GB2312"));
string strmob = (string)dt.Rows[i]["MobileNum"];
string strUrl = "http://econnect.hotdotchina.com/econnect/pitf/submit.jsp?uid=" + uid + "&pswd=" + pswd + "&extno=" + extno1 + "&msg=" + strmsg + "&mob=" + strmob;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(strUrl);
res = (HttpWebResponse)req.GetResponse ();             }
}

解决方案 »

  1.   


    this.timer1.Interval = 1000;
    this.timer1.Enabled = true;
    me.Timer1.Start();
    this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    这样试试
      

  2.   

    为什么要把读数据库的放在timer里,放在一个线程里也比timer里好
      

  3.   

    为什么要把读数据库的放在timer里,放在一个线程里也比timer里好
    --------------------------------------------------------------------------
    怎么放在一个线程里呀???有代码吗????
      

  4.   

    我的意思是,你别用timer1啊
    你直接把timer1里的代码拷出来,直接运行不行么?因为timer1有个周期,在这个周期内不管你数据处理完没有,都是要结束的,很容易引发异常
      

  5.   

    如果你的数据是不段增加的,建议你在数据库层建一个触发器就可以了,没有必要放在timer里,影响效率
      

  6.   

    你把time的enable先设置为false
    然后在你的load里把它置为ture
    就可以了
      

  7.   

    还有就是在timer,控件时间里不要循环访问数据库,那样会使你的程序在timer控件时间被触发的时候变的很慢,要是你机器性能不好点,程序就死了
    在里面用线程,更合理一点,以上是我的两点意见!望大家指教
      

  8.   

    给你一个使用线程的例子
    http://www.chinaworkroom.com/Bbs/ViewTopic.aspx?TopicID=193&boardID=8
      

  9.   

    Re:http://community.csdn.net/Expert/topic/4780/4780398.xml?temp=.5009424
    ----------------------------....threading.thread t = new threading.thread(run)
    t.start() '线程
    ===================
    private void run()
    {
    string connection = ConfigurationSettings.AppSettings["GetConncetion"].ToString();
    string extno1 = ConfigurationSettings.AppSettings["extno1"].ToString();
    string uid = ConfigurationSettings.AppSettings["uid"].ToString();
    string pswd = ConfigurationSettings.AppSettings["pswd"].ToString();
    SqlConnection myconnection = new SqlConnection(connection); 
    myconnection.Open(); 
            string selectstring = "select  top 2 * from TSMS where MobileNum between '130' and '133' order by MobileID"; 
    SqlDataAdapter mycommand = new SqlDataAdapter(selectstring, myconnection);
    DataTable dt = new DataTable();
    mycommand.Fill(dt);
    HttpWebResponse res = null;
    for(int i=0;i<dt.Rows.Count;i++)
                {string strmsg = System.Web.HttpUtility.UrlEncode((string)dt.Rows[i]["Content"],System.Text.Encoding.GetEncoding("GB2312"));
    string strmob = (string)dt.Rows[i]["MobileNum"];
    string strUrl = "http://econnect.hotdotchina.com/econnect/pitf/submit.jsp?uid=" + uid + "&pswd=" + pswd + "&extno=" + extno1 + "&msg=" + strmsg + "&mob=" + strmob;
    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(strUrl);
    res = (HttpWebResponse)req.GetResponse ();             }
    }
      

  10.   

    copico(一路向北) 
    -------------------------------------------------------------
    我的数据库里面的数据会不断变化的,这样好象不行呀
      

  11.   

    copico(一路向北) 
    -------------------------------------------------------------
    我的数据库里面的数据会不断变化的,这样好象不行呀