C#利用线程查询数据库数据,出现假死现象
当数据库里数据量大的时候,查询会出现假死现象,代码如下,请各高人指导:
public partial class Form6 : Form
{
public delegate void myDelegate();
DataTable dt;
Thread tSo;
private MMS.Public.DbAccLibry.DbAccess mydbacc;
public Form6()
{
mydbacc = new MMS.Public.DbAccLibry.DbAccess();
mydbacc.DatabaseInint(MMS.Public.DbAccLibry.DataBaseType.MsSqlServer, "Server=192.192.192.8;Database=escashsystem;User ID=sa;Password=Hy@ecSun.cn;Connect Timeout=20;Pooling=True;Min Pool Size=1;Max Pool Size=100;Persist Security Info=False;Asynchronous Processing=true;Integrated Security=false;");
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
tSo = new Thread(new ThreadStart(ThreadWork));
tSo.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString() + ",数据查询失败", "查询提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
void ThreadWork()
{
label2.BeginInvoke(new myDelegate (aa)) ;
this.dataGridView1.BeginInvoke(new myDelegate(FillData));//异步调用(来填充)
//this.Cursor = Cursors.Default;
label2.BeginInvoke(new myDelegate(bb));
}
private void FillData()
{
dt = mydbacc.ExecuteDataSet(CommandType.Text, "select * from ecv_spbaseinfo ").Tables[0];
this.dataGridView1.DataSource = dt.DefaultView;
}
private void aa()
{
this.label2.Text = "正在查询电价数据...";
}
private void bb()
{
this.label2.Text = "查询结束...";
}
}
public partial class Form6 : Form
{
public delegate void myDelegate();
DataTable dt;
Thread tSo;
private MMS.Public.DbAccLibry.DbAccess mydbacc;
public Form6()
{
mydbacc = new MMS.Public.DbAccLibry.DbAccess();
mydbacc.DatabaseInint(MMS.Public.DbAccLibry.DataBaseType.MsSqlServer, "Server=192.192.192.8;Database=escashsystem;User ID=sa;Password=Hy@ecSun.cn;Connect Timeout=20;Pooling=True;Min Pool Size=1;Max Pool Size=100;Persist Security Info=False;Asynchronous Processing=true;Integrated Security=false;");
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
tSo = new Thread(new ThreadStart(ThreadWork));
tSo.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString() + ",数据查询失败", "查询提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
void ThreadWork()
{
label2.BeginInvoke(new myDelegate (aa)) ;
this.dataGridView1.BeginInvoke(new myDelegate(FillData));//异步调用(来填充)
//this.Cursor = Cursors.Default;
label2.BeginInvoke(new myDelegate(bb));
}
private void FillData()
{
dt = mydbacc.ExecuteDataSet(CommandType.Text, "select * from ecv_spbaseinfo ").Tables[0];
this.dataGridView1.DataSource = dt.DefaultView;
}
private void aa()
{
this.label2.Text = "正在查询电价数据...";
}
private void bb()
{
this.label2.Text = "查询结束...";
}
}
...全文
相关推荐
C# WinForm程序处理后台繁忙导致前台控件假死现象解决方法
在C# WinForm应用程序开发中,常常遇到一个问题:当程序执行长时间的后台任务时,如循环处理或定时器触发的事件,可能导致用户界面(UI)变得反应迟钝,甚至出现假死现象。
C# WinForm食堂订餐系统课程设计实战指南
WinForm是高校C#课程设计最主流的桌面开发技术,其核心价值在于轻量、可控、贴近真实工程场景。本文围绕订单状态机设计、三层架构落地、SQL Server并发处理与Service Broker消息同步等关键技术原理展开,突出解决高并发下单卡顿、跨线程UI更新异常、打印小票错位、Excel中文乱码等高频实践痛点。结合MD5加盐密码存储、防重复提交、软删除替代方案等安全与健壮性设计,覆盖从数据库建模、DAL层封装规范到答辩演示话术的完整闭环。特别适配学校机房受限环境(如.NET Framework 4.0锁定
C# WinForm多线程防止界面假死源码
为了解决这个问题,我们可以利用多线程技术,将耗时任务放到后台线程执行,确保UI线程始终保持响应。本文将详细介绍如何在C# WinForm中实现多线程,防止界面假死,并提供一个显示进度的例子。1.
多线程正确调用,避免假死
为了解决这个问题,我们需要利用C#中的多线程机制,特别是委托和事件。委托在C#中是一种类型安全的函数指针,可以用于在多线程之间传递方法调用。
C# Application.DoEvents() 防界面假死例子。
当程序执行耗时的操作(如数据库查询或大型文件处理)时,如果不调用`DoEvents()`,主线程会被阻塞,无法响应用户的交互,导致界面看起来像是假死状态。


