I am trying to print pdf from windows application in my project with iTextSharp
dll..
But,The methods I have used till now doesn't seems to be reliable.(Sometimes it work and sometime it doesn't)
I am combining the whole process in a Process class and writing the followin code.
printProcess = new Process[noOfCopies];
// Print number of copies specified in configuration file
for (int counter = 0; counter < noOfCopies; counter++)
{
printProcess[counter] = new Process();
// Set the process information
printProcess[counter].StartInfo = new ProcessStartInfo()
{
CreateNoWindow = true,
Verb = "Print",
FileName = pdfFilePath,
ErrorDialog = false,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = true,
};
// Start the printing process and wait for exit for 7 seconds
printProcess[counter].Start();
// printProcess[counter].WaitForInputIdle(waitTimeForPrintOut);
printProcess[counter].WaitForExit(waitTimeForPrintOut); //<--**This is line for discussion**
if (!printProcess[counter].HasExited)
{
printProcess[counter].Kill();
}
}
// Delete the file before showing any message for security reason
File.Delete(pdfFilePath);
The problem is that if,no pdf is open then the process works fine...(It prints well).But if any pdf is open then WaitForExit(..)
method just doesn't waits for the process to exit..and hence the process runs too fast and as I am deleting the file after print it gives error if counter(no of times..) for printing report is more than once..
I have also used Timer
to slow the process but it just doesn't works.I don't know why.
Used Sleep command too..but it makes main thread to go to sleep and hence doesn't suits me either.
Please suggest me some really reliable way to do so..:)