Hang Prone Device I/O APIs


Applications should not perform any kind of synchronous I/O on a UI thread (the threads that service WNDPROC callbacks and have a message dispatching loop).  The reason is that an application does not know when environmental conditions may lead these APIs to become unresponsive, such as a bad hard drive or a slow network connection.  However, in practice, a lot of code gets away with the behavior.  It may be impractical to develop an architecture to account for a situation that has a low probability of hanging (such as reading from a local, fixed hard drive).


In Windows Vista and Windows 7, we collected a great deal of data on which APIs are the largest culprits in causing hangs based on device I/O.  The following list are the APIs that contribute most frequently to hangs in Device I/O.  While this list is heavily influenced by the choices of developers whose applications end up in the data, it is still a good indication of which APIs to avoid on your UI thread:



























































KERNEL32.DLL


CallNamedPipe


CopyFileEx


CopyFile


CreateFile


DeleteFile


FindFirstFileEx


FindFirstFile


GetDiskFreeSpaceEx


GetDiskFreeSpace


GetDriveType


GetFileAttributesEx


GetFileAttributes


GetLongPathName


GetPrivateProfileInt


GetPrivateProfileSectionNames


GetPrivateProfileSection


GetPrivateProfileString


GetPrivateProfileStruct


GetShortPathName


GetVolumeInformation


GetVolumeNameForVolumeMountPoint


GetVolumePathName


SearchPath


SetCurrentDirectory


SetFileAttributes


WritePrivateProfileString

















SHELL32.DLL


SHAddToRecentDocs


SHCreateItemFromParsingName


SHGetFileInfo


SHGetPropertyStoreFromParsingName


SHILCreateFromPath















SHLWAPI.DLL


PathFileExists


PathIsDirectory


SHCreateStreamOnFileEx


SHCreateStreamOnFile











VERSION.DLL


GetFileVersionInfoSize


GetFileVersionInfoSizeEx


Although this list is not exhaustive, it does represent APIs that are among the largest contributors to hangs on the UI thread.  Avoid using them from within your WNDPROC!


Comments (3)

  1. udigawa says:

    So do you have a work around for these pitfalls?  I am particularly interested in PathFileExists.

    I have an app that is hanging on this command (Win7 home edition)

    Thanks

    udi

  2. anon says:

    Workaround: Do the call on a background thread.  Don't block the UI thread!

  3. Nick says:

    It's a shame that Windows Explorer doesn't follow this logic.

Skip to main content