Last active
August 30, 2018 14:15
-
-
Save corytodd/6045b0e04a16cfedac76c75dcfecdf63 to your computer and use it in GitHub Desktop.
Shows how to use a timer to await a print completion before checking status
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using PTIRelianceLib; | |
using System; | |
using System.Drawing; | |
using System.Drawing.Printing; | |
using System.Threading; | |
namespace RelianceCLI | |
{ | |
internal class Program | |
{ | |
private static int executionCount = 0; | |
private static System.Timers.Timer _mTimer; | |
private static AutoResetEvent _mGate; | |
private static void Main(string[] args) | |
{ | |
// Submit a print job to default printer | |
PrintDocument doc = new PrintDocument(); | |
doc.PrintPage += (s, o) => | |
{ | |
// Create font and brush. | |
Font drawFont; | |
drawFont = new Font("Bitstream Vera Sans Mono", 29); | |
SolidBrush drawBrush = new SolidBrush(Color.Black); | |
o.Graphics.DrawString("Test Page", drawFont, drawBrush, 0.0F, 80.0F); | |
// Long graphic to simulate long ticket | |
o.Graphics.DrawEllipse(new Pen(drawBrush), new Rectangle(100, 0, 80, 1600)); | |
}; | |
doc.Print(); | |
// Barrier to prevent closing while timer is still waiting | |
_mGate = new AutoResetEvent(false); | |
_mTimer = new System.Timers.Timer(200) | |
{ | |
AutoReset = true, | |
Enabled = true, | |
}; | |
_mTimer.Elapsed += Timer_Elapsed; | |
// Wait until Ping() starts failing | |
BlockUntilBusy(); | |
_mTimer.Start(); | |
_mGate.WaitOne(); | |
Console.ReadKey(); | |
} | |
private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) | |
{ | |
using (var printer = new ReliancePrinter()) | |
{ | |
while(!printer.IsDeviceReady) | |
{ | |
/* Try a number of times or for period of time */ | |
} | |
if (printer.Ping() == ReturnCodes.Okay) | |
{ | |
var status = printer.GetStatus(); | |
Console.WriteLine(status); | |
// Shutdown timer | |
_mTimer.Stop(); | |
// Allow app to exit | |
_mGate.Set(); | |
} | |
else | |
{ | |
Console.WriteLine("Printer is busy"); | |
} | |
} | |
} | |
private static void BlockUntilBusy() | |
{ | |
using (var printer = new ReliancePrinter()) | |
{ | |
while(!printer.DeviceReady) | |
{ | |
/* Try a number of times or for period of time */ | |
} | |
while (printer.Ping() == ReturnCodes.Okay) | |
{ | |
/* Burn up that CPU */ | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ideally you don't really want to burn up that CPU but this is just demonstrating worst case.