Background desktop and data table issues

Hi everyone, I have a working BAckground and Datatable. I also have a timer. I populate the data table in the timer, and in Backgroundworker_Progress changed. I view it in my DataGrid as my DataSource. But even after the completion of the process. My desktop does not get the Completed.Due to which my application crashes. This only happens when I run exe directly

+3
source share
2 answers

I agree with @Simon. Paste some code so that we understand what might be wrong. Also, why are you using a timer for?

Do not assign an event DataTableto ProgressChanged. Do it in the event RunWorkerCompleted. Here is what I think you should do:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    try
    {
        e.Result = GetTableData();
    }
    catch (Exception ex)
    {
        e.Result = ex;
    }
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // only display progress, do not assign it to grid
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Result is DataTable)
    {
       dataGridView1.DataSource = e.Result as DataTable;
    }
    else if (e.Result is Exception)
    {
    }
}

private DataTable GetTableData()
{
    DataTable table = new DataTable();
    for (int i = 0; i < NumOfRows; i++)
    {
        //... fill data here
        backgroundWorker1.ReportProgress(i * 100F / NumOfRows);
    }
    return table;
}
+3
+1

Source: https://habr.com/ru/post/1734689/


All Articles