简体   繁体   English

带有退出事件的startet时,无法在C#中启动sqlcmd进程

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

Why does this code work? 为什么此代码有效?

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

But when I run this code nothing happens: 但是,当我运行此代码时,什么也没发生:

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

Exited Event: 退出事件:

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

Variable param has the following value: 可变参数具有以下值:

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

It works now with this code. 现在可以使用此代码。 I can't really tell why or what's the difference though. 我真的不能说出为什么或有什么区别。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM