CS0122 Form1.AvgWaiting 由于其保护级别和 C# Windows 形式的甘特图而无法访问
CS0122 Form1.AvgWaiting is inaccessible due to its protection level and gantt chart in C# Windows Form
我目前正在为我的学校项目创建一个 CPU 调度模拟器程序。目前我有错误:
CS0122 Form1.AvgWaiting 由于其保护级别
而无法访问
当我将 TextBox 从 Form1Designer.cs 更改为 public 时,出现以下错误:
CS0120 C# 非静态字段、方法或 属性
需要对象引用
我的算法有 Form1.cs 和一个单独的 class。我将使用我的算法来显示我的 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 n, int[] bt,
int[] wt, 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 n, int[] bt, int[] at)
{
int[] wt = new int[n]; int[] tat = new int[n];
// Function to find waiting time of all processes
findWaitingTime(processes, n, bt, wt, at);
// Function to find turn around time for all processes
findTurnAroundTime(processes, n, bt, wt, 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
}
}
也有人可以 link 给我一个可用的甘特图和 C# 中的现成队列源代码,这样我就可以研究它了。我在 youtube 上找到了一个,但它在 Java link
顺便说一句,非常感谢你帮助像我这样的新手。我知道我有很多东西要学,在你的帮助下我学起来更容易了。
Form1 是 class 名称,不是那个 class 的实例。如果要引用现有实例,则需要在需要的地方传递实例或从 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
我目前正在为我的学校项目创建一个 CPU 调度模拟器程序。目前我有错误:
CS0122 Form1.AvgWaiting 由于其保护级别
而无法访问当我将 TextBox 从 Form1Designer.cs 更改为 public 时,出现以下错误:
CS0120 C# 非静态字段、方法或 属性
需要对象引用我的算法有 Form1.cs 和一个单独的 class。我将使用我的算法来显示我的 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 n, int[] bt,
int[] wt, 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 n, int[] bt, int[] at)
{
int[] wt = new int[n]; int[] tat = new int[n];
// Function to find waiting time of all processes
findWaitingTime(processes, n, bt, wt, at);
// Function to find turn around time for all processes
findTurnAroundTime(processes, n, bt, wt, 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
}
}
也有人可以 link 给我一个可用的甘特图和 C# 中的现成队列源代码,这样我就可以研究它了。我在 youtube 上找到了一个,但它在 Java link
顺便说一句,非常感谢你帮助像我这样的新手。我知道我有很多东西要学,在你的帮助下我学起来更容易了。
Form1 是 class 名称,不是那个 class 的实例。如果要引用现有实例,则需要在需要的地方传递实例或从 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