以下是我的部分代码,当我点击按钮Button1后启动线程将数据插入到Access数据库中,可是点击之后界面就发生死锁,拖拖不动直接一片白,但是progressBar却在动!!!?请问怎样改我这几段代码才可以避免界面无响应被死锁啊?希望能将代码贴出来,最好是以改写我这个代码为基础的,不要在网上乱找的。同时希望能再加一个暂停的功能,能够做到我点另一个暂停按钮,它能够暂停,而我再点一次则继续执行(不是终止了后又重新跑以至于我数据库出现重复数据)。private DataTable GetData()
{
//获取数据
//.......
//.......
}private void ImportToDataBase()
{
DataTable dt=GetData();
int rsCount= dt.Rows.Count;
this.progressBar1.Minimum=0;
this.progressBar1.Maximum=rsCount;
this.progressBar1.Step=1;
int exeCount=0;
while (exeCount!=rsCount)
{
//从dt从获取数据整理后插入数据库
//.......
exeCount++;
this.progressBar1.Value=exeCount;
}
}private void Button1_Click()
{
Thread thd=new Thread(new ThreadStar(ImportToDataBase));
thd.Start();
}
{
//获取数据
//.......
//.......
}private void ImportToDataBase()
{
DataTable dt=GetData();
int rsCount= dt.Rows.Count;
this.progressBar1.Minimum=0;
this.progressBar1.Maximum=rsCount;
this.progressBar1.Step=1;
int exeCount=0;
while (exeCount!=rsCount)
{
//从dt从获取数据整理后插入数据库
//.......
exeCount++;
this.progressBar1.Value=exeCount;
}
}private void Button1_Click()
{
Thread thd=new Thread(new ThreadStar(ImportToDataBase));
thd.Start();
}
{
try
{
Thread thd=new Thread(new ThreadStar(ImportToDataBase));
thd.Start();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
http://www.cnblogs.com/net66/archive/2005/08/03/206132.html与这片文章相对应的还有一片。 更高 可惜我看不懂。 希望对你有用吧。
System.Threading.ManualResetEvent obj = new ManualResetEvent(true);
private void ImportToDataBase()
{
DataTable dt = GetData();
//int rsCount = dt.Rows.Count;
int rsCount = 100;
this.progressBar1.Minimum = 0;
this.progressBar1.Maximum = rsCount;
this.progressBar1.Step = 1;
int exeCount = 0;
while (exeCount != rsCount)
{
while (obj.WaitOne(100, false) == false) { };
//从dt从获取数据整理后插入数据库
//.......
exeCount++;
this.progressBar1.Value = exeCount;
Thread.Sleep(100);
}
} private void button1_Click(object sender, EventArgs e)
{
Form.CheckForIllegalCrossThreadCalls = false;
Thread thd = new Thread(new ThreadStart(ImportToDataBase));
thd.Start();
} private void button2_Click(object sender, EventArgs e)
{
obj.Reset();
} private void button3_Click(object sender, EventArgs e)
{
obj.Set();
}