Solved

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

Posted on 2016-07-17
6
38 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
Comment Utility
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 32

Accepted Solution

by:
sarabande earned 500 total points
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:jetbet
Comment Utility
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 32

Expert Comment

by:sarabande
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

9 Experts available now in Live!

Get 1:1 Help Now