Winform中,先看代码
private void startSearchDraws() { this.listView.Items.Clear(); dSearchWorker = new dataDrawSearch.mThreading(); dSearchWorker.mBackWoker = new BackgroundWorker(); dSearchWorker.mBackWoker.WorkerReportsProgress = true; dSearchWorker.mBackWoker.DoWork += new DoWorkEventHandler(mBackWoker_DoWork); dSearchWorker.mBackWoker.ProgressChanged += new ProgressChangedEventHandler(mBackWoker_ProgressChanged); dSearchWorker.isCancel = false; dSearchWorker.sKey = this.sText.Text; dSearchWorker.mBackWoker.RunWorkerAsync(); } private void mBackWoker_DoWork(object sender, DoWorkEventArgs e) { dataDrawSearch.SearchDraws(dSearchWorker); } private void mBackWoker_ProgressChanged(object sender, ProgressChangedEventArgs e) { switch (e.ProgressPercentage) { case 0: this.toolStripStatusLabel1.Text = string.Format("检索结束,共加载了 {0} 条记录,用时 {1:00}.{2:00} 秒", this.listView.Items.Count, ts.Seconds, ts.Milliseconds / 10); dSearchWorker.mBackWoker.Dispose(); break; default: ListViewItem lvi = (ListViewItem)e.UserState; this.listView.Items.Add(lvi); this.toolStripStatusLabel1.Text = string.Format("正在检索图纸... 已加载 {0} 项", e.ProgressPercentage); Application.DoEvents(); break; } }
dataDrawSearch类里面
public class mThreading { public System.ComponentModel.BackgroundWorker mBackWoker; public string sKey; public bool isCancel; } public static void SearchDraws(mThreading ddsWorker) { string searchKey = string.Empty, dataBase = string.Empty; searchKey = .....//这里省略过程 dataBase = .....//这里省略过程 using (MySqlConnection msCon = new MySqlConnection(userInfo.myConnection)) { string sql = string.Format("SELECT * FROM ({0}) AS TEMP WHERE {1}", dataBase, searchKey); try { if (msCon.State == ConnectionState.Closed) { msCon.Open(); } using (MySqlCommand msCmd = new MySqlCommand(sql, msCon)) { using (MySqlDataReader msRead = msCmd.ExecuteReader()) { int i = 0; while (msRead.Read()) { if (ddsWorker.isCancel) { break; } i += 1; ListViewItem lvi = new ListViewItem(new string[] { msRead["名称"].ToString(), msRead["日期"].ToString(), msRead["范围"].ToString() }); lvi.Tag = msRead["路径"].ToString(); lvi.ImageKey = msRead["类型"].ToString(); ddsWorker.mBackWoker.ReportProgress(i, lvi); } } } } catch { } } ddsWorker.mBackWoker.ReportProgress(0); }
在数据几个几十个时不会出错,但是数据到200多左右的时候出会出现那个错误,本人开始以为SQL语句出错了,本人就在SQL语句那调试把SQL语句复制后直接在MysSql Workbench里面执行,可以正常得到结果,有1000多条记录。
解决方案
40
莫非是你的Application.DoEvents()太频繁,然后才出这问题……本人也不懂……这算不算误打误撞……