动态进行多个搜索同步怎么操作? BackgroundWorker 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.cnblogs.com/caicaihui/archive/2013/03/17/2964422.html这个例子没有看懂 最简单的,在循环中加一个Application.DoEvents();就可以防止界面失去响应啦。 救命啊,不知道为什么会出现崩溃,搜索结果少的情况不会,搜索结束一遍再按搜索,反复进行发现抛进结果竟然会出现搜索结果数量不一致的情况,再多几遍搜索竟然出现崩溃退出。如果一次搜索结果太多也会出现崩溃退出,不知道是哪里出现了问题。 private List<BackgroundWorker> bws = new List<BackgroundWorker>(); private ArrayList listReasult = new ArrayList(); private StringBuilder SearchType = null; private string searchKey = string.Empty; private void toolStripButton1_Click(object sender, EventArgs e) { if (this.sText.Text.Trim() != "输入多个关键字间用%分开" && this.sText.Text.Trim() != "") { this.sText.Text = this.sText.Text.ToUpper().Trim(); this.sText.SelectionStart = this.sText.Text.Length; this.searchKey = this.sText.Text; this.SearchType = new StringBuilder(); foreach (ToolStripMenuItem tsm in this.toolStripDropDownButton2.DropDownItems) { if (tsm.Checked == true) { if (SearchType.Length == 0) { SearchType.Append("FileType='" + tsm.Text + "'"); } else { SearchType.Append(" or FileType='" + tsm.Text + "'"); } } } if (SearchType.Length == 0) { MessageBox.Show("查询的搜索类型不能为空", "图纸查询", MessageBoxButtons.OK, MessageBoxIcon.Hand); return; } this.toolStripLabel2.Visible = true; this.toolStripLabel2.Text = "Searching..."; this.toolStripButton1.Visible = false; this.toolStripButton2.Visible = true; this.listView.Items.Clear(); this.listView.BeginUpdate(); foreach (ToolStripMenuItem tsm in this.toolStripDropDownButton1.DropDownItems) { if (tsm.Checked == true) { BackgroundWorker search = new BackgroundWorker(); search.WorkerSupportsCancellation = true; search.DoWork += new DoWorkEventHandler(search_DoWork); search.RunWorkerCompleted += new RunWorkerCompletedEventHandler(search_RunWorkerCompleted); bws.Add(search); search.RunWorkerAsync(tsm.Text); } } if (this.bws.Count == 0) { MessageBox.Show("请选择图纸的搜索范围", "图纸查询", MessageBoxButtons.OK, MessageBoxIcon.Hand); } } else { MessageBox.Show("查询的关键字不能为空", "图纸查询", MessageBoxButtons.OK, MessageBoxIcon.Hand); } } private void toolStripButton2_Click(object sender, EventArgs e) { if (this .bws .Count !=0) { this.toolStripLabel2.Text = "SearchCanceling..."; for (int i = 0; i < bws.Count; i++) { this.bws[i].CancelAsync(); } } } private void search_DoWork(object sender, DoWorkEventArgs e) { string con = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + this.Tag + @"\" + e.Argument + ".mdb;Jet OLEDB:Database Password=Pleasecall110"; string sql = @"select FileDate,FileName,FileType,ViewPath,NetPath from [" + e.Argument + "] where (FileName like '%" + searchKey + "%') and (" + SearchType.ToString() + ")"; OleDbConnection dataCon = new OleDbConnection(con); OleDbCommand dataCmd = null; OleDbDataReader dataRead = null; try { if (dataCon.State == ConnectionState.Closed) { dataCon.Open(); } dataCmd = new OleDbCommand(sql, dataCon); dataRead = dataCmd.ExecuteReader(); while (dataRead.Read()) { if (((BackgroundWorker)sender).CancellationPending) { e.Cancel = true; break; } string[] list = new string[5]; list[0] = (string)dataRead["FileName"].ToString(); list[1] = (string)dataRead["FileDate"].ToString(); list[2] = (string)dataRead["ViewPath"].ToString(); list[3] = (string)dataRead["FileType"].ToString(); list[4] = (string)dataRead["NetPath"].ToString(); this.listReasult.Add(list); } } catch { } finally { if (dataRead != null) { dataRead.Close(); } if (dataCmd != null) { dataCmd.Dispose(); } if (dataCon.State == ConnectionState.Open) { dataCon.Close(); } } } private void search_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { bws.Remove(sender as BackgroundWorker); if (bws.Count == 0) { foreach (string[] list in this.listReasult) { ListViewItem lv = new ListViewItem(new string[] { list[0], DateTime.Parse(list[1]).ToString("yyyy-MM-dd HH:mm"), list[2] }); lv.ImageKey = list[3]; lv.Tag = list[4]; this.listView.Items.Add(lv); } this.toolStripButton1.Visible = true; this.toolStripButton2.Visible = false; this.toolStripLabel2.Text = "共搜索到 " + this.listReasult.Count + " 个结果"; this.listReasult.Clear(); this.listView.EndUpdate(); if (this.listView.Items.Count != 0) { string xx = string.Empty; foreach (object key in this.sText.Items) { if (this.sText.Text == key.ToString()) { xx = key.ToString(); break; } } if (xx == string.Empty) this.sText.Items.Add(this.sText.Text); } else { MessageBox.Show("没有查询到结果,请更改查询条件再试!", "图纸查询", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } MVC的的文件下载如何实现 ◆dateTimePicker控件如何进行类似combobox的扁平效果◆ 对access执行删除操作出错,急! 速结 向PDF文档中添加图片 formborderstyle =none问题 C#模式窗体的关闭事件 怎样实现一个简单的智能代码编辑器?识别关键字 新手遇到问题,需要高手解答一下,在线等 winform ado事务处理问题 安装 Crystal Report 9.2 后,Crystal Report Viewe 创建及复制时,VS.NET 时产生问题解疑(新作) 同一个WinService部署多次的问题 C# 窗体中关于控件层次的问题
Application.DoEvents();
就可以防止界面失去响应啦。
救命啊,不知道为什么会出现崩溃,搜索结果少的情况不会,搜索结束一遍再按搜索,反复进行发现抛进结果竟然会出现搜索结果数量不一致的情况,再多几遍搜索竟然出现崩溃退出。
如果一次搜索结果太多也会出现崩溃退出,不知道是哪里出现了问题。 private List<BackgroundWorker> bws = new List<BackgroundWorker>();
private ArrayList listReasult = new ArrayList();
private StringBuilder SearchType = null;
private string searchKey = string.Empty; private void toolStripButton1_Click(object sender, EventArgs e)
{
if (this.sText.Text.Trim() != "输入多个关键字间用%分开" && this.sText.Text.Trim() != "")
{
this.sText.Text = this.sText.Text.ToUpper().Trim();
this.sText.SelectionStart = this.sText.Text.Length;
this.searchKey = this.sText.Text;
this.SearchType = new StringBuilder();
foreach (ToolStripMenuItem tsm in this.toolStripDropDownButton2.DropDownItems)
{
if (tsm.Checked == true)
{
if (SearchType.Length == 0)
{
SearchType.Append("FileType='" + tsm.Text + "'");
}
else
{
SearchType.Append(" or FileType='" + tsm.Text + "'");
}
}
}
if (SearchType.Length == 0)
{
MessageBox.Show("查询的搜索类型不能为空", "图纸查询", MessageBoxButtons.OK, MessageBoxIcon.Hand);
return;
}
this.toolStripLabel2.Visible = true;
this.toolStripLabel2.Text = "Searching...";
this.toolStripButton1.Visible = false;
this.toolStripButton2.Visible = true;
this.listView.Items.Clear();
this.listView.BeginUpdate();
foreach (ToolStripMenuItem tsm in this.toolStripDropDownButton1.DropDownItems)
{
if (tsm.Checked == true)
{
BackgroundWorker search = new BackgroundWorker();
search.WorkerSupportsCancellation = true;
search.DoWork += new DoWorkEventHandler(search_DoWork);
search.RunWorkerCompleted += new RunWorkerCompletedEventHandler(search_RunWorkerCompleted);
bws.Add(search);
search.RunWorkerAsync(tsm.Text);
}
}
if (this.bws.Count == 0)
{
MessageBox.Show("请选择图纸的搜索范围", "图纸查询", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
}
else
{
MessageBox.Show("查询的关键字不能为空", "图纸查询", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
} private void toolStripButton2_Click(object sender, EventArgs e)
{
if (this .bws .Count !=0)
{
this.toolStripLabel2.Text = "SearchCanceling...";
for (int i = 0; i < bws.Count; i++)
{
this.bws[i].CancelAsync();
}
}
} private void search_DoWork(object sender, DoWorkEventArgs e)
{
string con = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + this.Tag + @"\" + e.Argument + ".mdb;Jet OLEDB:Database Password=Pleasecall110";
string sql = @"select FileDate,FileName,FileType,ViewPath,NetPath from [" + e.Argument + "] where (FileName like '%" + searchKey + "%') and (" + SearchType.ToString() + ")";
OleDbConnection dataCon = new OleDbConnection(con);
OleDbCommand dataCmd = null;
OleDbDataReader dataRead = null;
try
{
if (dataCon.State == ConnectionState.Closed) { dataCon.Open(); }
dataCmd = new OleDbCommand(sql, dataCon);
dataRead = dataCmd.ExecuteReader();
while (dataRead.Read())
{
if (((BackgroundWorker)sender).CancellationPending)
{
e.Cancel = true;
break;
}
string[] list = new string[5];
list[0] = (string)dataRead["FileName"].ToString();
list[1] = (string)dataRead["FileDate"].ToString();
list[2] = (string)dataRead["ViewPath"].ToString();
list[3] = (string)dataRead["FileType"].ToString();
list[4] = (string)dataRead["NetPath"].ToString();
this.listReasult.Add(list);
}
}
catch { }
finally
{
if (dataRead != null) { dataRead.Close(); }
if (dataCmd != null) { dataCmd.Dispose(); }
if (dataCon.State == ConnectionState.Open) { dataCon.Close(); }
}
} private void search_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
bws.Remove(sender as BackgroundWorker);
if (bws.Count == 0)
{
foreach (string[] list in this.listReasult)
{
ListViewItem lv = new ListViewItem(new string[] { list[0], DateTime.Parse(list[1]).ToString("yyyy-MM-dd HH:mm"), list[2] });
lv.ImageKey = list[3];
lv.Tag = list[4];
this.listView.Items.Add(lv);
}
this.toolStripButton1.Visible = true;
this.toolStripButton2.Visible = false;
this.toolStripLabel2.Text = "共搜索到 " + this.listReasult.Count + " 个结果";
this.listReasult.Clear();
this.listView.EndUpdate();
if (this.listView.Items.Count != 0)
{
string xx = string.Empty;
foreach (object key in this.sText.Items)
{
if (this.sText.Text == key.ToString())
{
xx = key.ToString();
break;
}
}
if (xx == string.Empty)
this.sText.Items.Add(this.sText.Text);
}
else
{
MessageBox.Show("没有查询到结果,请更改查询条件再试!", "图纸查询", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}