c# 2.0 - How to use Progressbar in Button Click without BackGroundworker -


I am working with C # .net, a Windows application, I use the Progressbar on the click button without backgainWorkwork Any suggestions, thanks in advance ...................., I tried to use the thread, for loop intrusion

Quick answer "You can not do" is close.

The long answer is:

Theoretically you just start a progress bar, and then update it in () loop.

However, it does not work very well in practice. The reason for this is that when you are in your loop, your application is "busy" and it does not respond to messages from windows coming from the OS. The OS will look into it and decide that you are becoming unresponsive, so it takes a snapshot of your window. From that point, whenever you update your progress bar, the OS only shows the snapshot instead of the current situation, so where the user is concerned, the progress bar (for example) went up to 35% and then "stuck" . It completely defeats the point of having a progress bar!

To fix this, your UI thread process should continue in the message, it can be done in two ways.

1) Use a different thread for processing work, and ask the UI thread to update progress bar. This is the "background worker" approach, and it works well.

2) You can call application.DoEvents () from within your free () loop. This message allows you to be transmitted when "busy" and thus avoids the problem Howeve, R. It is extremely dangerous because processing messages can re-enter your code (for example, if you want to start processing If you click on the "Go" button, the user will be able to click on "again" and you will try to do some processing inside your processing). To fix this, you have to be cautious, to ensure that you only process "safe" messages in this context, installing a message filter. Do not try this approach unless you really know what you are doing.

So who leaves you with the same practical perspective: Use a backgroundwork (or your own thread) on the web, there are a lot of tutorials on how to do that, Not detailed.


Comments