简体   繁体   中英

How to update dataset.datatable bound to datagridview via another thread

Hi I have a dataset that contains a few tables. One of these tables is bound to a bindingsource which is then to a datagridview.

The dataset table is updated via a single background thread as it fetches the data via http

This obviously causes the datagridview to throw a cross threading error as its being updated from a different thread.

My question is how to I get around this without doing a complete rewrite? I realise I could have the UI thread update the dataset from some sort of buffer that the background thread creates but thats starting again and I would like to check there is not a simpler way that Ive missed. Most of the examples I see on here seem like I need to start again.

I'm assuming you are not using async/await.

You can use Invoke() to run something on the UI thread.

Task.Run(new Action(() =>
{
    // << do http call >>

    this.Invoke(new Action(() =>
    {
        //manipulate grid here.....
    }));
}));

Ideally you would be using async/await. It is kind of a waste to run IO bound code with Task.Run.

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