Подсистема управления процессами
Курсовой проект - Компьютеры, программирование
Другие курсовые по предмету Компьютеры, программирование
{
protected Thread a;
bool isWorking = false;
public event EventHandler WorkingStateChanged = delegate { };
public bool IsWorking
{
get { return isWorking; }
set
{
isWorking = value;
WorkingStateChanged(this, EventArgs.Empty);
}
}
public void Delete()
{
if (a != null)
{
a.Abort();
a = null;
if (IsWorking == true)
{
if (WaitToStart.Set() == false)
WaitToStart.Set();
}
}
}
public ThreadPriority Prior
{
get { return a.Priority; }
set { a.Priority = value; }
}
public void Stop()
{
a.Abort();
if(isWorking == true)
WaitToStart.Set();
}
private DataGridView data;
public delegate void ChangeStateEventHandler(string msg);
public static event ChangeStateEventHandler change;
public abstract string GetType();
public string GetState()
{
return IsWorking ? "Работает" : "Не работает";
}
public string GetPriority()
{
return a.Priority.ToString();
}
public void ChangeState()
{
if (IsWorking == false)
{
IsWorking = true;
}
else
{
IsWorking = false;
}
}
public abstract void Base();
private Control pReporter;
public DataGridView reporterD
{
get
{
return data;
}
set
{
data = value;
}
}
public Control reporter
{
get
{
return pReporter;
}
set
{
pReporter = value;
}
}
public EventWaitHandle SwaitTostart
{
set
{
WaitToStart = value;
}
}
protected Stopwatch timer = new Stopwatch();
public void Start()
{
a = new Thread(Base);
a.Start();
}
delegate void SetTextDelegate2(string Text);
public static EventWaitHandle WaitToStart;
public void SetText2(string Text)
{
if (reporter.InvokeRequired)
{
SetTextDelegate2 a = new SetTextDelegate2(SetText2);
reporter.Invoke(a, new object[] { Text });
}
else reporter.Text += Text;
}
public void Restart()
{
if(isWorking == true)
ChangeState();
timer = new Stopwatch();
a=new Thread(Base);
a.Start();
timer.Start();
}
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
namespace ProcManager
{
class FibbonProc:BetaProc
{
public readonly string Type = "Числа Фиббоначи";
public override string GetType()
{
return Type;
}
private int FSum = 1;
private int FSum2 = 1;
private int temp = 0;
public override void Base()
{
WaitToStart.WaitOne();
if (IsWorking == false)
ChangeState();
timer.Reset();
timer.Start();
do
{
if (FSum >= 1000)
{
FSum = 1;
FSum2 = 1;
}
temp = FSum;
FSum = FSum + FSum2;
FSum2 = temp;
SetText2(FSum.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 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);
a.Rows.Count-1))">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;
///
/// Возвращает приоритет процесса
///
///
///
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);
// расчёт процессорноного времени
HighestPriority)HighestPriority=process.Prior;">if (process.Prio