Я разработал winforms GUI для чтения и декодирования файла CSV и сохранения его в формате Excel и CSV. Я показываю декодированные данные в datagridview. Все работает нормально, но для какой-то freaking причине GUI замерзает из-за больших файлов или может быть погода здесь холодная. Он по-прежнему делает то, что он предполагает, но показывает (программное обеспечение не отвечает). Я также использовал Thread и фоновую работу, но у меня пока нет надежды. вот код. пожалуйста, поделитесь какой-либо помощьC# 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;
}
в данном примере отображается как пронизывающий фон и метод
Благодаря
Где вы звоните StartDecoding из? У этого есть Сон, который бы заморозил UI, если вызван из потока пользовательского интерфейса –
, почему вы вызываете 'Invoke' в фоновом работнике? – KMoussa
вызов invoke для обновления таблицы –