Solved

A quicker version of File.Exists using Shlwapi.dll for my c# program

Posted on 2016-07-17
6
52 Views
Last Modified: 2016-07-19
I have a GUI that needs to load images throughout the day as things change.

These (around 80k of them) are stored on a separate server which I access via a shared drive. Often some of the images are missing.

I have also been trying to find an issue where the GUI locks up for seconds. Recently the server had its IP address changed and so the mapping was invalid. I noticed this caused the GUI to lock up for up to 30 seconds or so and tracing the problem discovered that the call to File.Exists was taking a long time to return for some (but not all?) of the checks.

I believe that there can be an issue with File.Exists in that if it cannot find the file it creates an internal exception and this slows things down. I have no choice but to do the dynamic loading, of the images, on the main thread.

I have read that the following would be a better method given the limitations

[DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
private extern static bool PathFileExists(StringBuilder path);

void Exists()
{
    // A StringBuilder is required for interops calls that use strings
    StringBuilder builder = new StringBuilder();
    builder.Append(@"C:\test.txt");
    bool exists = PathFileExists(builder);
}

Open in new window


Is it worth using this method and if so what would be the best way to put the call into my utility class (static methods)?
0
Comment
Question by:jetbet
6 Comments
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 41716267
Never tried this but from what I read it is much faster.

To put the call into a static methods you probably need to declare the above method as static:

http://www.pinvoke.net/default.aspx/shlwapi.pathfileexists
0
 
LVL 34

Accepted Solution

by:
sarabande earned 500 total points
ID: 41717084
i think your issues are related to the network rather than to using the wrong api.

if the server the files are stored at, is under your control, you might think of a service where you can direcly connect to from client and which could check file existence much faster than using a shell api over network. if you would use an own service at the Server, the service already could hold a list of the files in question in memory and Keep that list up-to-date by monitoring file creation and file modification within the folders of interest.

by the way, the fastest api to check file existence is stat function of the c runtime library.

Sara
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 41717133
Your GUI locking implies that you are doing processing on the GUI thread. You should restructure your code to make use of asynchronous and/or parallel programming techniques such as async/await or a BackgroundWorker so that you can avoid tying up the GUI thread.
1
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jetbet
ID: 41717818
A little more information.

The application in question is a GUI to drive a graphics engine. The images in question are horses "silks" that need to be displayed on several different scenes. Throughout the day as races close (or another is manually selected) we need to update the scenes with the current race information including up to, potentially, 24 silks. For some runners these silk files will not exist.

As the scene needs to have the "silks" available we need to do this on the main thread as we would need to wait for them to load before proceeding anyway. We can not always predict what the next race wanted will be and so preloading is not an option.


The repository of scenes is shared between 2 different studios and is updated daily by an automatic process. The scenes are on a server running an application called a "Graphics Hub".
0
 
LVL 34

Expert Comment

by:sarabande
ID: 41719288
what are you doing when a file doesn't exist? you probably show some default or dummy silk, right?

if so, you very well could do the retrieving of the images in the Background and when ever the background thread received a new Image it could send a message to the gui thread such that the dummy image was replaced by the right one.

note, even if you can't display a scene as long as not all images have been loaded. it nevertheless woudl make sense to do the loading in a worker thread, since you would keep the GUI responsive that way and not "freeze" what actually is not a good behavior.

Sara
0
 

Author Closing Comment

by:jetbet
ID: 41719878
I decided to do a variation on your idea and load the names of the images into a Hash Table when the application starts and use that for my file existence checks. I then keep an eye on any updates arriving so I can   add any new images produced during the day.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question