1

我正在使用 C# 启动 DISM 进程以捕获/应用图像并跟踪进度。

当我在 Windows 10 上运行代码时,进度正在正常更新,但是当我在 WinPE 环境中运行代码时,进度没有更新,但进程仍在正常运行。

这是触发方法启动 DISM 的代码。请注意它在 .NET Framework 4.0 上运行

Task T = Task.Factory.StartNew(() =>
                            {
                                ApplyImage(machineName, imageIndex, imageDescription);
                                //Utilities.BailOut();
                            });

这是我用来启动 DISM 的代码,参数根据需要而变化。

     public void ApplyImage(string imageName, int imageIndex)
            {
                string _imageName = imageName;
                int _index = imageIndex;
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
                {
                    UseShellExecute = true,
                    CreateNoWindow = true,
                    RedirectStandardOutput = true,
                    FileName = "Dism.exe",
                    Arguments = $@"/apply-image /imagefile:{OS_Deploy}\{_imageName} /index:{_index} /applydir:{TargetDisk}"
                };
                startInfo.UseShellExecute = false;
                process.StartInfo = startInfo;
                process.OutputDataReceived += (sender, e) => setLabelText(lblProgress, e.Data);
                process.Start();
                process.BeginOutputReadLine();

                process.WaitForExit();
                process.CancelOutputRead();
                process.Close();
            }

这是设置标签文本的代码

private void setLabelText(Label label, string text)
        {
            if (label.InvokeRequired)
            {
                label.Invoke((System.Action)(() => setLabelText(label, text)));
            }
            else
            {
                try
                {
                    if (text.Contains("%"))
                    {
                        label.Text = "Status : " + text.Split('%')[0].Substring(text.Split('%')[0].Length - 4, 4) + "%";
                    }
                    else if (text == "The operation completed successfully.")
                    {
                        label.Text = "Completed!";
                    }
                    else
                    {
                        label.Text = text;
                    }
                }
                catch (Exception) { }
            }
        }

是什么导致了这个问题,我可以用什么来解决它或者创建一个解决方法?

4

0 回答 0