Hi all;
I'm writing a C# WinForms application for Win7 and higher in Visual Studio 2008. I'm on a 64 bit computer - ideally compiling for 32 bit, but the problem occurs either way.
I'm using the latest version of the Windows API Code Pack from
here which has a lot of handy Win7-based features.
The thing I'm having difficulty with is the dialog CommonOpenFileDialog
I'm using it as a folder picker, but I have the issue in either mode.
It's fast to open, great for navigating my hard drive. However when I hit "Select Folder" to finalize my selection, my application hangs for (usually) five seconds. Windows even throws a "Not Responding" tag on the application name for a bit.
I can't find
any instances of this happening to anyone else on Google, which is doubly bizarre.
I tried getting the latest version of the API Code Pack (I did not have it earlier), no change.
I tried restarting my computer, no change.
I swear I've had this dialog working for me before but it was a while ago..
I have a hard time believing it's the control itself causing the problem, I'm sure it works as advertised. Simultaneously I have a hard time believing my sample program (below) is using it incorrectly.
I tried debugging the API Code Pack source, and nowhere in the managed code did I find the delay - it's somewhere in the native code.
The native code calls FileOk in the code pack which calls FileOk in the sample program, which starts the timer.
When ShowDialog returns, the timer is stopped. There is a large delay here.
I timed the guts of ShowDialog after the call to the native ShowDialog, and that's all <1ms. So it's inside the native ShowDialog, after it calls FileOk and before it returns.
Can someone give me suggestions as to how to debug this problem? (No idea about unmanaged debugging) What else about my installation/computer/code could be causing trouble?
I recently upgraded to VS2008 SP1, and there are no more Windows Updates available for my PC. I'm at my wits end!
Thanks for your time;
William
I made a tiny sample form that repros the problem.
0. Create a new WinForms project
1. Download the file from the link above and extract.
2. Stick Microsoft.WindowsAPICodePack.Shell.dll and Microsoft.WindowsAPICodePack.dll from Binaries into the project directory and add references to them. [Alternatively do the same with the source, but it makes no difference]
3. Stick one button and one label on the form.
4. Output is in milliseconds.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsAPICodePack;
using Microsoft.WindowsAPICodePack.Dialogs;
using System.Diagnostics;
namespace CommonOpenFileDialog_Issues
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
label1.Text = "Click Browse... to begin";
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "Waiting...";
CommonOpenFileDialog cofd = new CommonOpenFileDialog("Why so slow to return?");
cofd.IsFolderPicker = true;
cofd.Multiselect = false;
cofd.InitialDirectory = @"C:\";
cofd.FileOk += new CancelEventHandler(cofd_FileOk);
cofd.ShowDialog();
sw.Stop();
label1.Text = sw.ElapsedMilliseconds.ToString();
}
Stopwatch sw = new Stopwatch();
/// <summary>
/// Apparently called immediately before cofd.ShowDialog(); returns.
/// However there is a HUGE delay between this event, and the function returning.
/// It does not always happen (sometimes ~30ms as I might expect) but when it does, it takes 5 to 10 seconds.
/// It occurs *at least* once every 20 tries.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void cofd_FileOk(object sender, CancelEventArgs e)
{
sw.Reset();
sw.Start();
}
}
}