我收到错误消息“非静态字段,方法或属性'process.waitforexit()需要一个对象引用”。该如何纠正?

问题描述 投票:-1回答:2

我收到错误消息:“非静态字段,方法或属性'process.waitforexit()需要对象引用”

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace cpu_benchmark
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Process.Start("C:\\Users\\Reghunaath A A\\source\\repos\\cpu benchmark\\resources\\benchmark result\\bench.exe");
            Process.WaitForExit(100000);
            //StreamReader sr1 = new StreamReader("C:\\Users\\Reghunaath A A\\source\\repos\\cpu benchmark\\resources\\benchmark result\\count.txt");
            textBox1.Text = "hi";

        }
    }
}

c#
2个回答
1
投票

Start是静态方法,Start是实例方法。

WaitForExit方法返回WaitForExit类的一个实例,而该实例是您需要等待其退出的-因此您的代码应如下所示:

Start

0
投票
需要从对象的实例访问

方法。假设您像这样实例化Process类型的对象:

var process = Process.Start("C:\\Users\\Reghunaath A A\\source\\repos\\cpu benchmark\\resources\\benchmark result\\bench.exe"); process.WaitForExit(100000);
然后将实例方法称为O1。相反,

static

方法不需要对象实例,就像O1 myObject = new O1(); 在您的特定情况下,myObject.InstanceMethod()是静态方法,O1.StaticMethod()是实例方法。 Start方法返回WaitForExit类的对象实例,而该实例是您用来调用Start的实例,代码如下所示:

Process

© www.soinside.com 2019 - 2024. All rights reserved.