Gábor Gábor - 9 months ago 102
C# Question

IProgressDialog to start immediately

I use the

interface as per normal, everything works all right but the progress dialog only appears after a few seconds when it decides that the operation is indeed lengthy. Although this is nowhere documented, it seems to be intended behavior and not bad in many cases. However, in this particular application I'd need to display it immediately.

I found a single reference on the whole wide web to acknowledge this and to suggest a workaround. However, maybe because there were quite a few new Windows versions during that nine years, it doesn't seem to work today:

IProgressDialog dialog;
dialog.StartProgressDialog(owner, null, flags, IntPtr.Zero);
IntPtr DialogHWnd;
((IOleWindow)dialog).GetWindow(out DialogHWnd);
SendMessage(DialogHWnd, WM_TIMER, new IntPtr(1), IntPtr.Zero);

The window handle is OK, I checked it, there is no error, just there is no change in behavior. The dialog only appears after a few seconds.

Does anybody have a suggestion short of re-creating the same dialog and functionality in C# again? Not impossible, of course, but the stock dialog has nice features like animation and canceling behavior, in addition to being very familiar to users.

Answer Source

Call dialog.Timer(PDTIMER_RESUME, null) immediately before or after StartProgressDialog. Here is a minimal sample:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {

public enum IPD_Flags: uint {
    Normal = 0x00000000,
    Modal = 0x00000001,
    AutoTime = 0x00000002,
    NoTime = 0x00000004,
    NoMinimize = 0x00000008,
    NoProgressBar = 0x00000010 

public enum IPDTIMER_Flags: uint {
    Reset = 0x00000001,
    Pause = 0x00000002,
    Resume = 0x00000003

internal class ProgressDialog {

internal interface IProgressDialog {

    void StartProgressDialog(IntPtr hwndParent
        , [MarshalAs(UnmanagedType.IUnknown)] object  punkEnableModless
        , uint dwFlags
        , IntPtr pvResevered);

    void StopProgressDialog();

    void SetTitle([MarshalAs(UnmanagedType.LPWStr)] string pwzTitle);

    void SetAnimation(IntPtr hInstAnimation, ushort idAnimation);

    [return: MarshalAs(UnmanagedType.Bool)]
    bool HasUserCancelled();

    void SetProgress(uint dwCompleted, uint dwTotal);

    void SetProgress64(ulong ullCompleted, ulong ullTotal );

    void SetLine(uint dwLineNum
        , [MarshalAs(UnmanagedType.LPWStr)] string pwzString
        , [MarshalAs(UnmanagedType.VariantBool)] bool fCompactPath
        , IntPtr pvResevered);

    void SetCancelMsg([MarshalAs(UnmanagedType.LPWStr)]string pwzCancelMsg, object pvResevered);

    void Timer(uint dwTimerAction, object pvResevered);


public partial class Form1 : Form {
    public Form1() {

    private void button1_Click(object sender, EventArgs e) {
        ProgressDialog progressDialog = new ProgressDialog();
        IProgressDialog iProgressDialog = (IProgressDialog)progressDialog;

        iProgressDialog.Timer((uint) IPDTIMER_Flags.Resume, null);
            , null
            , (uint)(IPD_Flags.Normal | IPD_Flags.NoMinimize)
            , IntPtr.Zero);