在命令 window while 运行 c# 程序中的 sqlcmd 进程中打印信息

Printing information in the command window while running a sqlcmd process in a c# program

我 运行 我的 c# wpf 程序的更新过程是这样的:

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

变量参数是 build 来自 ui 中的用户输入,这里是一个例子:

sqlcmd.exe -S .\SQLEXPRESS -d mydatabase -v db_src = "db\file.bak" -i db\update.sql -o "\log\log_update.txt"

现在黑色 cmd window 打开并 运行 等待一段时间(1-5 分钟)。在某些机器上,白色 curos 闪烁。我想要的是 "Please wait... & " 之类的东西打印在 cmd window.

我尝试了不同的方法,但 sqlmd 删除了所有内容。就像在参数前面放一个 echo Please wait... 一样。

在 c# 程序中显示某些内容也很困难,因为在处理 运行s 时,程序被冻结。但是,如果有人知道可以在程序中显示消息的解决方案,那也很好。

使用 .WaitForExit() 会阻塞您当前的应用程序线程。相反,您应该在后台启动该进程,然后等待 Exited 事件。

引用MSDN - see the remarks:

WaitForExit() makes the current thread wait until the associated process terminates. It should be called after all other methods are called on the process. To avoid blocking the current thread, use the Exited event.

如果您的线程未被阻止,您可以编写 progress/status 条实际会显示的消息。