Im creating a c# windows form and ive come accross a problem.
When the window opens I am wanting to run a bit of code which can make the front console appear to freeze. The code runs fine but I want to show the status of the program in the status strip at the bottom of the page. I am running the code in the action Form.Shown. However the code does not update the status bar until everything is shown. I can change the label no problem.
How would I go about loading the window and then running the code and updating the status bar (like a background task)?
What areas would I need to look at to get this information?
You could use a BackgroundWorker to do this
Set the WorkerReportsProgress property to true and in the DoWork eventhandler raise the ReportProgress event
in the eventhandler for the ProgressChanged place your logic to update your progresbar
there is a example on msdn
You can use Multithreading to avoid freezing the forms. It means, you separate your form and the code (that you want to run in parallel) in different threads so that the form doesn't wait for the code completes. You can monitor the progress of the code via events.
Related
I’ve created a window in WPF to display the status of the currently running operation.
The window is run on a separate thread, derived from this: Multiple Windows, Multiple Threads example.
The first thing I did was to set the Owner of my status window, since I want it to be displayed at the parent owner. It should be minimized together and not hidden behind the owner. I have used WindowInteropHelper to set the owner.
And here is the problem: while the main thread is busy, and not reporting any progress for a while, the status display window stops responding as well. How can I keep the child window responsive while its owner is busy?
It sounds like your doing all over your heavy work on the UI thread. You need to off load this to a new thread then call back to the UI thread with updates to your status window. This will ensure that your status bar remains responsive while the work is being done in the background.
There is an example for this in the section above the windows example in the link you provided.
See Handling blocking operations
If you want something like a progress bar you will need to decide at which stage to advance the bar instead of just knowing when everything is complete.
I need to have a progress bar in a popup window, while my main thread is doing some heavy calculations.
I'm using an Oracle API , all the heavy work is done calling 1 function of this API, so I can't use the backgroudworker update becasue I don't have the API's code.
I tried to create a new form with an ProgressBar and a Timer, but the timer_Click event is never fired!!(???)
Any Idea??
while my main thread is doing some heavy calculations.
Wrong. Never do any heavy calculations inside your main thread in a Windows application or your users will simply hate you.
If your API doesn't provide a way to report the progress then the best you could do is show some rotating animation while the calculation is in progress because you will not be able to know the exact percentage of work done. So put the calculation inside a BackgroundWorker, show some spinning animation before running the calculation (which should itself be done inside the DoWork event) and hide the animation once the work is done (RunWorkerCompletedEvent).
The ProgressBar has a Style property that you could set to Marquee to emulate an infinite progression.
I have a MDI form in which, once I click on any menu item, the associated form is shown. Sometimes, forms take time to load, so I would like to show a progress bar for long-running calls.
Since few forms take time to open, you should be moving the form loading code into a separate thread using background worker(This will avoid UI from freezing). You can have WorkerReportsProgress property set to true, so that you can use it to show the progress in the progress bar in ProgressChanged event handler, while the loading.
If you do not have much idea about how to work with Background worker, I recommend you learn how to use them --- See this
Although this is a VB.NET example, the essential part is to get the idea, so I would recommend this excellent article that answers your question:
http://www.dreamincode.net/forums/topic/58239-progress-bar-while-loading-a-child-form/
PS: It would not hurt to google it first.
I have a WinForms application, and wanted to add some nice WPF controls, one of them being an 'indeterminate' progress bar.. which just animates nicely until I tell it to stop.
I have done this, and was racking my brains as to why it wasn't animating (changing the value of the progress bar).
I eventually tried showing my form (containing the ElementHost) modally, and hey presto, it animates, but I want to show the form non-modally, as I want to continue processing behind the scenes.
Is there some kind of setting that tells the ElementHost window to continue 'animating'?
Thanks
Rich.
There are many ways to do this , the simplest one is to use a backgroundworker for the lengthy task. The Backgroundworker has an event to report progress. Handle this event and in the handler change your progressbar's value. Just having an animation in the main thread while still doing work on the main thread will not work right ...
One other aproach you may try (though is not what I would recommend for a healty app) is to implement a DoEvents function and call it in the main thread when you want the progress bar to get updated ... Here is the link for the DoEvents implementation:
MSDN DoEvents sugestion
I would Strongly recommend the first approach though
As I mention in my comment above, the solution is to run the processing in a separte thread, which allows the .net Main GUI thread do its stuff, and animate the progress bar.
following scenario. I have a datagridview and a progressbar, datagridview is being filled with some datas from active directory. Filling my datagridview takes about 3 minutes. Now, while my program is working everything is fine until I switch windows - like for example open web browser or anything else. When I swich back to my program the whole window is "frozen", progressbar is not showing any progress until filling is completed. How can I refresh my datagridview and progressbar so that something like this doesn't happen anymore? I've tried datagdidview.Invalidate() and Refresh() run every 5s (via Timer, yes it was enabled) but it doesn't work. What am I missing here?
Thanks!
You should fetch the data in a separate thread (e.g. using BackgroundWorker or a new thread), marshalling back to the UI thread (with BackgroundWorker.ReportProgress on Control.Invoke/BeginInvoke) when you need to interact with the UI. See this article for more information and sample code.
You need to run the UI and work methods on different threads otherwise the UI will lock when your datagridview is working.