WPF progress bar update with dispatcher

前端 未结 3 1387
梦谈多话
梦谈多话 2021-01-06 10:12

I am trying to update a progressbar using the dispatcher but somehow cannot think where to place the dispatcher.Invoke and what to pass inside it.

Im trying to impor

3条回答
  •  孤独总比滥情好
    2021-01-06 10:52

    I am assuming that you've started a background thread to import the files. You should consider using BackgroundWorker for this, which is lightweight and has a mechanism built in to report progress (e.g., a ProgressBar) using an event.

    If you want to use a new thread, anywhere within the processing that you are doing, just declare a delegate, add a function to target, and call Dispatcher.BeginInvoke:

    Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateProgressDelegate(UpdateProgress), myProgressData);
    
    //...
    
    private delegate void UpdateProgressDelegate(ProgressClass progressClass);
    
    void UpdateProgress(ProgressClass progressClass)
    {
        progressbar.updateprogress(progressclass);
        progressbar.show(); 
    }
    

提交回复
热议问题