6 Replies - 203 Views - Last Post: 09 January 2018 - 10:48 AM Rate Topic: -----

#1 djkitt  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 192
  • Joined: 22-May 08

Printing To File - Locks Active Application

Posted 08 January 2018 - 05:13 PM

Hi All,

It has been a long time since I have posted here, but I have run into something that is pretty weird and was wondering if anyone here has seen the same thing.

So, I have a vb.net windows forms program that monitors an SQL database and prints a report when a flag is set. I set up a windows printer to print to a text file for testing.

In a nutshell, when the printer requests a file name it locks the UI of whatever program happens to be active on my Windows 7 machine until I either cancel the request, or enter a file name.

I created a simple test program to demonstrate this:

Imports System.Drawing.Printing

Public Class MainForm

    Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadPrinterNames()
    End Sub

    Private Sub PrintButton_Click(sender As Object, e As EventArgs) Handles PrintButton.Click
        Threading.Thread.Sleep(5000) 'delay the print to enable selecting another program as the active program
        TestPrint()
    End Sub

    Private Sub OkButton_Click(sender As Object, e As EventArgs) Handles OkButton.Click
        Me.Close()
    End Sub

    Private Sub LoadPrinterNames()
        Dim pSettings As New PrinterSettings
        For Each strPrinter As String In PrinterSettings.InstalledPrinters
            PrinterNameComboBox.Items.Add(strPrinter)
            '// Set the ComboBox to the Default Printer
            pSettings.PrinterName = strPrinter
            If pSettings.IsDefaultPrinter Then
                PrinterNameComboBox.Text = strPrinter
            End If
        Next
    End Sub

    Private Sub TestPrint()
        Dim pd As PrintDocument = New PrintDocument()
        pd.DocumentName = "DelayedPrintDoc"
        pd.PrinterSettings.PrinterName = PrinterNameComboBox.Text
        AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
        Try
            pd.Print()
        Catch ipex As InvalidPrinterException
            MessageBox.Show(ipex.ToString(), "Print Error")
        End Try
    End Sub

    Private Sub pd_PrintPage(sender As Object, ev As PrintPageEventArgs)
        Dim printFont As Font = New Font("Courier New", 12)
        Dim line As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz~!@#$%^&*()"
        Dim xPos As Single = 45
        Dim yPos As Single = 0
        Dim len As Integer = line.Length
        'Print 53 lines
        For i As Integer = 1 To 53
            yPos = 25 + (i * printFont.GetHeight(ev.Graphics))
            ev.Graphics.DrawString(line, printFont, Brushes.Black, xPos, yPos, New StringFormat())
            If len >= 2 Then
                line = line.Substring(len - 1, 1) + line.Substring(0, len - 1)
            End If
        Next
        'that's all folks...
        ev.HasMorePages = False
    End Sub

End Class



Basically, the form has a combobox that lists the printers on my system and a button that prints a test page when clicked. I added a 5 second delay to give me time make another application the active window. When the page is sent to the 'printToFile' printer, a 'Print to File' dialog box pops up asking for an 'Output File Name'. Whatever app I have selected is locked for UI access until this dialog box is cleared...

I have tried to search the web for comments on this but have come up blank.

Has anyone else experienced this behavior? Are there any other instances where this kind of thing could come back to bite us?

Thanks,

Is This A Good Question/Topic? 0
  • +

Replies To: Printing To File - Locks Active Application

#2 andrewsw  Icon User is online

  • blow up my boots
  • member icon

Reputation: 6447
  • View blog
  • Posts: 26,075
  • Joined: 12-December 12

Re: Printing To File - Locks Active Application

Posted 09 January 2018 - 02:41 AM

I am slightly confused about what your question is. When we choose 'Print to file' a dialog appears asking us to provide a filename, so the behaviour you are seeing isn't unexpected.

