[英]C# GUI is freezing when try to read and decode big files
我已經開發了一個GUI Winforms來讀取和解碼CSV文件並將其保存為Excel和CSV格式。 我在datagridview中顯示解碼的數據。 一切正常,但由於某些怪異的原因,GUI由於大文件而凍結,或者天氣可能很冷。 它仍會執行其應做的操作,但顯示(軟件未響應)。 我也使用過線程和后台工作,但仍然沒有希望。 這是代碼。 請分享任何幫助
public delegate void UpdatingTable();
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Invoke(new UpdatingTable(DecodingData));
}
private void cnvrtB_Click(object sender, EventArgs e)
{
progressBar1.Enabled = true;
progressBar1.Visible = true;
saveB.Visible = true;
cnvrtB.Enabled = false;
label5.Visible = true;
backgroundWorker1.RunWorkerAsync();
//DecodingThread = new Thread(new ThreadStart(StartDecoding));
//DecodingThread.IsBackground = true;
//if (!(DecodingThread.IsAlive) || DecodingThread == null)
//{
// progressBar1.Enabled = true;
// progressBar1.Visible = true;
// saveB.Visible = true;
// cnvrtB.Enabled = false;
// label5.Visible = true;
// Thread.Sleep(2000);
// DecodingThread.Start();
//}
//else if (DecodingThread.IsAlive)
//{
// progressBar1.Enabled = true;
// progressBar1.Visible = true;
// saveB.Visible = true;
// cnvrtB.Enabled = false;
// label5.Visible = true;
// DecodingThread.Resume();
//}
}
private void StartDecoding()
{
Thread.Sleep(1000);
Invoke(new UpdatingTable(DecodingData));
}
public void DecodingData()
{
try
{
//delete old records
records.Clear();
//get records from converter
if (VersionNumber == 1)
{
records.AddRange(LogEventDecode.getRecords());
}
else if (VersionNumber == 0)
{
records.AddRange(LogEventDecode.getRecordsOld());
}
//create datatable for records
table = new System.Data.DataTable("data");
//create columns
table.Columns.Add("Time", typeof(System.DateTime));
table.Columns.Add("Date", typeof(System.DateTime));
table.Columns.Add("dt", typeof(System.DateTime));
table.Columns.Add("User", typeof(System.String));
table.Columns.Add("SourceInformation", typeof(System.String));
table.Columns.Add("SourceType", typeof(System.String));
table.Columns.Add("SourceCondition", typeof(System.String));
table.Columns.Add("securityLevel", typeof(System.String));
table.Columns.Add("AdditionalInformation", typeof(System.String));
table.Columns.Add("RubCondition", typeof(System.String));
//populate datatable
foreach (LogRecord r in records)
{
DataRow row = table.NewRow();
row["Time"] = r.Time;
row["Date"] = r.Date;
row["dt"] = r.dt;
row["User"] = r.User;
row["SourceInformation"] = r.SourceInformation;
row["SourceType"] = r.SourceType;
row["SourceCondition"] = r.SourceCondition;
row["securityLevel"] = r.SecurityLevel;
row["AdditionalInformation"] = r.AdditionalInformation;
row["RubCondition"] = r.RubCondition;
table.Rows.Add(row);
//DecodingCount++;
//label5.Text = "Decoded" + DecodingCount + " out of Total Logs " + records.Count;
}
view = new DataView(table);
//bind to grid
//DecodingThread.Abort();
}
catch
{
MessageB.Show("Please make sure you have Imported the File \nOR\n The imported file is not corrupted.", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
cnvrtB.Enabled = true;
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
logRecordBindingSource.DataSource = view;
cnvrtB.Enabled = true;
progressBar1.Visible = false;
progressBar1.Enabled = false;
label5.Visible = false;
}
給定的示例同時顯示了線程和后台方法
謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.