public static void ThreadFunc()
{
SqlConnection Con=new SqlConnection("server=(local);uid=sa;pwd=;database=Catch_page");
int LastMinute  = DateTime.Now.Minute;
int Id = 750;
int j = 0;
while (true)
{
System.Threading.Thread.Sleep(60000);
if (DateTime.Now.Minute - 1 == LastMinute)
{
WebRequest webr=WebRequest.Create("http://192.168.0.23:888/dx_allfile/list_dx.asp?id="+Id.ToString());
Stream rc=webr.GetResponse().GetResponseStream();
StreamReader read=new StreamReader(rc,System.Text.Encoding.Default);
string Str_Read = read.ReadToEnd().TrimEnd();
if(Str_Read.Trim() == "")
{
j++;
}
else
{
j = 0;
string Sql_insert = "insert info (Content) values ('"+Str_Read+"')"; 
SqlCommand myCommand = new SqlCommand(Sql_insert,Con);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
Con.Close();
read.Close();
}
if(j<3)
Id++;
MessageBox.Show("记录已写入!http://192.168.0.23:888/dx_allfile/list_dx.asp?id="+Id.ToString(),"警告",MessageBoxButtons.OK,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button1,MessageBoxOptions.DefaultDesktopOnly);
LastMinute = DateTime.Now.Minute;

}

}
}
上面这个修改的方法应该没有错吧。

解决方案 »

  1.   

    各位兄台:帮忙看看
    http://community.csdn.net/Expert/TopicView.asp?id=3751707
      

  2.   

    先测试一下你的ThreadFunc()函数是否能正常运行,如果有异常发生而没有扑获服务就会停止。
      

  3.   

    写成这样
    ThreadFunc()
    {
       ....
       while(true)
       {
          try
          {
             //处理代码
           }
           catch(Exception ex)
          {
              //扑获异常,但是不要再抛出异常,用写日志代替.
           }
       }
    }
      

  4.   

    LastMinute = DateTime.Now.Minute;的比较问题吧?你可以调试一下,看看哪里的问题
      

  5.   

    public static void ThreadFunc()
    {
    SqlConnection Con=new SqlConnection("server=(local);uid=sa;pwd=;database=Catch_page");
    int LastMinute  = DateTime.Now.Minute;
    int Id = 750;
    int j = 0;
    while (true)
    {
    System.Threading.Thread.Sleep(60000);
    if (DateTime.Now.Minute - 1 == LastMinute)
    {
    WebRequest webr=WebRequest.Create("http://192.168.0.23:888/dx_allfile/list_dx.asp?id="+Id.ToString());
    Stream rc=webr.GetResponse().GetResponseStream();
    StreamReader read=new StreamReader(rc,System.Text.Encoding.Default);
    string Str_Read = read.ReadToEnd().TrimEnd();
    if(Str_Read.Trim() == "")
    {
    j++;
    }
    else
    {
    j = 0;
    string Sql_insert = "insert info (Content) values ('"+Str_Read+"')"; 
    SqlCommand myCommand = new SqlCommand(Sql_insert,Con);
    myCommand.Connection.Open();
    myCommand.ExecuteNonQuery();
    myCommand.Connection.Close();
    Con.Close();
    read.Close();
    }
    if(j<3)
    Id++;
    MessageBox.Show("记录已写入!http://192.168.0.23:888/dx_allfile/list_dx.asp?id="+Id.ToString(),"警告",MessageBoxButtons.OK,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button1,MessageBoxOptions.DefaultDesktopOnly);
    LastMinute = DateTime.Now.Minute;

    }

    }
    }为什么ThreadFunc()函数内的循环只能一次。只能执行一次插入语句。且问题是自动增量的以2为单位。
    假如我把ThreadFunc()写成下面的就是正常的。
    public static void ThreadFunc()
    {
    int LastMinute=DateTime.Now.Minute;
    while (true)
    {
    System.Threading.Thread.Sleep(60000);
    if (DateTime.Now.Minute-1 == LastMinute)
    {
    MessageBox.Show("一分钟到了!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button1,MessageBoxOptions.DefaultDesktopOnly);
    LastMinute=DateTime.Now.Minute;
    }
    }
    }
      

  6.   

    为什么ThreadFunc()函数内的循环只能一次。只能执行一次插入语句。且问题是自动增量的以2为单位。
    假如我把ThreadFunc()写成下面的就是正常的。
    public static void ThreadFunc()
    {
    int LastMinute=DateTime.Now.Minute;
    while (true)
    {
    System.Threading.Thread.Sleep(60000);
    if (DateTime.Now.Minute-1 == LastMinute)
    {
    MessageBox.Show("一分钟到了!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button1,MessageBoxOptions.DefaultDesktopOnly);
    LastMinute=DateTime.Now.Minute;
    }
    }
    }
      

  7.   

    为什么ThreadFunc()函数内的循环只能一次。只能执行一次插入语句。且问题是自动增量的以2为单位。
    假如我把ThreadFunc()写成下面的就是正常的。
    public static void ThreadFunc()
    {
    int LastMinute=DateTime.Now.Minute;
    while (true)
    {
    System.Threading.Thread.Sleep(60000);
    if (DateTime.Now.Minute-1 == LastMinute)
    {
    MessageBox.Show("一分钟到了!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button1,MessageBoxOptions.DefaultDesktopOnly);
    LastMinute=DateTime.Now.Minute;
    }
    }
    }
      

  8.   

    为什么ThreadFunc()函数内的循环只能一次。只能执行一次插入语句。且问题是自动增量的以2为单位。
    假如我把ThreadFunc()写成下面的就是正常的。
    public static void ThreadFunc()
    {
    int LastMinute=DateTime.Now.Minute;
    while (true)
    {
    System.Threading.Thread.Sleep(60000);
    if (DateTime.Now.Minute-1 == LastMinute)
    {
    MessageBox.Show("一分钟到了!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button1,MessageBoxOptions.DefaultDesktopOnly);
    LastMinute=DateTime.Now.Minute;
    }
    }
    }
      

  9.   

    为什么ThreadFunc()函数内的循环只能一次。只能执行一次插入语句。且问题是自动增量的以2为单位。
    假如我把ThreadFunc()写成下面的就是正常的。
    public static void ThreadFunc()
    {
    int LastMinute=DateTime.Now.Minute;
    while (true)
    {
    System.Threading.Thread.Sleep(60000);
    if (DateTime.Now.Minute-1 == LastMinute)
    {
    MessageBox.Show("一分钟到了!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button1,MessageBoxOptions.DefaultDesktopOnly);
    LastMinute=DateTime.Now.Minute;
    }
    }
    }