抑制“服务器忙...切换到/重试”对话框

San*_*ord 3 c# process inkscape

我正在使用 Inkscape 使用以下代码将图像从 PDF 转换为 SVG:

internal static void ConvertImageWithInkscapeToLocation(string baseImagePath, string newImagePath, bool crop = true)
{
    InkscapeAction(string.Format("-f \"{0}\" -l \"{1}\"", baseImagePath, newImagePath));
}

internal static void InkscapeAction(string inkscapeArgs)
{
    Process inkscape = null;

    try
    {
        ProcessStartInfo si = new ProcessStartInfo();
        inkscape            = new Process();

        si.WindowStyle      = System.Diagnostics.ProcessWindowStyle.Hidden;
        si.FileName         = inkscapePath; // the path to Inkscape.exe, defined elsewhere
        si.Arguments        = inkscapeArgs;
        si.CreateNoWindow   = true;
        inkscape.StartInfo  = si;

        inkscape.Start();
    }
    finally
    {
        inkscape.WaitForExit();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一个相当简单的“启动带参数的应用程序,等待关闭”设置,并且运行良好;唯一的问题是,在速度较慢的机器上,图像转换过程(大概是inkscape.WaitForExit())花费的时间太长,并且会显示以下对话框消息:

服务器繁忙对话框

单击“切换到...”会弹出 Windows 开始菜单(我猜测是因为我隐藏了该进程);“重试”将一遍又一遍地恢复该消息,直到该过程完成。 是否可以完全抑制消息框,并自动重试直到它通过?我可以至少延长显示消息之前的超时时间吗?

Gus*_*man 5

有一些方法可以做到这一点:

1-一种肮脏廉价的方法是等待超时(WaitForExit(timeout)),执行DoEvents(我假设您在主线程的winforms应用程序中执行此操作),检查进程是否完成并循环直到它:

finally
{
    while(!inkScape.HasExited)
    {
        inkscape.WaitForExit(500);
        Application.DoEvents();
    }

}
Run Code Online (Sandbox Code Playgroud)

2-正确的方法是在另一个线程中执行此操作,然后通知主程序继续

ThreadPool.QueueUserWorkItem((state) => { ConvertImageWithInkscapeToLocation...  });
Run Code Online (Sandbox Code Playgroud)

如果您在另一个线程中执行此操作,请记住 CrossThreadException,不要从该线程更新 UI。