📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace test1
{
enum _suanfa{N,Y,S,A}
public partial class Form1 : Form
{
public prc ps;
public prc[] tp;
_suanfa suanfa;
int ppoint;
code_window cw;
Thread new_p;
public Form1()
{
InitializeComponent();
cw = new code_window();
cw.Width = this.Width;
new_p = new Thread(new ThreadStart(this.ThreadProcSafe));
new_p.Start();
ps = new prc("系统总资源");
for (int i = 0; i < 10; i++)
{
ps.total[i] = 100;
ps.have[i] = 100;
ps.need[i] = 0;
}
tp = new prc[50];
suanfa = _suanfa.N;
ppoint = 0;
string[] temp_p = new string[12];
temp_p[0] = "系统总资源";
temp_p[1] = "";
for (int i = 2; i < 12; i++)
{
temp_p[i] = "100";
}
ListViewItem temp_l = new ListViewItem(temp_p);
listView1.Items.Add(temp_l);
temp_p = new string[12];
temp_p[0] = "系统剩余资源";
temp_p[1] = "";
for (int i = 2; i < 12; i++)
{
temp_p[i] = "100";
}
temp_l = new ListViewItem(temp_p);
listView1.Items.Add(temp_l);
}
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Maximum = re_listview_count(2,0);
cw.clear();
for (int i = 2; i <= (listView1.Items.Count - 1); i++)
listView1.Items[i].SubItems[1].Text = "等待";
progressBar1.Value = 0;
switch (suanfa)
{
case _suanfa.Y: this.timer1.Tick += new System.EventHandler(this.bank); timer1.Start();
button2.Enabled = true;break;
case _suanfa.S: this.timer1.Tick += new System.EventHandler(this.suiji); timer1.Start();
button2.Enabled = true;break;
case _suanfa.A: this.timer1.Tick += new System.EventHandler(this.anxu); timer1.Start();
button2.Enabled = true;break;
case _suanfa.N: MessageBox.Show("请先在左上方选择实验算法!"); break;
}
}
private void yhj(object sender, EventArgs e)
{
this.请选择实验算法ToolStripMenuItem.Text = "银行家算法";
suanfa = _suanfa.Y;
}
private void rom(object sender, EventArgs e)
{
this.请选择实验算法ToolStripMenuItem.Text = "随机算法";
suanfa = _suanfa.S;
}
private void axfp(object sender, EventArgs e)
{
this.请选择实验算法ToolStripMenuItem.Text = "按序分配算法";
suanfa = _suanfa.A;
}
private void 添加新进程NToolStripMenuItem_Click(object sender, EventArgs e)
{
dlg_name dlg1 = new dlg_name();
dlg1.InitializeComponent();
dlg1.tb_name_1.Text = "进程"+(ppoint+1).ToString();
ps.total.CopyTo(dlg1.total,0);
dlg1.ShowDialog();
if (dlg1.stat)
{
try
{
tp[ppoint] = new prc(dlg1.tb_name_1.Text.ToString());
}
catch { MessageBox.Show("SH. is going wrong"); Close(); }
string[] temp_p = new string[12];
temp_p[0] = tp[ppoint].readname();
temp_p[1] = tp[ppoint].readstat();
for (int i = 2; i < 12; i++)
{
dlg1.res.CopyTo(tp[ppoint].total, 0);
temp_p[i] = dlg1.res[i - 2].ToString();
}
ListViewItem temp_l = new ListViewItem(temp_p);
listView1.Items.Add(temp_l);
label3_ppoint.Text = ((listView1.Items.Count)-2).ToString();
ppoint++;
}
}
private void 设置系统ToolStripMenuItem_Click(object sender, EventArgs e)
{
dlg_system dlg2 = new dlg_system();
dlg2.ShowDialog();
dlg2.res.CopyTo(ps.total,0);
dlg2.res.CopyTo(ps.have, 0);
for (int j = 0; j < 2; j++)
for (int i = 0; i < 10; i++)
{
listView1.Items[j].SubItems[i + 2].Text = dlg2.res[i].ToString();
}
}
private void 全部删除AToolStripMenuItem_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
string[] temp_p = new string[12];
temp_p[0] = "系统总资源";
temp_p[1] = "";
for (int i = 2; i < 12; i++)
{
temp_p[i] = "100";
}
ListViewItem temp_l = new ListViewItem(temp_p);
listView1.Items.Add(temp_l);
temp_p = new string[12];
temp_p[0] = "系统剩余资源";
temp_p[1] = "";
for (int i = 2; i < 12; i++)
{
temp_p[i] = "100";
}
temp_l = new ListViewItem(temp_p);
listView1.Items.Add(temp_l);
ppoint = 0;
label3_ppoint.Text = ((listView1.Items.Count) - 2).ToString();
}
private void 删除上个进程DToolStripMenuItem_Click(object sender, EventArgs e)
{
if (listView1.Items.Count > 2)
{ listView1.Items[listView1.Items.Count - 1].Remove(); ppoint--; }
else MessageBox.Show("还未加入任何进程");
label3_ppoint.Text = ((listView1.Items.Count) - 2).ToString();
}
public Array readprc()
{
return ps.total;
}
private void ThreadProcSafe()
{
Point pt = new Point(this.Location.X, this.Location.Y + this.Height);
cw.Location = pt;
cw.ShowDialog();
}
private void form1_close(object sender, FormClosingEventArgs e)
{
timer1.Stop();
timer1.Dispose();
new_p.Abort();
cw.cls();
}
private void cw_sh(object sender, EventArgs e)
{
cw.cw_sh();
}
private void suiji(object sender, EventArgs e)
{
int x, y, max,temp_1;
bool flg=false;
System.Random ro = new Random();
max = listView1.Items.Count;
for (x = 2; x <= max - 1; x++)
{
for (y = 2; y <= 11; y++)
{
int temp = Int16.Parse(listView1.Items[x].SubItems[y].Text.ToString());
int system_y = Int16.Parse(listView1.Items[1].SubItems[y].Text.ToString());
if (temp == 0)
continue;
else if (temp < 10)
{
listView1.Items[1].SubItems[y].Text = (system_y - temp).ToString();
if (int.Parse(listView1.Items[1].SubItems[y].Text.ToString()) < 0) { flg = true; break; }
listView1.Items[x].SubItems[y].Text = "0";
if (re_listview_count(x, x) == 0 && listView1.Items[x].SubItems[1].Text != "完成")
{
for (int m = 2; m < 12; m++)
listView1.Items[1].SubItems[m].Text = (Int16.Parse(listView1.Items[1].SubItems[m].Text.ToString()) + tp[x - 2].total[m - 2]).ToString();
listView1.Items[x].SubItems[1].Text = "完成";
}
tp[x - 2].have[y - 2] += temp;
if ((progressBar1.Maximum - progressBar1.Value) < temp) progressBar1.Value = progressBar1.Maximum;
else progressBar1.Value += temp;
}
else
{
temp_1 = temp;
temp = ro.Next(0, 10);
listView1.Items[1].SubItems[y].Text = (system_y - temp).ToString();
if (int.Parse(listView1.Items[1].SubItems[y].Text.ToString()) < 0) { flg = true; break; }
listView1.Items[x].SubItems[y].Text = (Int16.Parse(listView1.Items[x].SubItems[y].Text.ToString()) - temp).ToString();
if (re_listview_count(x, x) == 0 && listView1.Items[x].SubItems[1].Text != "完成")
{
for (int m = 2; m < 12; m++)
listView1.Items[1].SubItems[m].Text = (Int16.Parse(listView1.Items[1].SubItems[m].Text.ToString()) + tp[x - 2].total[m - 2]).ToString();
listView1.Items[x].SubItems[1].Text = "完成";
}
tp[x - 2].have[y - 2] += temp;
if ((progressBar1.Maximum - progressBar1.Value) < temp) progressBar1.Value = progressBar1.Maximum;
else progressBar1.Value += temp;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -