Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用c#windows服务打印文件_C# - Fatal编程技术网

使用c#windows服务打印文件

使用c#windows服务打印文件,c#,C#,我想使用windows server自动打印目录中的文件。一旦任何文件被添加到目录中,它就会被自动打印出来。我们如何才能做到这一点 谢谢。我还没有测试过从其他线程打印,但这两个选项中的一个应该可以工作。您可能必须修改代码才能使用.NET2,因为我只使用3.5SP1或4 假设您有一个Print方法和一个队列,其中ItemToPrint是保存打印设置/信息的类 public Queue<ItemToPrint> PrintQueue = new Queue<ItemToPrint&

我想使用windows server自动打印目录中的文件。一旦任何文件被添加到目录中,它就会被自动打印出来。我们如何才能做到这一点


谢谢。

我还没有测试过从其他线程打印,但这两个选项中的一个应该可以工作。您可能必须修改代码才能使用.NET2,因为我只使用3.5SP1或4

假设您有一个Print方法和一个队列,其中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);
    }
public Queue PrintQueue=new Queue();
私人后台工作人员bgwPrintWatcher;
public void SetupBackgroundWorker()
{
bgwPrintWatcher=新的BackgroundWorker();
bgwPrintWatcher.workerSupportsScanCellation=true;
bgwPrintWatcher.ProgressChanged+=新的ProgressChangedEventHandler(bgwPrintWatcher\u ProgressChanged);
bgwPrintWatcher.RunWorkerCompleted+=新的RunWorkerCompletedEventHandler(bgwPrintWatcher\u RunWorkerCompleted);
bgwPrintWatcher.DoWork+=新的DoWorkEventHandler(bgwPrintWatcher\u DoWork);
bgwPrintWatcher.RunWorkerAsync();
}
无效bgwPrintWatcher\u DoWork(对象发送方,DoWorkEventArgs e)
{
BackgroundWorker worker=发件人作为BackgroundWorker;
而(!worker.CancellationPending)
{
//在读取/编辑队列时防止写入队列
锁(打印队列)
{
如果(PrintQueue.Count>0)
{
ItemToPrint item=PrintQueue.Dequeue();
//这里有两个选项,您可以将其发送回主线程进行打印
worker.ReportProgress(PrintQueue.Count+1,项);
//或从背景线程打印
印刷品(项目);
}
}
}
}
专用作废打印(ItemTopPrint item)
{
//在这里打印
}
私有void AddItemToPrint(ItemToPrint item)
{
锁(打印队列)
{
PrintQueue.Enqueue(项目);
}
}
void bgwPrintWatcher\u RunWorkerCompleted(对象发送方,RunWorkerCompletedEventArgs e)
{
//这里的任何内容都将从主线程/原始线程运行
//打印队列将不再被监视
}
void bgwPrintWatcher\u ProgressChanged(对象发送方,ProgressChangedEventArgs e)
{
//这里的任何内容都将从主线程/原始线程运行
ItemToPrint item=e.UserState为ItemToPrint;
//e.ProgressPercentage保存作为第一个参数传递的int值
印刷品(项目);
}

我还没有测试从另一个线程打印,但这两个选项之一应该可以工作。您可能必须修改代码才能使用.NET2,因为我只使用3.5SP1或4

假设您有一个Print方法和一个队列,其中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);
    }
public Queue PrintQueue=new Queue();
私人后台工作人员bgwPrintWatcher;
public void SetupBackgroundWorker()
{
bgwPrintWatcher=新的BackgroundWorker();
bgwPrintWatcher.workerSupportsScanCellation=true;
bgwPrintWatcher.ProgressChanged+=新的ProgressChangedEventHandler(bgwPrintWatcher\u ProgressChanged);
bgwPrintWatcher.RunWorkerCompleted+=新的RunWorkerCompletedEventHandler(bgwPrintWatcher\u RunWorkerCompleted);
bgwPrintWatcher.DoWork+=新的DoWorkEventHandler(bgwPrintWatcher\u DoWork);
bgwPrintWatcher.RunWorkerAsync();
}
无效bgwPrintWatcher\u DoWork(对象发送方,DoWorkEventArgs e)
{
BackgroundWorker worker=发件人作为BackgroundWorker;
而(!worker.CancellationPending)
{
//在读取/编辑队列时防止写入队列
锁(打印队列)
{
如果(PrintQueue.Count>0)
{
ItemToPrint item=PrintQueue.Dequeue();
//这里有两个选项,您可以将其发送回主线程进行打印
worker.ReportProgress(PrintQueue.Count+1,项);
//或从背景线程打印
印刷品(项目);
}
}
}
}
专用作废打印(ItemTopPrint item)
{
//在这里打印
}
私有void AddItemToPrint(ItemToPrint item)
{
锁(打印队列)
{
PrintQueue.Enqueue(项目);
}
}
void bgwPrintWatcher\u RunWorkerCompleted(对象发送方,RunWorkerCompletedEventArgs e)
{
//这里的任何内容都将从主线程/原始线程运行
//打印队列将不再被监视
}
void bgwPrintWatcher\u ProgressChanged(对象发送方,ProgressChangedEventArgs e)
{
//这里的任何内容都将从主线程/原始线程运行
ItemToPrint item=e.UserState为ItemToPrint;
//e.ProgressPercentage保存作为第一个参数传递的int值
印刷品(项目);
}

您可以使用FileSystemWatcher类


您可以使用FileSystemWatcher类