Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6192789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:00:21+00:00 2026-05-24T03:00:21+00:00

I am writing a Console application using C#, in which I am firing Print

  • 0

I am writing a Console application using C#, in which I am firing Print jobs through code. My issue is I do not know how much time I should wait for Print Dialog after sending commands to Print. For the time being I am using Thread sleep of 1000 milliseconds.

//Sending Commands to Print(to press "Ctrl+P" button).
 SendKeys.SendWait("^(p)");

 Thread.Sleep(1000);

 //Sending Commands to Print(to press "Enter" Button).
 SendKeys.SendWait("{ENTER}");

Can anybody help me to fix this issue please. Any help will be appreciated.

Thanks in advance

Update:

Here is my whole code:

//Launch the file from the location specified in.
White.Core.Application application =White.Core.Application.Launch(@path);

Console.WriteLine("launch is done");

Thread.Sleep(_delayOfPrint);

//Sending Commands to Print(to press "Ctrl+P" button).
SendKeys.SendWait("^(p)");

Thread.Sleep(1000);

//Sending Commands to Print(to press "Enter" Button).
SendKeys.SendWait("{ENTER}");

//Get the current time as the document fired for print job.
_printedTime = DateTime.Now;

Thread.Sleep(1500);

//Closing the window.
SendKeys.SendWait("%{F4}");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:00:22+00:00Added an answer on May 24, 2026 at 3:00 am

    I would have a maximum wait of a few seconds, but during that time I would periodically use the Win32 function FindWindowEx to see if the print dialog actually came up, and if so proceed. You can get a process’ main window with code like this:

    Process[] processes = Process.GetProcessesByName(appName);
    foreach (Process p in processes)
    {
         IntPtr pFoundWindow = p.MainWindowHandle;
    }
    

    You can then pass the found main window handle to FindWindowEx to peruse the child windows to check for the print dialog. FindWindowEx has PInvoke signatures like this:

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
    
    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className,  IntPtr windowTitle);
    

    Edit 2: Since the OP seems to be demanding I give a perfectly working function, I wrote a general one that does the trick and tested it. Here it is, working general code to wait for any child window. Call as WaitForChildWindow(“myApp”, “Print”, 5000) for this case:

    /// <summary>
    /// Wait for a child window of an application to appear
    /// </summary>
    /// <param name="appName">Application name to check (will check all instances)</param>
    /// <param name="childWindowName">Name of child window to look for (titlebar)</param>
    /// <param name="timeout">Maximum time, in milliseconds, to wait</param>
    /// <returns>True if the window was found; false if it wasn't.</returns>
    public static bool WaitForChildWindow(string appName, string childWindowName, int timeout)
    {
        int sleepTime = timeout;
        while (sleepTime > 0)
        {
            Process[] processes = Process.GetProcessesByName(appName);
            foreach (Process p in processes)
            {
                IntPtr pMainWindow = p.MainWindowHandle;
                IntPtr pFoundWindow = FindWindowEx(pMainWindow, IntPtr.Zero, null, childWindowName);
                if (pFoundWindow != IntPtr.Zero)
                    return true;
            }
            Thread.Sleep(100);
            sleepTime -= 100;
        }
    
        // Timed out!
        return false;
    }
    

    Edit 3: Here’s another way of doing it for merely owned windows that don’t have a child relationship:

    [DllImport("user32.dll", SetLastError = true)]
    static extern uint GetWindowThreadProcessId(IntPtr hwnd, IntPtr processId);
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool EnumThreadWindows(uint dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
    [DllImport("user32", SetLastError = true, CharSet = CharSet.Auto)]
    private extern static int GetWindowText(IntPtr hWnd, StringBuilder text, int maxCount);
    
    public delegate bool EnumThreadDelegate(IntPtr hwnd, IntPtr lParam);
    
    static bool EnumThreadCallback(IntPtr hWnd, IntPtr lParam)
    {
        StringBuilder text = new StringBuilder(500);
        GetWindowText(hWnd, text, 500);
        if (text.ToString() == "Print")
            return false;
        return true;
    }
    
    public static bool FindThreadPrintWindow(uint threadId)
    {
        return !EnumThreadWindows(threadId, EnumThreadCallback, IntPtr.Zero);
    }
    
    public static bool WaitForOwnedPrintWindow(string appName, int timeout)
    {
        int sleepTime = timeout;
        while (sleepTime > 0)
        {
            Process[] processes = Process.GetProcessesByName(appName);
            foreach (Process p in processes)
            {
                IntPtr pMainWindow = p.MainWindowHandle;
                uint threadId = GetWindowThreadProcessId(pMainWindow, IntPtr.Zero);
                if (FindThreadPrintWindow(threadId))
                    return true;
            }
            Thread.Sleep(100);
            sleepTime -= 100;
        }
    
        // Timed out!
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a console application program as httplistener and using it through jquery and
If we are writing about power-wise option - is using a console-based application in
I am writing a console application which makes use of the F1 key (for
I'm writing a console application which is looking up information about SSIS packages in
I am writing a console based Java application in which the user will be
I am writing a console application using BDE 2006 and I want it to
I'm writing a small program using CLR console application and when I write something
I'm writing a console application that will be deployed with an installation of an
I'm writing a console application in Visual Basic 2008 Express. I added several text
I am writing a console application in python that will consist of a handful

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.