Click here to Skip to main content
15,794,275 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
HI..

I'm using background-worker but still it freeze UI in windows Application.

C#
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    try
    {
        Action DoCrossThreadUIWork = ()
        {
            CreateDataGridView();   // Method Create dataGridView and Handling Some GridView Events (e.g dataGridView1_Paint)

            DayScheduleTable = clsSqlQueries.ExecuteQuery(clsSqlQueries.GetDaySchedule); // Read Table From DataBase
            FillDataGridView();   // Fill DataGridView
        };
        this.BeginInvoke(DoCrossThreadUIWork);
    }
    catch (Exception ex)
    {
        clsWriteLog.GetInstance().WriteErrorLog(clsPublicMethod.GetInstance().GetMethodName(), ex.Message);
    }
}



private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
}



And Call background worker in Form_load Event As:
C#
if (!backgroundWorker1.IsBusy)
                  backgroundWorker1.RunWorkerAsync()

If have any idea please share..
Posted
Updated 9-Nov-14 20:41pm
v2
Comments
dan!sh 10-Nov-14 1:40am    
Why are you handling Paint event in another thread? This is not the purpose of a background worker. You might want to rethink your design.
Member 11055395 10-Nov-14 19:33pm    
Paint event is runtime assigned to gridview inside method which handled in background worker. so is paint event not in the background thread ?
Kornfeld Eliyahu Peter 10-Nov-14 1:52am    
As all UI is a separate thread, there is a major problem to handle UI elements from the thread of the BackgroundWorker...It will mix non UI related windows messages on the UI thread and that blocks UI related messages (UI related messages have the lowest priority in message queue)...
Sergey Alexandrovich Kryukov 10-Nov-14 2:39am    
Not clear. You don't show what CreateDataGridView and FillDataGridView do. Most likely, they should also be invoked to UI thread.
—SA
Member 11055395 10-Nov-14 19:29pm    
CreateDataGridView Method Add Column in the GridView and also Assing Runtime GridVirew Paint Event inside this method.
FillDataGridView Method adds Rows in the GridView.

1 solution

Right from the start, I think the reason it's hanging is because your progress bar is likely OWNED by another thread. Like others have stated, you might want to reconsider your design here.

A way to invoke updates to a GUI element made by another thread is to make an invoker delegate. The following code illustrates an update method to a label which is designed to display the current time.

Also, a matter of personal preference is that anonymous threads are like loose canons. They're great for executing some very simple, one-time operation. But if it's to continuously update a UI element, I'd advise against their use in this case.



private delegate void StatusCallbackString(string someText);
.
.
.

private void updateUITime(string time)
{
    if (lblTime.InvokeRequired)
    {
        StatusCallbackString sCB = new StatusCallbackString(updateUITime);
        this.BeginInvoke(sCB, new object[] { time });
        return;
    }

    lblTime.Text = time;
}


Meanwhile, in a background thread somewhere...


private void MyThreadedOperation()
{
    while(keepThreadAlive)
    {
        updateUITime(DateTime.Now.ToString());

        .
        .
        // Sleep nicely for 1 second...
        System.Threading.CurrentThread.Join(1000);
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900