.net - Handle Thread in c# for print -
i want printing dot net project background work thread, did is, first collect each output string collection of string this:
myoutputstringcollection.add(str); then after collecting lines want send printer, write code executes thread:
public static void printalllines() { thread t = new thread(sendtoprinter); t.start(); //starts thread } and send printer function this:
public static void sendtoprinter() { int count = myoutputstringcollection.count; string[] myarray = new string[count]; myoutputstringcollection.copyto(myarray, 0); (int = 0; < count; i++) { sendstringtoprinter(myarray[i].tostring()); } array.clear(myarray, 0, myarray.length); } the problem here facing is, if click print button more 1 times printing alignment not correct, think if handle thread execution right.
to prevent problem occurring, i'd disable print button before start thread , re-enable after strings have been sent printer:
public static void printalllines() { btnprint.enabled = false; thread t = new thread(sendtoprinter); t.start();//starts thread } public static void sendtoprinter() { int count = myoutputstringcollection.count; string[] myarray = new string[count]; myoutputstringcollection.copyto(myarray, 0); (int = 0; < count; i++) { sendstringtoprinter(myarray[i]); // no need tostring() it's string } // re-enable print button marshalling ui thread this.invoke(new methodinvoker(delegate() { btnprint.enabled = true; })); } the call array.clear() shouldn't needed myarray local variable , therefore destroyed anyway when sendtoprinter method (and thread) ends.
Comments
Post a Comment