المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : How to sleep the thread in this example?



C# Programming
06-03-2012, 11:51 AM
Hi,

I am writing an automated printer driver performance measuring tool. I have one problem that I need to fix.

Please check the sample code below.

What I need to do is, when I add the job to the print queue, I need to "poll" for the "isDeleted" status (or if possible, "isCompleted" status) and display the time (that part is not written in the sample code) it took to finish the job. In this case, "finish" means that the PRN file has been created. (The port of the printer is set to Local port: PRN file path.)

For this, I need to add a pause (of suitably low amount so that it doesn't affect the time) where I have put a comment in the code below.

I can easily do this with a timer, but it got a bit complicated when i kept on adding features. (i would be adding a list of job, would be backing up the PRN files, get time measurement, use two drivers and compare performance etc. etc.)

using System; using System.Windows.Forms; using System.Printing; using System.Threading; namespace AddJob { public partial class frmAddJob : Form { public frmAddJob() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Thread printingThread = new Thread(printXPS); printingThread.SetApartmentState(ApartmentState.STA); printingThread.Start(); } private void printXPS() { LocalPrintServer lp = new LocalPrintServer(); PrintQueue pq = LocalPrintServer.GetDefaultPrintQueue(); try { PrintSystemJobInfo psji = pq.AddJob("myXPSJob", "C:\\test.xps", false); while (psji.IsDeleted != true) { //need to wait here for some time psji.*******(); } MessageBox.Show("Done"); } catch { MessageBox.Show("Error"); } } } }
Does anyone have any tips for me?