How to Print PRN file

M

Michael D Murphy

Any information on the best way to print a prn file from within a VB.Net Web
Application would be appreciated.
Thanks,
Michael Murphy
954-452-1047
(e-mail address removed)
 
P

Peter Huang [MSFT]

Hi

Based on my test, the code in the KB will work for ASP.NET application.
http://support.microsoft.com/?scid=kb;EN-US;322090

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If RawPrinterHelper.SendFileToPrinter("PrinterName", "C:\test.prn")
Then
Response.Write("OK")
Else
Response.Write("Failed")
End If
End Sub

In this way the test.prn is located on the IIS Server.
In addition we need to configure the ASP.NET process to run under the user
account which has added the "PrinterName" printer.

If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Michael D Murphy

Peter,
If I wanted to spawn the printing of the PRN file as a new thread, what
would I have to do differently??
Michael
 
P

Peter Huang [MSFT]

Hi

We do not need to do special things, we just need to create a new thread to
run the print code.
e.g.
Private Sub Print()
If RawPrinterHelper.SendFileToPrinter("printername", "C:\test.prn")
Then
Response.Write("OK")
Else
Response.Write("Failed")
End If
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim th As New Threading.Thread(AddressOf Print)
th.Start()
th.Join()
End Sub

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Michael D Murphy

Hi Peter,
Thanks for responding.
Just so I understand it correctly, I just have to spawn the print thread one
time in the form load and then every time I call the print function it will
automatically be executed in a new thread? This is my situation. I have an
XP Embedded program that runs mpeg files on the media player. I want the
player to keep playing, but if the user clicks the right mouse button, I
want to print a prn file associated with what the user is watching. Based on
what I have told you, can I still declare the new thread only once, or do I
need to declare it every time I need to print?
Thanks,
Michael
 
P

Peter Huang [MSFT]

Hi

If we spawn a new thread to run certain method, once the method exited, the
thread will exit too.
So one approach is run the thread on a method which have a while statement,
and the while statement will keep querying for a file or certain var to do
the print.
Otherwise, we need to spawn a new thread every time to run the print method.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Michael D Murphy

Hi Peter,
Do you think the code below will work, or am I misunderstanding what you
have instructed me to do?
Michael

Public intPrintNow as Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
intPrintNow =1
End Sub

Private Sub CheckForPrint()
While (1)
If intPrintNow then
If RawPrinterHelper.SendFileToPrinter("printername",
"C:\test.prn") Then
Response.Write("OK")
Else
Response.Write("Failed")
End If
End If
Wend
End Sub

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim th As New Threading.Thread(AddressOf CheckForPrint)
intPrintNow = 0
th.Start()
th.Join()
End Sub
 
P

Peter Huang [MSFT]

Hi

1) Because you are using While(1), so the loop will continue forever.
2) ASP.NET application is not similar with winform application. It is based
on Request/Respose.
i.e. The client post request, the server response to client in a reasonable
time.
th.Join()
The code above mean the page_load will continue until the working
thread(CheckForPrint) exit, while the working thread will not exit because
the while(1), so the page_load will not exit and the client will not get
response forever.

So I think for a quick print we can just spawn one thread to print, while
if we want to create a printer server, I think we can consider to use a
winform application or a windows service and the asp.net application will
sumbit the job to the windows application or windows service. Because we
must response to the Asp.NET client(e.g. IE) in a reasonable time.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Michael D Murphy

Hi Peter,
I was a thinking of another project I am working on when I told you it was a web app. The app is a windows app. Sorry, about that. I did get the following code to work, but it seems erratic. I am setting intPrintNow throughout different parts of the program. I tried including the call to Join, but it hung up my machine just as you said below, so I removed it and inserted an Application.DoEvents in the while loop and it works--most of the time. Can you tell me what I am doing wrong. Also, I assume the new thread will end when the program ends--is this correct?


Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim th As New Threading.Thread(AddressOf CheckForPrint)

intPrintNow = 0
th.Priority = ThreadPriority.BelowNormal

th.Start()

End Sub

Private Sub CheckForPrint()

While (1)

If intPrintNow Then

RawPrinterHelper.SendFileToPrinter("Ifcom Thermal Printer", "C:\iftest.prn")

intPrintNow = 0

End If

Application.DoEvents()

End While

End Sub

"Peter Huang" said:
Hi
1) Because you are using While(1), so the loop will continue forever.
2) ASP.NET application is not similar with winform application. It is
based on Request/Respose.
i.e. The client post request, the server response to client in a
reasonable time.

The code above mean the page_load will continue until the working
thread(CheckForPrint) exit, while the working thread will not exit
because the while(1), so the page_load will not exit and the client
will not get response forever.

So I think for a quick print we can just spawn one thread to print,
while if we want to create a printer server, I think we can consider
to use a winform application or a windows service and the asp.net
application will sumbit the job to the windows application or windows
service. Because we must response to the Asp.NET client(e.g. IE) in a reasonable time.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security This posting is provided "AS
 
