Solved

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

Posted on 2016-07-17
6
45 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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 Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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