Nafihudheen KT

Nafihudheen KT

  • NA
  • 43
  • 79.2k

How to handle Thread in c# ?

Jan 6 2012 1:02 AM
Hai,

I want to do the printing from my dot net project as background work as a thread, for that what I did is, first collect the each output string to a collection of string

like this 
 myOutputStringCollection.Add(str);

then after collecting all lines I want to send it to printer, I write code like this which executes a thread

 public static void printAllLines()
 {
            Thread t = new Thread(sendToPrinter);            
            t.Start();//starts the thread
        }

and the send to printer function is like this

 public static void sendToPrinter()
 {
 int count = myOutputStringCollection.Count;
            string[] myArray = new string[count];
            myOutputStringCollection.CopyTo(myArray, 0);
            for (int i = 0; i < count; i++)
 {
         SendStringToPrinter(myArray[i].ToString());
    }
 Array.Clear(myArray, 0, myArray.Length);
 }



but the problem here am facing is, if I click the print button more than one times Immediately then the printing alignment is not correct, I think if I handle the thread execution properly then it will be all right, If any body know more about threading and its operation then pls share here

Thanks in advance.



Answers (6)