I've run into a situation with loading content from an xml file. Basically there can very well be hundreds of items in one of these xml files at a time. Now when I navigate to a page I have to databind these objects to a listbox control and display them to a user. Now I was using the OnNavigatedTo Event and was loading the content using LINQ. The problem was as more items were added to the xml file, the page started taking longer to load.
Then I waited until I navigated to the page and it was displayed to call the xml file, but the UI became unresponsive for about a second and a half.
So the thought that came to my mind was to see if there was someway to load the xml file on a background thread so that it doesn't affect the UI. Is this possible, and if so can you point me to a resource where I can get some more info.
BackgroundWorker is exactly what you need: http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx
Basically, it executes the DoWork method in a background thread, then executes the WorkerCompleted method where you can update the UI with the result of the calculations.
Related
I can't seem to figure out how to use either the threading or Backgroundworker task. My problem is that I am loading a large XML file into a TreeView and would like to display a progress bar while doing so. The issue right now is that the app freezes when it's loading and says the app is non-responsive.
Threading / Background worker tasks is new to me so I am not exactly sure how to make this work.
First we will start with my button to get the user details
So we get the users's details and call CreateXML for the user detail object. So we have the XML in one big string called strXML. Then I call dom.LoadXml to load xml sting into the dom object. Then we initialize the tree view control and add the nodes etc.
So during this whole process the app hangs and indicates that it's not responding while it's busy churning thought the XML and creating it into a treeview. I want to put in a progress bar so the end user see's that something is actually going on and they don't think the app just died on them.
How would i implement some sort of progress bar with a thread or back ground worker?
Please just don't redirect me to link after link because that never really works for me.
Thanks
I was able to implement multi-threading which then I was able to achieve the progress bar moving while the data was being loaded. Sometimes people need to wait for data to be loaded and a simple indicator is all that's needed so users know the app is working.
I am not sure how should I put the title of my question, But I have one issue in Window based application C# code.
basically I am creating 2 WPF application in C#
For my First application, following steps are there
A. I am Creating some folders by C# code and showing status on a TextBox.
B. Then I am unzipping a folder by C# code and Copying files to a new folder location in C drive. This time also I have to show the status in TextBox.
But there is a problem with second step. Copying files blocked the first step to show the status in TextBox. I was hoping that It should show the status in TextBox first for the first step then it should start the coping files. Right now it shows the complete message in TextBox, after coping the files. :(
For my second application, I load XML with more then 3000 data in a List. So Loading and displaying the items in the list, block the entire application. So how could I resolve this?
I am new in .net, so please help me in that.
You need to use Threads or Background Workers to accomplish this task. When you do some work in single application, your UI will get block till some Long Task get completed [in your case updating your text box].
Also to access UI components not inside your own thread [Original thread] you will need Invokes. Go through guides and try to adapt techniques in them. In my experience "Background workers" are easy to work with.
Guides -
Backgorund workers
Invokes
Updating UI- in your case show progress
When a browser is opened, before it's completely loaded, we can use the controls as others are being loaded (the address bar appears and, while the bookmarks are loaded, we can already type in it).
I'm making a personal browser, and I don't know how to perform that. I imagined creating the controls in another thread, but soon I discovered that that's not possible.
In my last question (where I discovered the above), I received an answer talking about Attribute, Reflection, async/await modifiers and observable collection as the closest solution to that and I'll study them yet. In this new question, I would like to receive others suggestions of how that could be made (allow the user to use the window and controls while others are being created/loaded).
Thanks in advance.
Actually I believe the process of loading the UI part of controls isn't the heavy one.
In the other hand, loading the data which is later bound to the control is the problem.
You can't draw controls outside UI thread, but you can load heavy data, preload resources or do calculation in a background thread.
While heavy controls' data is prepared to hit the UI in some background thread, UI will still be responsive.
For example, I guess Web browsers do HTML to DOM parsing in a background thread and they stream results in real time to the UI thread. That is, address bar and other UI components are responsive because UI thread isn't stressed.
I am developing a desktop application for pdf file management using C#.
When I search any folder for *.pdf the application stops responding for some time, which is undesired behavior.
I am using XmlWriter to write data (i.e. file name,author name,subject). Also I have a label to show current scanning of file but it only show last file after complition of scanning.
This is a classic problem.
Basically the thread which displays the application is the thread which is doing all the work. So any updates/responsivness of the GUI will have to wait until its finished.
The solution to this is to make it multithreaded. The simplest way is to use a Background Worker thread which will do the writing searching and whatever, and just leave the main thread free.
http://www.dotnetperls.com/backgroundworker
If you can update your program, .NET 4.0 has new IO functions that return before finishing:
For example EnumerateFiles:
http://msdn.microsoft.com/en-us/library/dd383458.aspx
In addition to Haedrian's answer, I'd say that you could use the ProgressChanged event of BackGroundWorker to handle updating your progress indicators.
More specifically, you could raise that event with ReportProgress method, passing the name of the file curently scanning, and in the BackGroundWorker.ProgressChanged event handler you could update the label you want to use to show the file under scanning
If you don't like to create an extra thread you can call
Application.DoEvents();
in the loop. This keeps application responding and updates the label.
On a button click, I make several changes to form elements (hiding some, showing some, bringing some to front, etc.). After those form element changes are made, I run an external process with a Process.Start(). However, even those those form element layout changes are sequentially coded before the Process.Start() call, they're not being executed/displayed BEFORE my Process.Start().
How do you force a flush of these layout changes that seem to be buffered?
You could try the Control.Invalidate(true) function on the control you want to be redrawn.
Here is a good post about the difference between Refresh, Update, and Invalidate
Based on the Post, I think you would want to use Refresh over Update to invalidate, then immediately update the control
Try either running the .Refresh method before the process.Start, or run Process.Start in a separate thread, such as:
System.Threading.ThreadPool.QueueNewWorkerItem(new System.Threading.WaitCallback(StartProcess));
void StartProcess(object state)
{
Process.Start(...);
}
By putting the start in a background thread, you allow .NET to update the UI before items in the background thread run. If the Process.Start is in the same thread as the UI, then the UI cannot refresh until all processes in that thread have finished running.
Found answer..
mainFormName.ActiveForm.Update();
Bang bang.