How to use Progress bar during loading an xml File

前端 未结 4 1902
伪装坚强ぢ
伪装坚强ぢ 2021-01-06 05:32

I want to show progress bar during loading a Remote xml file. I am using Windows Application Form in Visual C# 2008 Express Edition.

private void button1_Cl         


        
4条回答
  •  逝去的感伤
    2021-01-06 06:02

    The progress bar doesn't work here, because this is all "synchronous" code. The Click event blocks the UI from updating.

    There are 2 solutions.

    You can create a background thread to process the XML file, and the background thread reports progress to the foreground. I'd recommend using a BackgroundWorker, but there's a lot to learn before you do this.

    An easier solution, although not optimal, is to refresh the form by calling Application.DoEvents() after each progress update. This should only be considered a short-term solution, because if the task isn't quick, then your application could appear to freeze.

    Both solutions have a drawback - when your progress bar is refreshed, your application will also process any events. Therefore, you must manually disable your UI while the task is processing, and re-enable it when finished.

    A while back, I created a reusable solution for this issue. I created a "ProgressDialog" that takes a delegate. The progress dialog executes the delegate, and captures the progress, updates its progress bar, and even calculates the time remaining. The benefit of using the ProgressDialog is that I can show the dialog in "modal" mode, which blocks the access to the main UI, and even prevents the Click event from finishing.
    Unfortunately, I don't have any code to share, just the idea.

提交回复
热议问题