1
A
回答
1
我還沒有測試從另一個線程打印,但這兩個選項之一應該工作。您可能需要修改代碼與.net 2的工作,因爲我只用3.5 SP1或4
假設你有一個打印方法和隊列,其中ItemToPrint是類持有的打印設置/信息
public Queue<ItemToPrint> PrintQueue = new Queue<ItemToPrint>();
private BackgroundWorker bgwPrintWatcher;
public void SetupBackgroundWorker()
{
bgwPrintWatcher = new BackgroundWorker();
bgwPrintWatcher.WorkerSupportsCancellation = true;
bgwPrintWatcher.ProgressChanged += new ProgressChangedEventHandler(bgwPrintWatcher_ProgressChanged);
bgwPrintWatcher.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgwPrintWatcher_RunWorkerCompleted);
bgwPrintWatcher.DoWork += new DoWorkEventHandler(bgwPrintWatcher_DoWork);
bgwPrintWatcher.RunWorkerAsync();
}
void bgwPrintWatcher_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
while (!worker.CancellationPending)
{
// Prevent writing to queue while we are reading/editing it
lock (PrintQueue)
{
if (PrintQueue.Count > 0)
{
ItemToPrint item = PrintQueue.Dequeue();
// Two options here, you can either sent it back to the main thread to print
worker.ReportProgress(PrintQueue.Count + 1, item);
// or print from the background thread
Print(item);
}
}
}
}
private void Print(ItemToPrint item)
{
// Print it here
}
private void AddItemToPrint(ItemToPrint item)
{
lock (PrintQueue)
{
PrintQueue.Enqueue(item);
}
}
void bgwPrintWatcher_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// Anything here will run from the main/original thread
// PrintQueue will no longer be watched
}
void bgwPrintWatcher_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// Anything here will run from the main/original thread
ItemToPrint item = e.UserState as ItemToPrint;
// e.ProgressPercentage holds the int value passed as the first param
Print(item);
}
0
您可以使用FileSystemWatcher類。
相關問題
- 1. 如何使用Windows服務打印PDF文件
- 2. 從Windows服務/ C#.NET打印
- 3. 從Windows服務打印
- 4. 從Windows服務打印
- 5. Windows服務打印PDF
- 6. Windows服務打印行爲
- 7. 在Windows服務中打印
- 8. Windows服務作爲「打印服務器」
- 9. 使用C#打印文件
- 10. 如何通過使用C#窗口服務通過打印機打印數據打印文本文件
- 11. 打印excel,使用windows服務的pdf文檔
- 12. 無法使用Java打印機打印文檔服務(JPS)
- 13. 通過Windows服務發送PDF文件到打印機
- 14. 從WCF數據服務打印文件
- 15. 服務器打印RTF文件
- 16. C#Windows服務打開文件對於用戶
- 17. 使用PHP打印到共享的Windows打印機(Linux PHP服務器)
- 18. 從Windows服務的XPS打印
- 19. Python/perl打印服務器;編寫打印作業到文件
- 20. 從Windows打印服務器輪詢打印機信息
- 21. 使用Windows服務(c#)
- 22. 在Windows中使用java找不到打印服務
- 23. 在使用後臺工作的Windows服務中打印
- 24. 使用C/C++在Windows中打印HTML文檔?
- 25. 在windows服務中使用C++獲取默認的打印機更改通知
- 26. Google雲打印或其他服務使用C#或PHP自動打印
- 27. 打印文件夾和文件遞歸使用Windows批量
- 28. 使用WCF服務打印信息
- 29. 服務器端PDF使用Flash打印?
- 30. 如何使用C#.NET中的Windows服務調用Windows服務?