主题:通过辅助线程连接数据库,怎样快速取消连接
简要描述一下:
从主线程开启一辅助线程用来连接局域网内的数据库服务器,同时在主线程窗口上设置一按钮(即代码中的取消按钮),在连接服务器的过程中(即连接尚处于Connecting状态)如果点击按钮,那么将改变公共变量的值以提醒辅助线程立刻中断这次尝试(辅助线程依靠一个Timer来检测这个公共变量的值)。
我依此思路写了一个程序,但点击按钮后连接不会马上关闭,而要等到连接失败后才会关闭。怎样才能让连接立刻关闭呢?请教各位了。
并附上代码:
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 System.Threading;
using System.Data.SqlClient;
namespace 数据库连接提问
{
public partial class Form1 : Form
{
Button btn1 = new Button();
Button btn2 = new Button();
Label lbl1 = new Label();
System.Windows.Forms.Timer timer;
public Form1()
{
InitializeComponent();
btn1.Text = "连接";
btn1.Location = new Point(50, 20);
this.Controls.Add(btn1);
btn1.MouseClick+=new MouseEventHandler(btn1_MouseClick);
btn2.Text = "取消";
btn2.Location = new Point(150, 20);
this.Controls.Add(btn2);
btn2.MouseClick+=new MouseEventHandler(btn2_MouseClick);
lbl1.Text = "连接状态";
lbl1.Location = new Point(100, 60);
this.Controls.Add(lbl1);
Common.Initialize();
timer = new System.Windows.Forms.Timer();
timer.Interval = 100;
timer.Tick += new EventHandler(timer_Tick);
}
private void btn1_MouseClick(object sender, MouseEventArgs e)
{
Thread thread=new Thread(ConnThread.Connect);
thread.Start();
lbl1.Text = "连接中";
timer.Enabled=true;
}
private void btn2_MouseClick(object sender, MouseEventArgs e)
{
Common.SynFlag = SynState.Cancel;
}
private void timer_Tick(object sender, EventArgs e)
{
if (Common.conn.State != ConnectionState.Connecting)
{
lbl1.Text = "关闭";
timer.Enabled = false;
}
}
}
//辅助线程
class ConnThread
{
public static void Connect()
{
AutoResetEvent autoEvent = new AutoResetEvent(false);
TimerCallback timerDelegate = WaitToQuit;
System.Threading.Timer timer = new System.Threading.Timer(timerDelegate, autoEvent, 0, 100);
try
{
Common.conn.Open();
}
finally { Common.SynFlag = SynState.End; }
}
private static void WaitToQuit(object state)
{
if (Common.SynFlag == SynState.Cancel)
{
Common.conn.Close();
}
}
}
//公共变量
static class Common
{
public static SqlConnection conn;
public static SynState SynFlag;
public static void Initialize()
{
conn = new SqlConnection();
//以下连接字符串中的服务器应是一个不存在的
Common.conn.ConnectionString="Data Source=xxx;User ID=sa;Pwd=";
}
}
public enum SynState
{ Cancel,Stopped,End };
}
从主线程开启一辅助线程用来连接局域网内的数据库服务器,同时在主线程窗口上设置一按钮(即代码中的取消按钮),在连接服务器的过程中(即连接尚处于Connecting状态)如果点击按钮,那么将改变公共变量的值以提醒辅助线程立刻中断这次尝试(辅助线程依靠一个Timer来检测这个公共变量的值)。
我依此思路写了一个程序,但点击按钮后连接不会马上关闭,而要等到连接失败后才会关闭。怎样才能让连接立刻关闭呢?请教各位了。
并附上代码:
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 System.Threading;
using System.Data.SqlClient;
namespace 数据库连接提问
{
public partial class Form1 : Form
{
Button btn1 = new Button();
Button btn2 = new Button();
Label lbl1 = new Label();
System.Windows.Forms.Timer timer;
public Form1()
{
InitializeComponent();
btn1.Text = "连接";
btn1.Location = new Point(50, 20);
this.Controls.Add(btn1);
btn1.MouseClick+=new MouseEventHandler(btn1_MouseClick);
btn2.Text = "取消";
btn2.Location = new Point(150, 20);
this.Controls.Add(btn2);
btn2.MouseClick+=new MouseEventHandler(btn2_MouseClick);
lbl1.Text = "连接状态";
lbl1.Location = new Point(100, 60);
this.Controls.Add(lbl1);
Common.Initialize();
timer = new System.Windows.Forms.Timer();
timer.Interval = 100;
timer.Tick += new EventHandler(timer_Tick);
}
private void btn1_MouseClick(object sender, MouseEventArgs e)
{
Thread thread=new Thread(ConnThread.Connect);
thread.Start();
lbl1.Text = "连接中";
timer.Enabled=true;
}
private void btn2_MouseClick(object sender, MouseEventArgs e)
{
Common.SynFlag = SynState.Cancel;
}
private void timer_Tick(object sender, EventArgs e)
{
if (Common.conn.State != ConnectionState.Connecting)
{
lbl1.Text = "关闭";
timer.Enabled = false;
}
}
}
//辅助线程
class ConnThread
{
public static void Connect()
{
AutoResetEvent autoEvent = new AutoResetEvent(false);
TimerCallback timerDelegate = WaitToQuit;
System.Threading.Timer timer = new System.Threading.Timer(timerDelegate, autoEvent, 0, 100);
try
{
Common.conn.Open();
}
finally { Common.SynFlag = SynState.End; }
}
private static void WaitToQuit(object state)
{
if (Common.SynFlag == SynState.Cancel)
{
Common.conn.Close();
}
}
}
//公共变量
static class Common
{
public static SqlConnection conn;
public static SynState SynFlag;
public static void Initialize()
{
conn = new SqlConnection();
//以下连接字符串中的服务器应是一个不存在的
Common.conn.ConnectionString="Data Source=xxx;User ID=sa;Pwd=";
}
}
public enum SynState
{ Cancel,Stopped,End };
}