简体   繁体   中英

C# winform backgroundworker

I am currently working on a home project for myself. The program is written in C# using winforms.

The problem I'm currently experiencing is as followed:

I have a listview in my mainform called lvwGames When I run the program without debugging, it runs fine. However when I start with a debug, I get an error. This has something to do with the background worker thread.

Allow me to post some code to assist me.

    private void MainViewLoad(object sender, EventArgs e)
    {
        RefreshGamesListView();
    }

Nothing special here. The reason I am calling RefreshGamesListView() is because I have to refresh on several occasions.

The method being called looks like this.

    public void RefreshGamesListView()
    {
        pbRefreshGamesList.Value = 0;
        bgwRefreshList.RunWorkerAsync();
    }

So when the method is called, the background worker is called and runs the dowork method. This one is quite big.

    private void BgwRefreshListDoWork(object sender, DoWorkEventArgs e)
    {
        List<Game> games = _mainController.RetrieveAllGames();

        int count = 1;
        foreach (Game game in games)
        {
            string id = game.id.ToString();
            var li = new ListViewItem(id, 0);
            li.SubItems.Add(game.title);
            li.SubItems.Add(game.Genre.name);
            li.SubItems.Add(game.Publisher.name);
            li.SubItems.Add(game.Platform.name);
            li.SubItems.Add(game.CompletionType.name);
            li.SubItems.Add(game.gameNotice);
            lvwGames.Items.Add(li);

            double dIndex = (double)(count);
            double dTotal = (double)games.Count;
            double dProgressPercentage = (dIndex / dTotal);
            int iProgressPercentage = (int)(dProgressPercentage * 100);

            count++;
            bgwRefreshList.ReportProgress(iProgressPercentage);
        }
    }

When i run the code in debug, when the code is on lvwGames.Items.Add(li); It gives me the following error:

Cross-thread operation not valid: Control 'lvwGames' accessed from a thread other than the thread it was created on.

I have absolutely no clue why. I think it is code specific. But it can also mean I don't get the background worker completely, and specifically when to use it properly.

The reason I'm using it is because I'm loading a large large list from the database, I want to keep responsiveness in the UI when the list is loaded, and inform the users how far it is, using a progress bar.

If any code is missing, or you actually understand why this is happening PLEASE explain me why in this case its causing the error. You don't need to fix it for me. I just want to know WHY it's caused.

Thanks for taking the time to read this post. I hope to be able to continue using the debugger soon. :)

You need to call Conrol.Invoke when accessing visual controls from background threads.

if (_lvwGames.IsHandleCreated) {

    Action addGameToList = () => {
        string id = game.id.ToString();
        var li = new ListViewItem(id, 0);
        li.SubItems.Add(game.title);
        ....
        _lvwGames.Items.Add(li);
    };

    if (_lvwGames.InvokeRequired) {                        
        _lvwGames.Invoke(addGameToList);
    } else {
        addGameToList();
    }
}

From Manipulating Controls from Threads

...For example, you might call a method that disables a button or updates a display on a form in response to action taken by a thread. The .NET Framework provides methods that are safe to call from any thread for invoking methods that interact with controls owned by other threads. The Control.Invoke method allows for the synchronous execution of methods on controls...

This happening cause, just like compiler cliams, you are going to update UI control content from another thread. You can not do that, as UI control can be updated only within main thread.

Please have look on this SO answer with example code provided:

Invoke from another thread

This is because you're attempting to access a UI control ( lvwGames ) from a background thread. The way to make it work requires you to marshal the information back to the main UI thread and update the control from there:

private void BgwRefreshListDoWork(object sender, DoWorkEventArgs e)
{
    List<Game> games = _mainController.RetrieveAllGames();

    int count = 1;
    foreach (Game game in games)
    {
        string id = game.id.ToString();
        var li = new ListViewItem(id, 0);
        li.SubItems.Add(game.title);
        li.SubItems.Add(game.Genre.name);
        li.SubItems.Add(game.Publisher.name);
        li.SubItems.Add(game.Platform.name);
        li.SubItems.Add(game.CompletionType.name);
        li.SubItems.Add(game.gameNotice);

        // This is the new line you need:
        lvwGames.Invoke(new MethodInvoker(delegate { lvwGames.Items.Add(item) }));

        double dIndex = (double)(count);
        double dTotal = (double)games.Count;
        double dProgressPercentage = (dIndex / dTotal);
        int iProgressPercentage = (int)(dProgressPercentage * 100);

        count++;
        bgwRefreshList.ReportProgress(iProgressPercentage);
    }
}

Normally you would check the InvokeRequired property first as mentioned in other answers, but there is really no need if you are always calling it from the background thread. Your DoWork method will always require an invoke call, so you might as well just go ahead and write it like that.

The background worker is not working properly if you run in debug mode in studio. If you have calls that use the windows handle to retrieve messages, then they will fail. If you for instance have a progressChanged event handler and this changes a text in a textbox that might fail.

I had this scenario: A Form that has a background worker. If I just start the worker without getting a dialog box up first then it works ok. If I show a dialog and then start the background worker then it fails. When I run the program normally it does not fail. It is somehow the debug environment that destroys the link between the events and the foreground window. I have changed my code to use invoke, and now all works both in when running in release and when I debug.

Here is a link explaining what can be done to make a program thread safe. http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx

I did not do the same as the sample to microsoft. I made delegates, assigned to the functions I needed to run. and called invoke on them.

sample pseudo code:

class MyClassWithDelegates
{
    public delegate void ProgressDelegate( int progress );
    public ProgressDelegate myProgress;
    public void MyProgress(int progress)
    {
         myTextbox.Text = ..... ; // this is code that must be run in the GUI thread.
    }

    public MyClassWithDelegates()
    {
      myProgress = new ProgressDelegate(MyProgress);    
    }
    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        Invoke( myProgress, e.ProgressPercentage );
    }

}

All code that potentially have to be run in the GUI thread of the application must be Invoked to be safe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM