48068 (Подсистема управления процессами), страница 3
Описание файла
Документ из архива "Подсистема управления процессами", который расположен в категории "". Всё это находится в предмете "информатика" из 1 семестр, которые можно найти в файловом архиве . Не смотря на прямую связь этого архива с , его также можно найти и в других разделах. Архив можно найти в разделе "курсовые/домашние работы", в предмете "информатика, программирование" в общих файлах.
Онлайн просмотр документа "48068"
Текст 3 страницы из документа "48068"
Restart();
WaitToStart.Set();
}
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
namespace ProcManager
{
class ProcRandom:BetaProc
{
Random a = new Random();
private int res;
public readonly string Type = "Случайное число";
public override string GetType()
{
return Type;
}
public override void Base()
{
WaitToStart.WaitOne();
if (IsWorking == false)
ChangeState();
timer.Reset();
timer.Start();
do
{
res = a.Next(100);
SetText2(res.ToString()+"\r\n");
Thread.Sleep(1110);
}
while (timer.ElapsedMilliseconds <= 3000);
Restart();
WaitToStart.Set();
}
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
namespace ProcManager
{
class SinProc:BetaProc
{
private double x = 1;
public readonly string Type = "Синус X";
public override string GetType()
{
return Type;
}
public override void Base()
{
WaitToStart.WaitOne();
if(IsWorking == false)
ChangeState();
timer.Reset();
timer.Start();
do
{
x = Math.Sin(x);
SetText2(Math.Round(x, 3).ToString()+"\r\n");
Thread.Sleep(1110);
}
while (timer.ElapsedMilliseconds <= 3000);
Restart();
WaitToStart.Set();
}
}
}
using System.Collections;
using System.Threading;
using System.Windows.Forms;
using System;
namespace ProcManager
{
class ClassProcManager
{
private BetaProc[] mas = new BetaProc[30];
private DataGridView a;
private int index = 0;
public BetaProc[] ReturnMas()
{
return mas;
}
public int Index()
{
return index;
}
public DataGridView reporterD
{
get
{
return a;
}
set
{
a = value;
}
}
public void AddThread(BetaProc a)
{
if (index < mas.Length)
{
mas[index] = a;
}
else
MessageBox.Show("Слишком много процессов");
}
public void ShowInDataView(BetaProc b)
{
a.Rows.Add(index + 1, b.GetType(), b.GetState(), b.GetPriority());
index++;
}
public void SetWaitProperty(BetaProc b)
{
int i = Array.IndexOf(mas, b);
if((ia.Rows.Count - 1))
return;
for (int s = 0; s < index; s++)
{
if ((int)a.Rows[s].Cells[0].Value == i+1)
{
DataGridViewRow row = a.Rows[s];
row.Cells[2].Value = b.GetState();
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace ProcManager
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int index = 0;
private ClassProcManager manager = new ClassProcManager();
private EventWaitHandle wh1 = new AutoResetEvent(true);
private RadioGroup processType;
private RadioGroup processPriority;
private ThreadPriority[] ProcessPriorities = new ThreadPriority[30];
ThreadPriority HighestPriority = ThreadPriority.Lowest;
///
/// Возвращает приоритет процесса
///
/// Tag
/// Объект из перечисления ThreadPriority
private ThreadPriority IndexToPriority(int priority)
{
switch (priority)
{
case 0: return ThreadPriority.Lowest;
case 1: return ThreadPriority.BelowNormal;
case 2: return ThreadPriority.Normal;
case 3: return ThreadPriority.AboveNormal;
case 4: return ThreadPriority.Highest;
default: return ThreadPriority.Normal;
}
}
private void button1_Click(object sender, EventArgs e)
{
BetaProc process;
switch (processType.SelectedButton)
{
case 0: process = new FibbonProc();
break;
case 1: process = new ProcRandom();
break;
case 2: process = new SinProc();
break;
default: process = new ProcRandom();
break;
}
process.SwaitTostart = wh1;
process.reporter = richTextBox1;
process.reporterD = dataGridView1;
process.Start();
process.Prior = IndexToPriority(processPriority.SelectedButton);
manager.AddThread(process);
manager.ShowInDataView(process);
process.WorkingStateChanged += new EventHandler(a_WorkingStateChanged);
// расчёт процессорноного времени
if (process.Prior > HighestPriority) HighestPriority = process.Prior;
ProcessPriorities[index] = process.Prior;
if (index >= 1)
{
double FreeProcessorTime = 100;
double TimePerProcess = 100 / (index + 1);
double PriorityWeight = 0;
int HighPriorityProcessCount = 0;
// расчёт для процессов с приоритетом ниже самого большого
for (int i = 0; i < index + 1; i++)
{
if (ProcessPriorities[i] != HighestPriority)
{
switch (ProcessPriorities[i])
{
case ThreadPriority.Lowest: PriorityWeight = 0.2;
break;
case ThreadPriority.BelowNormal: PriorityWeight = 0.4;
break;
case ThreadPriority.Normal: PriorityWeight = 0.6;
break;
case ThreadPriority.AboveNormal: PriorityWeight = 0.8;
break;
}
FreeProcessorTime -= TimePerProcess * PriorityWeight;
dataGridView1.Rows[i].Cells[4].Value = Math.Round(TimePerProcess * PriorityWeight);
}
else HighPriorityProcessCount++;
}
// расчёт для процессов с самым большим приоритетом
for (int i = 0; i < index + 1; i++)
{
if (ProcessPriorities[i] == HighestPriority)
{
dataGridView1.Rows[i].Cells[4].Value = Math.Round(FreeProcessorTime / HighPriorityProcessCount);
}
}
}
else dataGridView1.Rows[0].Cells[4].Value = "100";
index++;
}
void a_WorkingStateChanged(object sender, EventArgs e)
{
BetaProc b = sender as BetaProc;
manager.SetWaitProperty(b);
}
private void Form1_Load(object sender, EventArgs e)
{
manager.reporterD = dataGridView1;
// Заполняем RadioGroup processType и processPriority объектами RadioButton
RadioButton[] processTypeRadioButtons = new RadioButton[3];
for (int i = 0; i < groupBox1.Controls.Count; i++) processTypeRadioButtons[i] = (RadioButton)groupBox1.Controls[i];
RadioButton[] processPriorityRadioButtons = new RadioButton[5];
for (int i = 0; i < groupBox2.Controls.Count; i++) processPriorityRadioButtons[i] = (RadioButton)groupBox2.Controls[i];
processType = new RadioGroup(processTypeRadioButtons);
processPriority = new RadioGroup(processPriorityRadioButtons);
}
private void button2_Click(object sender, EventArgs e)
{
int processID = (int)dataGridView1.SelectedRows[0].Cells[0].Value - 1;
if (processID != -1 && (string)manager.reporterD.Rows[processID].Cells[3].Value != "Завершен")
{
manager.ReturnMas()[processID].Stop();
manager.reporterD.Rows[processID].Cells[2].Value = "Приостановлен";
}
}
private void button3_Click(object sender, EventArgs e)
{
int processID = (int)dataGridView1.SelectedRows[0].Cells[0].Value - 1;
if ((string)manager.reporterD.Rows[processID].Cells[2].Value == "Приостановлен")
{
manager.ReturnMas()[processID].Restart();
manager.reporterD.Rows[processID].Cells[2].Value = "Возобновлен";
}
}
private void button4_Click(object sender, EventArgs e)
{
int processID = (int)dataGridView1.SelectedRows[0].Cells[0].Value - 1;
if (processID != -1)
{
manager.ReturnMas()[processID].Delete();
manager.reporterD.Rows[processID].Cells[2].Value = "Завершен";
}
}
}
}