c#界面卡死處理方法
阿新 • • 發佈:2017-09-02
logs def 屬性 earch etl mar 處理 bsp round
方法一:
設置屬性:
Control.CheckForIllegalCrossThreadCalls = false;
開啟一個新線程
Thread th = new Thread(() => Search(detailID.Trim())); th.IsBackground = true; th.Start();
public void Search(string detailID) { dataGridView1.DataSource =BLLBillConsume.BllGetClearMarketLog_detail(detailID); dataGridView1.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight;//文本向右對齊 }
上面的方法不推薦,推薦以下方法:
拖一個組件backgroundWorker
設置屬性:
backgroundWorker1.WorkerReportsProgress = true; backgroundWorker1.WorkerSupportsCancellation = true;
執行用時較長的操作,
backgroundWorker1.CancellationPending是用來判斷是否允許用戶取消
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { dtMain = BLLBillConsume.BllGetClearMarketLog_detail(detailID); if (!backgroundWorker1.CancellationPending) {//退出 backgroundWorker1.CancelAsync(); } }
Thread.Sleep(100000);只是為了模擬界面卡死
然後就顯示查詢到的數據
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { dataGridView1.DataSource = dtMain; dataGridView1.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight;//文本向右對齊 //Search(detailID.Trim()); }
這個是開啟線程
backgroundWorker1.RunWorkerAsync();
c#界面卡死處理方法