我写的一个数据库连接类,因为网络连接情况可能不好,所以myConnection.Open()就会一直连接,程序也就停在这了,现在要判断在3秒内能否连接上,如果连接不上,强行停止连接,请教给位高手,如何做?另外开始我用的线程来解决这个问题:public void Open(string strDateBaseName)    
{
//データベースにつながる
if (myConnection == null) 
{
    myConnection = new SqlConnection(" ………………");
            thread1 = new Thread(new ThreadStart(Count1));      开始线程
    thread1.Priority = ThreadPriority.Highest;
    thread1.Start();
    thread1.Join();
}
}
//データベースを開ける
public void Count1()

if(myConnection.State == ConnectionState.Closed)
{
try
{
System.Timers.Timer time = new System.Timers.Timer(1000);       定义时间变量,一定时间后检查连接状况
time.Elapsed += new System.Timers.ElapsedEventHandler(theout);
time.AutoReset = false;
time.Enabled = true;
myConnection.Open();     在线程里执行连接数据库
}
catch(ThreadAbortException ee)
{
Console.WriteLine(ee.Message);
}
}
}
/// <summary>
/// データベースを閉鎖する
/// </summary>
/// 
public void theout( Object source, ElapsedEventArgs e)
{
if(myConnection.State == ConnectionState.Open)
{
}
else 
{
myConnection.Close();
thread1.Abort();                             如果连接不上,强行中止线程
thread1.Join();
//thread1 = null;
}
}
但是Abort执行之后,不会马上执行catch(ThreadAbortException ee)从而结束程序,而是一定要等一段时间,严重的影响了系统的性能,请问立即结束线程的需要什么方法,谢谢大家了!

解决方案 »

  1.   

    直接用连接超时设置就可以了
    myConnection.TimeOut = 3*1000;
      

  2.   

    谢谢你的回复,不过myConnection.TimeOut只对完全没有网络有效,对网络拥堵无效。
      

  3.   

    多线程解决 ,给你参考下,本来我用来判断SQL服务器是不是可连接用的...
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    using System.Data.SqlClient;namespace WindowsApplication3
    {
        public partial class Form1 : Form
        {
            bool Return=true;
            AutoResetEvent sleepSynchro = new AutoResetEvent(false);
            Exception err = null;
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                Thread controlThread = new Thread(new ThreadStart(test));
                controlThread.Start();
                if (!sleepSynchro.WaitOne(3000, false))
                {
                    MessageBox.Show("数据库连接超时.", "错误:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (!Return)
                {
                    MessageBox.Show("数据库连接失败:" + err.Message, "错误:", MessageBoxButtons.OK, MessageBoxIcon.Error);             }
            }
            void test()
            {
                string sqlconn = @"Data Source=3.0.0.0;Initial Catalog=eDocument;Integrated Security=True";            SqlConnection conn = new SqlConnection(sqlconn);
                try
                {
                    conn.Open();
                    Return = true;
                }
                catch (Exception e)
                {
                    Return = false;
                    err = e;
                }
                finally
                {
                    conn.Close();
                    sleepSynchro.Set();
                }        }
        }
    }
      

  4.   

    谢谢兄弟的回复,不过你也没有结束open的语句啊,不过谢谢关心,结帖时加分。
      

  5.   

    正在执行的open语句是没法终止的,
      

  6.   

    在这里我说明一下..
     private void button1_Click(object sender, EventArgs e)
            {
                Thread controlThread = new Thread(new ThreadStart(test));
                controlThread.Start();
                if (!sleepSynchro.WaitOne(3000, false))
                {
                    controlThread.Abort();                                                           
    MessageBox.Show("数据库连接超时.", "错误:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (!Return)
                {
                    MessageBox.Show("数据库连接失败:" + err.Message, "错误:", MessageBoxButtons.OK, MessageBoxIcon.Error);             }
            }
    当加入这句controlThread.Abort();是不可以立即结束线程的..
    特别是对这种处于非.Net代码段的阻塞线程的情况...为了达到目的..因此没有加controlThread.Abort();现在我比较关心的就是不加.Abort();资源是否可以释放的问题
                                                      
    void test()
            {
                string sqlconn = @"Data Source=3.0.0.0;Initial Catalog=eDocument;Integrated Security=True";            SqlConnection conn = new SqlConnection(sqlconn);
                try
                {
                    conn.Open();
                    Return = true;
                }
                catch (Exception e)
                {
                    Return = false;
                    err = e;
                }
                finally
                {
                    conn.Close();
                    sleepSynchro.Set();
                }        }
    这段代码没有循环.只是一个顺序执行的程序块..因此当执行完成的时候,包括线程都会得到释放.就是说没有释放的问题.
    所以解决这个问题..这个代码应该是可以使用的...
    唯一的问题就是当连接不上的时候...立即关闭程序主进程要等到test()方法完成才可以释放.
    当然.如果有人对此还有疑义的话,那就不是我可以解决的了.