views:

117

answers:

1

I have an application which uses Webbrowser control to navigate to a few vendor sites and scrape reporting. Because there are several accounts with one vendor, I need to have the browser end its session and clear any set cookies. I am using API calls to wininet.dll for both and am able to achieve the desired result. However, intermittently, I get what appears to be a deadlock on the InternetSetOption for INTERNET_OPTION_END_BROWSER_SESSION.

It is very linear code and InternetSetOption is called from a single thread in one place. What makes it difficult to figure out is that while running in debug, I've only very rarely been able to reproduce the problem. As soon as I compile and run outside of VS, it will happen shortly after. I isolated it to that by pumping messages to the console as it runs.

The few times I've been able to catch the problem while debugging, it just shows InternetSetOption as the next statement and sits, no exceptions or errors, nothing in the output window. InternetSetOption should return boolean, but the program will go no further and I never get any return from the API call. I tried look at the last error message thrown, but in this case it doesnt help, as it never throws an error.

Anyone have any input as to what I can further do to debug this?

Declared as:

[DllImport("wininet.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool InternetSetOption(
    IntPtr hInternet,
    int dwOption,
    IntPtr lpBuffer,
    int lpdwBufferLength);

And called by:

InternetSetOption(IntPtr.Zero, 42, IntPtr.Zero, 0);
A: 

For anyone who comes across this and, by chance, has the same issue - the problem appears to be caused by a function I had found on MSDN's site for clearing cache and cookies. found here.

There were several issues in that ClearCache() function, one being between x86 and x64 and how it referenced the cache files to delete them -

In the current function, it calles DeleteUrlCacheEntry like so:

DeleteUrlCacheEntry(internetCacheEntry.lpszSourceUrlName);

However, in x64 is should be

DeleteUrlCacheEntry(internetCacheEntry.lpszLocalFileName);

You have to toggle between those based on platform, one single reference does not work on both x64 and x86. Moot point for me, however. This function often throws Access Violation exceptions, and even when caught and handled, I believe that was the root of InternetSetOption performing what seemed as a deadlock. Again, was never able to trouble shoot it thoroughly due to the odd nature of the deadlock, for me to use both that ClearCache() function AND InternetSetOption. Changing the sequence of the two functions calls did not correct it.

I removed the WebBrowserHelper class and I resorted back to clearing cache and cookies by executing RunDLL32 and waiting for that process to exit.

Hope that helps someone!

kmfk