C#'s not my strongest language ... ... but I have a working program with a problem. Here's the code in question :
Code:
private void button2_Click(object sender, System.EventArgs e)
{
string[] frmdata = textBox1.Lines; //Incoming numbers, each number is a different form to print.
string[] valid = new String[textBox1.Lines.Length]; //Make an array to use as a check.
int cnt = 0;
Regex nMatch =new Regex("^[0-9]{5,}$"); //Each line should be 5 digits.
object bleh = System.Reflection.Missing.Value; //A junk value - used later as a filler for variables that are not used.
foreach (string frmElem in frmdata) {
if ((nMatch.IsMatch(frmElem)))
{ //Is the current line a good result?
valid[cnt] = frmElem; //Yeah, add to the check array.
}
else
{
valid[cnt] = "XXXXX:" + frmElem; //No doh! Change it to the flag-value
}
cnt++;
}
for (int x = 0;x < valid.Length;x++)
{
axBrowse1.BringToFront(); //Use the browser to view the HTML forms.
valid[x] = @"c:\forms\" + valid[x] + ".html"; //Modify the checked value to a local address.
try
{
axBrowse1.Navigate(valid[x],ref bleh,ref bleh,ref bleh,ref bleh); //Load
mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2) axBrowse1.Document;
while(axBrowse1.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
{ //While the form's not loaded, just do whatever the program needs to do .
Application.DoEvents();
}
axBrowse1.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT,
SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER,
ref bleh, ref bleh); //It's done loading the form. Print it to the default printer.
System.Threading.Thread.Sleep(1100);
//1.1 sec pause for the job to finish?? It helped out-of-order somewhat, but there are still problems.
}
catch
{
valid[x] = "XXXXX: ERROR: " + valid[x] + " was not found.";
// Note all errors for later. A list of valid and invalid prints is saved to print a job report later on.
}
}
Yes, I already know I'm using a depreciated library (mshtml). I can deal with that for the moment. But, the problem is as such:
I'm printing a number of sequential HTML files, each file as a separate print job. The code is passed those files as frmdata.
Everything prints fine.
But, everything doesn't print in order. I'd expect the doevents() to clean up and flush + my thread-sleep ... so everything comes out in order ... but the spooler just doesn't do it for some reason. Is there something I should be doing to force-send to the spooler so things come in order?
Also, on occasion ... a print from the end of the list might come out first (huh??!!) So that's a bonus right there.
It's not a big deal - as this program saves a good half hour of work daily, but, we could save another 5 minutes if I could figure out how to get everything printed in order.
Any ideas? What is it that I'm just missing/overlooking?
My normal job size is 30-50 separate documents, some of them multiple pages.