2013-03-14 95 views
1

我想使用windows server自動打印目錄中的文件。一旦任何文件被添加到目錄,它應該會自動打印。我們怎樣才能做到這一點?使用c#windows服務打印文件

謝謝。

回答

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); 
    }