静默执行进程/程序

发布于 2025-01-05 19:51:43 字数 1762 浏览 0 评论 0原文

我正在创建一个需要先决条件的 wpf 应用程序。如果不满足该先决条件,那么我会询问用户是否愿意安装恰好是的先决条件: Microsoft Visual C++ 2010 SP1 可再发行组件包

因此,如果用户选择安装先决条件,我将执行 vcredist_x86.exe(即从我提供的第一个链接下载的文件)。

然后在我的应用程序上,我将能够通过执行以下操作来判断安装何时完成:

ProcessStartInfo psi = new ProcessStartInfo(@"vcredist_x86.exe");
var p = new Process(); p.StartInfo = psi; 

p.Start(); //start the process
p.WaitForExit(); // wait for the installation to finish

// installation should be done now

好的,到目前为止一切都很好。 问题是我的 wpf 应用程序中有一个进度条,我想在其中显示进度。

我已经能够通过执行以下操作来显示安装进度:

有一个名为 AutoIt 的程序,它非常适合自动执行简单的任务。例如,我可以使用以下内容轻松检测是否存在带有 autoit 的窗口:在此处输入图像描述

然后我可以编译它脚本并创建一个非常小的可执行文件。 在该可执行文件中,如果指定的窗口存在,我将返回 1,否则返回 0。

当用户移动到下一个窗口时,我的脚本可能会返回 2,因为那是一个不同的窗口。 AutoIt还可以看到窗口进度条的进度!例如,如果该脚本返回 80,那么我会将进度更新为 80%。

我通过执行以下操作来做到这一点:

// start the autoitExecutable....

// wait for executable to exit usually takes 10 miliseconds it is fast

if (autoitProcess.ExitCode == 1)
{
   // do somthing
}else if(autoitProcess.ExitCode == 2)
{
   // do something else
 } //etc....

如您所见,我必须每 1 秒执行一次该脚本来检测已完成哪些更改,以便更新 WPF 中的进度栏​​。这可行,但每次我用 c# 执行该可执行文件时,我都会得到:

在此处输入图像描述

光标大约 500 毫秒,然后打开下一秒又出现了。即使没有窗口出现,这也会变得烦人。如果我能够摆脱该光标并以某种方式静默执行该可执行文件,那就太好了。当我执行 autoit 可执行文件时,没有显示任何窗口,也没有其他任何内容。

PS

我知道我可以使用 c# 来检查窗口是否存在,也许可以查看其他窗口处理程序的值,就像 autoit 能够做到的那样,但是使用 AutoIt 创建这些程序非常简单,如果我可以使用 AutoIt 而不是 C# 来完成此类任务

I am creating a wpf application that needs a prerequisite. If that prerequisites is not met then I ask the user if he will like to install the prerequisite that happens to be: Microsoft Visual C++ 2010 SP1 Redistributable Package.

So if the user chose to install the prerequisite I will execute vcredist_x86.exe (that is the file that get's downloaded from the first link that I provided).

Then on my application I will be able to tell when the installation is complete by doing something like:

ProcessStartInfo psi = new ProcessStartInfo(@"vcredist_x86.exe");
var p = new Process(); p.StartInfo = psi; 

p.Start(); //start the process
p.WaitForExit(); // wait for the installation to finish

// installation should be done now

Ok everything works great so far. The problem is that I have a progress bar in my wpf application and I will like to show the progress in there.

I have been able to show the progress of the installation by doing the following:

There is a program called AutoIt that it is great for automating simple tasks. For example I can easily detect if a window exists with autoit by using something like:enter image description here

I could then compile that script and create a very small executable. In that executable I will return 1 if the specified window exists or 0 otherwise.

When the user moves to the next window my script might return 2 because that is a different window. AutoIt can also see the progress of a progress bar of a window!!! so if that script returns 80 for example then I will update my progress to 80% for instance.

I do that by doing something like:

// start the autoitExecutable....

// wait for executable to exit usually takes 10 miliseconds it is fast

if (autoitProcess.ExitCode == 1)
{
   // do somthing
}else if(autoitProcess.ExitCode == 2)
{
   // do something else
 } //etc....

As you can see I have to execute that script every 1 second to detect what changes have been done in order to update my progress bar in WPF. That works but every time I execute that executable with c# I get the:

enter image description here

cursor for about 500 milliseconds then on the next second it appears again. That becomes annoying even though no windows show up. It will be nice if I could get rid of that cursor and execute that executable silently somehow. when I execute the autoit executable there are no windows that show up nor nothing else.

P.S.

I know I could use c# to check for the existance of a window and maybe see the value of other window's handler's just like autoit is able to do it but it is so simple to create those programs with AutoIt and it will be nice if I could use AutoIt instead of C# for this kind of taks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

是伱的 2025-01-12 19:51:43

当 exe 设置为“Windows 应用程序”而不是“控制台应用程序”时,我看到了这种行为。

将类型更改为控制台不再在启动时出现繁忙的光标。

I saw this behavior when the exe was set to "windows application" rather than "console application".

Changing the type to console no longer gives a busy cursor at launch.

情绪操控生活 2025-01-12 19:51:43

您也可以添加一个事件处理程序,例如

System.Diagnostics.ProcessStartInfo p = new 
     System.Diagnostics.ProcessStartInfo(@"vcredist_x86.exe") ;
p.Arguments="-RunForever";
proc = new System.Diagnostics.Process();
proc.StartInfo = p;
proc.EnableRaisingEvents = true;
proc.Exited += new EventHandler(myProcess_Exited);
proc.Start();

在事件内部,如果我想做这样的事情

// Inside the form class:

private System.Diagnostics.Process proc;

private void myProcess_Exited(object sender, System.EventArgs e)
{
    button3.BackColor=Color.LightGreen;  //success indicator
}

,如果您想在 While 循环中执行此操作,您也可以这样做,例如
但你必须更改参数以适合你的情况
您可以使用的示例代码

while(!autoitProcess.WaitForExit(someTimeout))
{ 
   if(ShouldCancel)
   {
      break;
   }
}

这是否有意义或有帮助......?

You could add an event handler as well for example

System.Diagnostics.ProcessStartInfo p = new 
     System.Diagnostics.ProcessStartInfo(@"vcredist_x86.exe") ;
p.Arguments="-RunForever";
proc = new System.Diagnostics.Process();
proc.StartInfo = p;
proc.EnableRaisingEvents = true;
proc.Exited += new EventHandler(myProcess_Exited);
proc.Start();

inside the event if I wanted to do something like this

// Inside the form class:

private System.Diagnostics.Process proc;

private void myProcess_Exited(object sender, System.EventArgs e)
{
    button3.BackColor=Color.LightGreen;  //success indicator
}

if you wanted to do this in a While Loop you could also do something like this for example
but you would have to change the params to fit your case
example code you can utilize

while(!autoitProcess.WaitForExit(someTimeout))
{ 
   if(ShouldCancel)
   {
      break;
   }
}

does this make sense or help out...?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文