Solved

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

Posted on 2016-07-17
6
70 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Technology Partners: 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!

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

630 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