CS0122 Form1.AvgWaiting 由于其保护级别和 C# Windows 窗体中的甘特图而无法访问

问题描述

我目前正在为我的学校项目创建一个 cpu 调度模拟器程序。目前我有错误

CS0122 Form1.AvgWaiting 由于其保护级别而无法访问

当我将 TextBox 从 Form1Designer.cs 更改为 public 时,出现以下错误

CS0120 C# 非静态字段、方法属性需要对象引用

我有 Form1.cs 和一个单独的类用于我的算法。我将使用我的算法来显示我的 datagridview 和文本框的输出

我的 Form1.cs

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;

namespace cpu_Scheduling_Simulator
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        // set default values to comboBox

        foreach (Control cont in this.Controls)
        {
            if (cont is ComboBox)
            {
                ((ComboBox)cont).Selectedindex = 0;
            }
        }
    }

    public void Form1_Load(object sender,EventArgs e)
    {

    }

    public void groupBox2_Enter(object sender,EventArgs e)
    {

    }

    public void dataGridView1_CellContentClick(object sender,DataGridViewCellEventArgs e)
    {

    }

    public void button1_Click(object sender,EventArgs e)
    {
        Close();
    }

    public void buttonGenerate_Click(object sender,EventArgs e)
    {
        dataGridView1.Rows.Clear();
        int count = int.Parse(comboBoxProcess.GetItemText(comboBoxProcess.SelectedItem));

        
        if (comboBoxProcess.Selectedindex == -1)
            MessageBox.Show("Please select num of Process");           

        else
        {

        dataGridView1.Rows.Add(count);

            for (int i = 0; i < count; i++)
            {
            dataGridView1.Rows[i].Cells["Processes"].Value = "" + (i+1);
            }
        }
    }

    public void buttonClear_Click(object sender,EventArgs e)
    {
        foreach (Control control in this.Controls)
        {
            if (control is TextBox)
                ((TextBox)control).Text = null;
        }

        dataGridView1.Rows.Clear();
    }

    public void groupBox5_Enter(object sender,EventArgs e)
    {

    }

    public void buttonSimulate_Click(object sender,EventArgs e)
    {

        int count = int.Parse(comboBoxProcess.GetItemText(comboBoxProcess.SelectedItem));    

            int index = comboBoxAlgorithm.Selectedindex;

            switch (index)
            {
                case 0:

                    // Process id's 
                    int[] processes = new int[count];
                    int n = processes.Length;

                    // Burst time of all processes 
                    int[] burst_time =  new int[n];
                    for (int x = 0; x < n; x++)
                        burst_time[x] = int.Parse((string)dataGridView1.Rows[x].Cells["BurstTime"].Value);


                    // Arrival time of all processes 
                    int[] arrival_time = new int[n];
                    for (int x = 0; x < n; x++)
                        arrival_time[x] = int.Parse((string)dataGridView1.Rows[x].Cells["ArrivalTime"].Value);

                    FCFS.findavgTime(processes,n,burst_time,arrival_time);

                    break;

                default:
                    MessageBox.Show("Please select an Algorithm");

                break;
            }

        
        
    }
}

}

我的 FCFS.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


  namespace cpu_Scheduling_Simulator
  {
public class FCFS
{
    // Function to find the waiting time for all 
    // processes 
    public static void findWaitingTime(int[] processes,int n,int[] bt,int[] wt,int[] at)
    {
        int[] service_time = new int[n];
        service_time[0] = 0;
        wt[0] = 0;

        // calculating waiting time 
        for (int i = 1; i < n; i++)
        {
            // Add burst time of prevIoUs processes 
            service_time[i] = service_time[i - 1] + bt[i - 1];

            // Find waiting time for current process = 
            // sum - at[i] 
            wt[i] = service_time[i] - at[i];

            // If waiting time for a process is in negative 
            // that means it is already in the ready queue 
            // before cpu becomes idle so its waiting time is 0 
            if (wt[i] < 0)
                wt[i] = 0;
        }
    }

    // Function to calculate turn around time 
    public static void findTurnAroundTime(int[] processes,int[] tat)
    {
        // Calculating turnaround time by adding bt[i] + wt[i] 
        for (int i = 0; i < n; i++)
            tat[i] = bt[i] + wt[i];
    }

    // Function to calculate average waiting and turn-around 
    // times. 
    public static void findavgTime(int[] processes,int[] at)
    {
        int[] wt = new int[n]; int[] tat = new int[n];

        // Function to find waiting time of all processes 
        findWaitingTime(processes,bt,wt,at);

        // Function to find turn around time for all processes 
        findTurnAroundTime(processes,tat);

        // display processes along with all details 
        //Console.Write("Processes " + " Burst Time " + " Arrival Time "
        //    + " Waiting Time " + " Turn-Around Time "
        //    + " Completion Time \n");
        int total_wt = 0,total_tat = 0;
        for (int i = 0; i < n; i++)
        {
            total_wt = total_wt + wt[i];
            total_tat = total_tat + tat[i];
            int compl_time = tat[i] + at[i];

            //Console.WriteLine(i + 1 + "\t\t" + bt[i] + "\t\t"
            //    + at[i] + "\t\t" + wt[i] + "\t\t "
            //    + tat[i] + "\t\t " + compl_time);


            Form1.dataGridView1.Rows[i].Cells["Processes"].Value = i + 1;
            Form1.dataGridView1.Rows[i].Cells["BurstTime"].Value = bt[i];
            Form1.dataGridView1.Rows[i].Cells["ArrivalTime"].Value = at[i];
            Form1.dataGridView1.Rows[i].Cells["WaitingTime"].Value = wt[i];
        }

        //Console.Write("Average waiting time = "
        //    + (float)total_wt / (float)n);
        //Console.Write("\nAverage turn around time = "
        //    + (float)total_tat / (float)n);

        Form1.AvgWaiting.Text = ""+(float)total_wt / (float)n);
        Form1.AvgTurnaround.Text ""+(float)total_tat / (float)n);
    }

    // Driver code 


}

}

也有人可以将我链接到 C# 中的工作甘特图和就绪队列源代码,以便我可以研究它。我在 youtube 上找到了一个,但它是 Java link

顺便说一句,非常感谢你帮助像我这样的新手。我知道我有很多东西要学,有了你们的帮助,我学起来更容易了。

解决方法

Form1 是类名,而不是该类的实例。如果要引用现有实例,则需要将实例传递到需要的地方或从 Application.OpenForms 集合中检索它

例如你可以在使用 Form1 之前写这个

var f1 = Application.OpenForms.OfType<Form1>().FirstOrDefault();
if(f1 != null)
{
    // use f1 wherever you use Form1.
}

如果您在同一时间范围内只打开了一个 Form1 实例,这将起作用。如果您有多个实例,则需要将当前 Form1 的实例传递给 FCFS 方法

FCFS.findavgTime(processes,n,burst_time,arrival_time,this);
break;

public static void findavgTime(int[] processes,int n,int[] bt,int[] at,Form1 f1)
{
     // and again use f1 instead of Form1