English 中文(简体)
C#:点击按钮运行进程
原标题:C#: Running a process on button click
  • 时间:2011-05-29 03:59:25
  •  标签:
  • c#
  • wpf

我正在运行一个进程,点击我的WPF应用程序的按钮,如下所示:

private void btnGetValues_Click(object sender, RoutedEventArgs e)
{
    string arg1 = "1";
    Process p1 = new Process();
    p1.StartInfo.CreateNoWindow = true;
    p1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    p1.StartInfo.FileName = "myexe.exe";
    p1.StartInfo.Arguments = arg1;
    p1.StartInfo.UseShellExecute = false;
    p1.StartInfo.RedirectStandardOutput = true;
    p1.StartInfo.RedirectStandardInput = true;
    p1.StartInfo.RedirectStandardError = true;
    p1.Start();

    //while (!p1.HasExited)
    //{

    //}

    MessageBox.Show(p1.StandardOutput.ReadToEnd());
}

问题是,即使在流程执行之后,按钮仍会继续保持在单击状态。可能是什么问题?

最佳回答

为什么你需要最后一个信息框?读一读它就会奏效。如果您仍然遇到问题,那么您可以在单独的线程中运行它。这是一种同时执行多个任务的方式这里就是一个例子。

编辑

对不起,我理解错了。如果您希望按钮失去外观,可以将焦点设置为其他内容。

private void button1_Click(object sender, EventArgs e)
        {
            string arg1 = "1";
            Process p1 = new Process();
            p1.StartInfo.CreateNoWindow = true;
            p1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            p1.StartInfo.FileName = @"yourExecutable";
            p1.StartInfo.Arguments = arg1;
            p1.StartInfo.UseShellExecute = false;
            p1.StartInfo.RedirectStandardOutput = true;
            p1.StartInfo.RedirectStandardInput = true;
            p1.StartInfo.RedirectStandardError = true;
            p1.Start();
            //while (!p1.HasExited)
            //{

            // }

            MessageBox.Show(p1.StandardOutput.ReadToEnd());

            button2.Focus();  // set button 2 to have a height of 0 so it is not visible
            // or place it somewhere where it cannot be seen



        }
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签