P

Peter Huang [MSFT]

Hi

Based on my test, the new spawned thread will not terminate if we did not
terminate it or it terminate itself even if the main thread is exit.
When it a winform application started, it will have a default thread, i.e.
the Main Thread(Winform UI thread), if we start a new thread, then the
process will have two threads.
Even if we terminate the main thread(by closing the winform window), the
other thread will keep running, and the process will not exit until all the
threads exits.


[Test codoe, the threadproc will keep running,even if we close the winform
window]
Private Sub threadproc()
Try
While True
System.Diagnostics.Debug.WriteLine(Now().ToString() &
System.Threading.Thread.CurrentThread.Name)
System.Threading.Thread.Sleep(3000)
End While
Catch ex As Exception
System.Diagnostics.Debug.WriteLine("Thread " & ex.ToString())
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Dim th As New Threading.Thread(AddressOf threadproc)
th.Name = "Test Thread"
th.Start()
Catch ex As Exception
End Try
End Sub


For your scenario, you may try to control the thread state in the winform
application.
NOTE: the code below is for test purpose, you may need to change according
to your scenario.

'Give a bool value to indicate if we need to thread to exit
Dim bContinue As Boolean = False
Private Sub threadproc()
Try
While bContinue
System.Diagnostics.Debug.WriteLine(Now().ToString() &
System.Threading.Thread.CurrentThread.Name)
System.Threading.Thread.Sleep(1000)
End While
Catch ex As Exception
System.Diagnostics.Debug.WriteLine("Thread " & ex.ToString())
End Try
End Sub

Dim th As New Threading.Thread(AddressOf threadproc)

'Start thread
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
th.Name = "Test Thread"
bContinue = True
th.Start()
End Sub

'suspend thread if needed
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
If th.ThreadState And (Threading.ThreadState.Running Or
Threading.ThreadState.WaitSleepJoin) Then
th.Suspend()
End If
End Sub

'resume the thread from suspended state
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
If th.ThreadState And Threading.ThreadState.Suspended Then
th.Resume()
End If
End Sub

'exit the thread by set the bContinue flag to false.
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
bContinue = False
If th.ThreadState And Threading.ThreadState.Suspended Then
th.Resume()
End If
End Sub


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Michael D Murphy

Hi Peter,
One thing I did read was that if you set the IsBackGround = True, you don't
have to worry about terminating the worker thread.
Thanks again for your help.
Michael
"Peter Huang" said:
Hi

Based on my test, the new spawned thread will not terminate if we did not
terminate it or it terminate itself even if the main thread is exit.
When it a winform application started, it will have a default thread, i.e.
the Main Thread(Winform UI thread), if we start a new thread, then the
process will have two threads.
Even if we terminate the main thread(by closing the winform window), the
other thread will keep running, and the process will not exit until all
the
threads exits.


[Test codoe, the threadproc will keep running,even if we close the winform
window]
Private Sub threadproc()
Try
While True
System.Diagnostics.Debug.WriteLine(Now().ToString() &
System.Threading.Thread.CurrentThread.Name)
System.Threading.Thread.Sleep(3000)
End While
Catch ex As Exception
System.Diagnostics.Debug.WriteLine("Thread " & ex.ToString())
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Dim th As New Threading.Thread(AddressOf threadproc)
th.Name = "Test Thread"
th.Start()
Catch ex As Exception
End Try
End Sub


For your scenario, you may try to control the thread state in the winform
application.
NOTE: the code below is for test purpose, you may need to change according
to your scenario.

'Give a bool value to indicate if we need to thread to exit
Dim bContinue As Boolean = False
Private Sub threadproc()
Try
While bContinue
System.Diagnostics.Debug.WriteLine(Now().ToString() &
System.Threading.Thread.CurrentThread.Name)
System.Threading.Thread.Sleep(1000)
End While
Catch ex As Exception
System.Diagnostics.Debug.WriteLine("Thread " & ex.ToString())
End Try
End Sub

Dim th As New Threading.Thread(AddressOf threadproc)

'Start thread
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
th.Name = "Test Thread"
bContinue = True
th.Start()
End Sub

'suspend thread if needed
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
If th.ThreadState And (Threading.ThreadState.Running Or
Threading.ThreadState.WaitSleepJoin) Then
th.Suspend()
End If
End Sub

'resume the thread from suspended state
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
If th.ThreadState And Threading.ThreadState.Suspended Then
th.Resume()
End If
End Sub

'exit the thread by set the bContinue flag to false.
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
bContinue = False
If th.ThreadState And Threading.ThreadState.Suspended Then
th.Resume()
End If
End Sub


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no
rights.
 
P

Peter Huang [MSFT]

Hi

Thanks for your feedback.
If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top