Solved

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

Posted on 2016-07-17
6
39 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 29

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 33

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now