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服務打印
- 2. Windows服務打印行爲
- 3. 在Windows服務中打印
- 4. Windows服務打印PDF
- 5. 從Windows服務打印
- 6. Windows服務作爲「打印服務器」
- 7. 如何通過使用C#窗口服務通過打印機打印數據打印文本文件
- 8. 打印excel,使用windows服務的pdf文檔
- 9. 無法使用Java打印機打印文檔服務(JPS)
- 10. 通過Windows服務發送PDF文件到打印機
- 11. 從WCF數據服務打印文件
- 12. C#Windows服務打開文件對於用戶
- 13. 使用PHP打印到共享的Windows打印機(Linux PHP服務器)
- 14. 從Windows服務的XPS打印
- 15. Python/perl打印服務器;編寫打印作業到文件
- 16. 從Windows打印服務器輪詢打印機信息
- 17. 如何使用打印機的Web C#我打印JPG文件
- 18. 使用Windows服務(c#)
- 19. 在使用後臺工作的Windows服務中打印
- 20. Google雲打印或其他服務使用C#或PHP自動打印
- 21. 打印文件夾和文件遞歸使用Windows批量
- 22. 使用WCF服務打印信息
- 23. 服務器端PDF使用Flash打印?
- 24. Android打印使用自定義打印服務
- 25. 如何使用C#.NET中的Windows服務調用Windows服務?
- 26. 使用windows服務創建文件
- 27. 在Windows服務中使用ocx文件
- 28. 保存文件使用Windows服務
- 29. C服務器 - 打印接收按摩?
- 30. 打印我的TCP TCP服務器C