Or are you asking how to supply the filename automatically..? I have my doubts that this is possible so would just exclude 'Print to file' as an option.
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13666
  • View blog
  • Posts: 54,541
  • Joined: 12-June 08

Re: Printing To File - Locks Active Application

Posted 09 January 2018 - 08:01 AM

Seems like expected activity..

Although I am confused on the logic flow. So see if I get this right.

The app pings a SQL DB.
Prints some report.
Testing has it printing to a file
The file picker dialog asks for a file
The process is locked until that happens.
Was This Post Helpful? 0
  • +
  • -

#4 djkitt  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 192
  • Joined: 22-May 08

Re: Printing To File - Locks Active Application

Posted 09 January 2018 - 09:17 AM

Sorry, I was unclear.

The initial information I posted about accessing an SQL database was just background on how I ran into this problem.

And yes, it is expected behavior for a dialog to pop up and ask for a file name.

What is not expected, is the dialog window then blocking whatever application happens to be top-most.

Basically:

1) Start up notepad.exe
2) Run the program
3) Click the Print Button (The program sleeps for 5 seconds)
4) Click on notepad to make it the active application
5) The program comes out of its sleep and prints to the text file printer
6) The 'file name' dialog window pops up
7) Access to the running notepad window is blocked

The code I posted is just a small test app that demonstrates the behavior.

Selecting and printing to an XPS printer works as expected and the dialog window does block the test program (not notepad).

Thanks,

This post has been edited by djkitt: 09 January 2018 - 09:18 AM

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • blow up my boots
  • member icon

Reputation: 6447
  • View blog
  • Posts: 26,075
  • Joined: 12-December 12

Re: Printing To File - Locks Active Application

Posted 09 January 2018 - 09:25 AM

I remain confused because that is how Notepad (and most other applications) behave. If you open Notepad, go to print, check "Print to file", then a dialog will pop up preventing you from continuing with Notepad until you provide a filename or cancel the dialog.

Restating what I mentioned before, there is little point in automating "Print to file" unless there is some way of providing the filename automatically (or relying on the user to behave).
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13666
  • View blog
  • Posts: 54,541
  • Joined: 12-June 08

Re: Printing To File - Locks Active Application

Posted 09 January 2018 - 09:27 AM

That may just be an odd feature from a combination of the app waiting for reply coupled with the OS wanting that window to be addressed.

Why wouldn't you just click through it instead of pausing for X seconds to be able to go to some other app and not address it?
Was This Post Helpful? 0
  • +
  • -

#7 djkitt  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 192
  • Joined: 22-May 08

Re: Printing To File - Locks Active Application

Posted 09 January 2018 - 10:48 AM

View Postandrewsw, on 09 January 2018 - 10:25 AM, said:

If you open Notepad, go to print, check "Print to file", then a dialog will pop up preventing you from continuing with Notepad until you provide a filename or cancel the dialog.


Hey guys,

Yes. I expect if I was printing from notepad that it would block. But it is the test program that is printing, not notepad.

And, I am switching away to demonstrate what is actually happening on the test computer when our system is running.

Basically, we have a vendor provided Human Machine Interface windows program that is always on top and that we do not want to block. A windows form program is running behind the scenes managing a lot of things, including whether a report should be sent to the printer.

Because the HMI is top most it ends up being truly blocked by the pop-up. I can't just click on the HMI and continue what I am testing there without addressing the dialog window. I should be able to go back to the HMI and address the printer issue later.

I want the text file that is produced, but I don't need it right away.

Hopefully that make sense.

I have used CutePDF in the past to print without a dialog, or I could use the Windows XPS printer but I want the text file for testing. I could simply write to a text file, but I would rather not mess with the Printing procedure.

But most of all, I just found it odd, wondered if anyone else had run into this, and if there might not be similar gotcha out there... We have had the HMI go unresponsive in the past and found Windows Error Messages recorded, but not seen a pop-up on the error. Probably not related, but...

Thanks again,

Kitt
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1