当使用退出事件启动时,在 C# 中启动 sqlcmd 进程不起作用

Starting sqlcmd process in C# doesn't work when startet with exited event

为什么这段代码有效?

Process sql = Process.Start("sqlcmd.exe", param);
sql.WaitForExit(21600000);

但是当我 运行 这段代码没有任何反应:

        Process sql = new System.Diagnostics.Process();
        sql.EnableRaisingEvents = true;
        sql.Exited += new EventHandler(sql_Exited);
        sql.StartInfo.FileName = @"sqlcmd.exe";
        sql.StartInfo.Arguments = param;
        sql.Start();

        this.Dispatcher.Invoke((Action)(() => { I_loader.Visibility = Visibility.Visible; })); 

退出事件:

        private void sql_Exited(object sender, System.EventArgs e)
        {
            eventHandled = true;
            this.Dispatcher.Invoke((Action)(() => { I_loader.Visibility = Visibility.Hidden; }));            
        }

变量参数具有以下值:

-S .\SQLEXPRESS -d mydatabase -v db_src = \"c:\temp\update.bak\" -i db\update.sql -o \"C:\myprogram\bin\Debug\log\log_update.txt\"

现在可以使用此代码。我真的说不出为什么或有什么区别。

   Process sql = new Process();   

   sql.StartInfo.CreateNoWindow = true;
   sql.StartInfo.UseShellExecute = true;
   sql.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
   sql.StartInfo.FileName = @"sqlcmd.exe";
   sql.StartInfo.Arguments = param;

   sql.EnableRaisingEvents = true;
   sql.Exited += new EventHandler(sql_Exited);
   sql.Start();