progress bar not progressing

前端 未结 4 1466
青春惊慌失措
青春惊慌失措 2021-01-28 07:17

I have a program which is updating a table in sqlserver, I have a form which I want to show the progress of this, the progress bar is incrementing but this is not being displaye

4条回答
  •  悲哀的现实
    2021-01-28 07:43

    Short answer, yes. If you do it from the main thread you won't see the progress bar update.

    Long answer:

    Whenever you modify the user interface from a piece of your code that's translated into a "message" into the window message queue that will get that message and update the UI accordingly in the main thread.

    However, since the main thread is busy handling your piece of code it doesn't have the "time" to actually update the user interface, and only when your process is done, it's free to update the user interface. That's why you see the progress bar going from 0% to 100% without any intermediate steps.

    What you should do:

    What you wanna do is put the work into a background worker, that way the main thread is free to attend UI update requests... that's, by the way, a standard practice if you wanna keep the UI responsive.

提交回复
热议问题