I'm building a Windows Form application that manipulates a text file, the manipulation process takes time, so I creadted a Hidden PictureBox Control and I put an animated gif Image in it (Like a Progress Bar), and once the process starts I show the PictureBox, but the gif doesn't move cause it is on the same process I guess, is there any kind of solution...
Thanks
You should be doing your long processing in another thread, or simply by using a Background Worker.
msdn: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
Plus, you can report any progress made to a progress bar...
Here's an example of use http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx
Note that one of the main reasons why your GUI won't update when you're doing heavy computing, is the fact that the computing is done on the same thread as the UI in running on.
One solution is to use Threads, you fired tle process in one thread, then you show the Image... Here you have an excelent guide for beginners in Threads, Beginners Guide to Threading.
But onther easy solution, is to show the picturebox before the process start, so the picture shows in your form before the process take the control of the main Thread.
Related
There is a form. On the form there is a pictureBox docked on all form's surface.
When app starts, for a second a form with white surface is displayed, then
the picture is shown.
how can i get rid of that 1s white form?
Sounds like you are doing something time consuming in form_Shown event. Call Form's Refresh() method as the first thing in form shown -event and it will first draw the form, then do the time consuming things
You have not stated when you are loading the picturebox with your image. But I would try making your picturebox visible at the end of your Form_Load event or in your Form_Shown event.
Sounds more like a threading problem to me. I guess that your UI thread is doing too much work and cannot update the UI often enough.
Do all of the following:
Make sure loading and processing any data (including the images) is NOT located in the constructors.
Move that code into the appropriate FormLoad() event handler methods.
Implement loading of the images so that it runs a separate thread.
You can find some advice in this MSDN article: Give Your .NET-based Application a Fast and Responsive UI with Multiple Threads
If you are working in a .NET 4.x version, you can also use the Task Parallel Library to make working with multiple threads easier.
The upcoming .NET 4.5 also offers the even more comfortable await and asyc keywords: Asynchronous Programming with Async and Await.
I have Winform application,which does some operation in loops (file access, copy file, ....). During theses operation, the application freeze completely, the job is done but impossible to move the main window or refresh the RichTextBox information (we display errors and the job in progress).
Do you have an idea how to do this ?
Thanks,
Consider using BackgroundWorker.
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
It can both run your code in the background and report progress back to to the main
UI thread.
i suggest that you use a Background Worker and let the worker do the loops. i think your application read a file by line and added to the rich textbox control right?
this article will guide you in using BackGround Worker. or this
You can create separate thread and execute other task or may use background worker thread.
I have written an application which does lots of calculation on huge floating point numbers that makes the UI not responsive at all most of the time.
I am thinking of adding a status bar to this application and show some info about cpu load, used memory and a progress bar. Consider adding labels and progress bar to the statusbar as childs, how can I run this status bar on a separate thread which can be reliable to be responsive as much as possible?
I can already use progress bars and system diagnosting stuff normally. What I am looking for is your ideas and tips, possibly with some codes!
Update
I want the status bar shows real time cpu and memory details. How to workaround this?
You've got this the wrong way round. You should run all the UI in the same thread, and run the long calculation in a background worker. Trying to run UI in different threads within the same app just leads to pain.
Best thing is to just do a google search on BackgroundWorker. Here is one particular result on how to use it.
http://midnightprogrammer.net/post/Using-Background-Worker-in-C.aspx
I have a program which does some copy jobs (via File.Copy) that could last a few minutes. When the user brings another window to foreground in this time the program window gets blank since it doesn't redraw itself.
Now I want to show a ProgressBar in an extra window in the center of the screen, but that window get's blank too.
So I startet it in another thread which didn't help.
I'm quite sure someone did this before but I cannot find a working example. Any Ideas?
There is nice example in the documentation on BackgroundWorker class.
ChrisF is correct. Your long operation should be done in a BackgroundWorker thread. You can use BackgroundWorker to report progress, and hook that up to a progress bar on your form.
You need to thread your operation using a BackgroundWorker. There are other ways to thread the operation, but this one is probably the simplest, and will continue to perform message pumping in the foreground so Windows doesn't think your application has stopped responding.
Another option would be to use a Thread, and use Thread.Join to wait for the background task to complete, since Thread.Join also sends standard message pump information to Windows while it waits.
You can use BackgroundWorker class.
See this answer.
(WPF) We have been asked to build a little window that pops up when the app is busy (instead of or in addition to a wait cursor). We put a textblock in the window, and a ProgressBar with IsIndeterminate = true.
We call show on the little window, and then start up our long-running process, calling Close on the little window when we are through. However, during the long-running process, the ProgressBar does not show any activity, and the little window shows (Not Responding) in its title.
Is this even possible? A better idea?
You need to look into using another thread (or multiple threads) to do the heavy processing that could take longer than 100ms (100ms and above can cause this 'hanging' appearence before 'Not Responding' appears.)
You could create a new thread using a BackgroundWorker
object and subscribe to the OnProgressChanged event to indicate a progress bar update. This does not however get around the problem of accessing the main (UI) threads Progress Bar. You would have to check if an Invoke is required and then invoke a piece of code responsible for updating the Progress Bar.
Here is a StackOverflow question that shows a nice Extension Method in the accepted answer for invoking a method call on a control:
Invoking
Hopefully that helps!!
The problem is most likely that whatever is causing the app to be "busy" is being run in the main thread of the app. So, the app stops responding, including responding to layout and painting requests from the OS. So, when the app is "busy", it's too busy to paint the new window.
The solution is to move as much "heavy lifting" as possible into background threads, so the main thread and thus the UI remain responsive.