VI: the preparation of a more robust C # calling code (the actual consideration of the services available on the WINDOWS program) -------------------------------------------------- ------------- Actual use, due to PDF conversion at very high occupancy rate of CPU, consider the conversion at the same time only a WORD document, asynchronous thread to give up the use of the callback function, consider a WINDOWS service procedures. Write a function CheckData2Convert (), continue to check there is no conversion of the WORD document and use of recycled ToPdf class call methods of the implementation of the conversion StartConvertPDF
//the following is given, the Pan-code, the user according to their own needs, you can complete //bool bStart for global variables to control the entry and exit cycle //Example: 18:30 to start checking and conversion, then 18:30 am, bStart = true; and start the conversion thread //6:30 to stop the conversion thread, bStart = fasle;
private void CheckData2Convert () ( //Check the specified directory does not convert the WORD document, you can also check the database there is no record of the WORD document conversion string sPath = System.Threading.Thread.GetDomain (). BaseDirectory;//the current path while (bStart) ( int iFileCount = CheckWord ();//CheckWord is a process, not to check the current conversion of the WORD document, there is no conversion back to the number of documents, the method of preparation of the code by the readers themselves for (int i = 0; i ( string sWord = GetWordFileName (i)//GetWordFileName is a process, to return to a path with no file name of the WORD, the methods of preparation of the code by the readers themselves //ToPdf category StartConvertPDF () method without using the WORD file name path ToPdf my2Pdf = new ToPdf (sWord, sPath); my2Pdf.StartConvertPDF ();
if (my2Pdf.sExecResult.IndexOf ( "isuccess ")!=- 1) ( //Success, writing the log, or write-back database ) else if (my2Pdf.sExecResult.IndexOf ( "isfail ")!=- 1) ( //Fails, to write the log, or write-back database )
)
if (! bStart) break; Thread.Sleep (1000); ) )
the beginning and then in the service incident, start threads protected override void OnStart (string [] args) ( //Can use a timer to start to check the start time, the time comes, the beginning of the implementation of threads on here the beginning of the implementation of thread can be the beginning of the incident from time to time //End timer can use a check to the end of time, the time comes, the end of threads, thread the end of the code can be placed on the end of the incident from time to time //NOTE: should not use the timer component, rather than the Windows timer FORMS of //The class timer named System.Timers.Timer, never made a mistake, otherwise the implementation will not be normal bStart = true; Thread thConvert = new Thread (new ThreadStart (StartConvertData)); thConvert.Start (); )
and then in the service end of the incident, set up to stop the logo thread bStart = false protected override void OnStop () ( bStart = false; //Why the thread does not stop here, because, taking into account, and now the thread WORD document is converted, but not ended, it only set up to stop labeling, conversion is completed, the implementation of the end of the thread also. ) |