2

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.

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
Gourav Goyal
  • 37
  • 1
  • 1
  • 4
  • Hi all I did it by using backgroundworker. But a problem persist, when i reading current scanning through label it gave me error "Cross-thread operation not valid: Control 'label2' accessed from a thread other than the thread it was created on". Any clue why it is showing that error Thanks Regards Gourav Goyal – Gourav Goyal Mar 19 '12 at 13:36

4 Answers4

1

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

Haedrian
  • 4,240
  • 2
  • 32
  • 53
1

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

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
0

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

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
themarcuz
  • 2,573
  • 6
  • 36
  • 53
  • Hi all I did it by using backgroundworker. But a problem persist, when i reading current scanning through label it gave me error "Cross-thread operation not valid: Control 'label2' accessed from a thread other than the thread it was created on". Any clue why it is showing that error Thanks Regards Gourav Goyal – Gourav Goyal Mar 19 '12 at 13:38
  • read [this post](http://stackoverflow.com/a/906097/333223) or [this post](http://stackoverflow.com/a/142069/333223) – themarcuz Mar 19 '12 at 14:03
0

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.

Florian
  • 467
  • 4
  • 8