【问题标题】:I get this error from my web service: error 1053 the service did not respond to the start or control request in a timely fashion我从我的 Web 服务收到此错误:错误 1053 服务未及时响应启动或控制请求
【发布时间】:2021-10-28 09:59:21
【问题描述】:
public partial class Service1 : ServiceBase
{
    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        while (true)
        {
            try
            {
                Print();
            }
            catch (Exception ex)
            {
                File.AppendAllText(@"C:\fold1\Log.txt", ex.ToString());
            }
        }
    }

    public static void Print()
    {
        //Print & Move the files after printing
        DirectoryInfo sourceinfo = new DirectoryInfo(@"C:\A");
        DirectoryInfo target = new DirectoryInfo(@"C:\A1");

        foreach (FileInfo fi in sourceinfo.GetFiles())
        {
            if (fi.Length != 0)
            {
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.Verb = "print";
                process.StartInfo.FileName = fi.FullName;
                process.StartInfo.UseShellExecute = true;
                process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                process.Start();

                if (!process.WaitForExit(1000))
                    process.Kill();
            }

            MoveFile(fi.FullName);
        }
    }

    public static void MoveFile(string Filename)
    {
        string SourcePath = @"C:\fold";
        string targetpath = @"C:\fold1";

        if (!Directory.Exists(targetpath))
        {
            Directory.CreateDirectory(targetpath);
        }

        string[] sourceFiles = Directory.GetFiles(SourcePath);

        foreach (string sourcefile in sourceFiles)
        {
            string mfilename = Path.GetFullPath(sourcefile);
            string mname = Path.GetFileName(sourcefile);

            if (mfilename == Filename)
            {
                string distnition = Path.Combine(targetpath, mname);
                File.Move(mfilename, distnition);
            }
        }
    }

    protected override void OnStop()
    {
        File.AppendAllText(@"C:\fold1\stop.txt", "Stop method");
    }
}

我正在尝试通过 Windows 服务静默打印文件,我使用此命令安装它:

SC CREATE "MyServiceName" binpath = "My service's path"

但我什至无法启动我的服务,我收到错误 1053。启动时间太长,启动时它只执行我的代码的一部分。

让我解释一下:我的代码应该打印一个目录中的 pdf 文件,然后将它们移动到另一个目录,但是当我收到此错误时,它只执行移动部分,它不打印文件。

【问题讨论】:

  • 将任何长时间运行的工作放在单独的线程中,OnStart 方法必须快速返回。
  • 已经做了,同样的问题
  • 发布更新的代码,如果是这样的话。
  • 我贴出来了,我把定时器从10秒减到1秒,还是一样的问题
  • OnStart 中有一个无限循环。您可以尝试使用FileSystemWatcher,但我怀疑您能否成功从服务打印 pdf 文件(使用 adobe 阅读器?)。

标签: c# windows service printing silent


【解决方案1】:

操作系统要求OnStart 快速返回(在几秒钟内),但您的OnStart 包含无限的while (true) 循环!

最简单的解决方案是将OnStart 的内容包装在一个lambda 表达式中,并使用Task 在单独的线程上运行它,从而允许OnStart 立即返回。此代码还使用cooperative cancellation 在调用OnStop 时优雅地退出任务。

private CancellationTokenSource cts;

protected override void OnStart(string[] args) {
    cts = new CancellationTokenSource(); // CTSs are only good for one cancel
    Task.Run(() => { // This lambda expression runs on a different thread
        while (true) {
            if (cts.IsCancellationRequested) return; // Check if we should stop
            ...
        }
    });
}

protected override void OnStop() {
    cts.Cancel(); // Tell the other thread it should stop
    cts.Dispose();
    ...
}

【讨论】:

  • 我删除了代码中的无限循环,它开始了,我不再收到错误但还有另一个问题:服务不打印文件,它只是移动它们!!!
  • 我阅读了你发给我的文档,但我有点困惑,因为我还是 C# 的初学者,有人可以帮忙完成这个项目吗?
  • @RamadanAbdinour 目前您的代码在终止打印进程之前等待一秒钟。你确定够长吗?您还应该让它在您弄清楚这一点时创建并显示一个窗口 - 控制台上可能会有有用的错误消息。
  • 我将等待时间升级为 18 秒,但我仍然遇到同样的问题,它跳过了打印部分,只是继续移动文件
  • 那么显然您的Print 方法有问题。您确定print 动词不会弹出确认对话框吗? (由于您设置了CreateNoWindowWindowStyle,您可能看不到?)
猜你喜欢
  • 2016-03-22
  • 1970-01-01
  • 2013-12-20
  • 1970-01-01
  • 2014-07-02
  • 1970-01-01
  • 2011-03-28
  • 2014-08-05
  • 2011-01-05
相关资源
最近更新 更多