Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows窗体冻结并禁用';不回答_C#_Windows_Forms_Windows Forms Designer - Fatal编程技术网

C# Windows窗体冻结并禁用';不回答

C# Windows窗体冻结并禁用';不回答,c#,windows,forms,windows-forms-designer,C#,Windows,Forms,Windows Forms Designer,我的实验室是关于时间表的,我使用改进的Kron算法。 按下按钮时,表单应在4个文本框上打印信息。 有时一切正常,但有时表格冻结,无法回答。如果我打开任务管理器窗口,我会看到我的应用程序占用了大约30%的CPU。也许是因为按钮上的循环?也许我应该多做些扣子?还是怎样我不明白。。 那么问题是什么 using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Dr

我的实验室是关于时间表的,我使用改进的Kron算法。 按下按钮时,表单应在4个文本框上打印信息。 有时一切正常,但有时表格冻结,无法回答。如果我打开任务管理器窗口,我会看到我的应用程序占用了大约30%的CPU。也许是因为按钮上的循环?也许我应该多做些扣子?还是怎样我不明白。。 那么问题是什么

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Windows.Forms;

namespace LW3_OTPR_Forms
{

    public partial class Form1 : Form
{
    public Form1()
    {
    InitializeComponent();
        textBoxZ.Text = "3";
        textBoxTasks.Text = "12";
        textBoxProcs.Text = "4";
        textBoxFrom.Text = "10";
        textBoxTo.Text = "20";
}
private void buttonGetResult_Click(object sender, EventArgs e)
{
        richTextBoxMatrix.Clear();
        richTextBox1.Clear();
        richTextBox2.Clear();
        richTextBox3.Clear();

        int z = Convert.ToInt32(textBoxZ.Text);
    for (int i = 0; i < z; i++)
        {
            richTextBox1.Text += "========= Z = " + (i+1) +" ===========";
            richTextBox1.Text += Environment.NewLine;
            richTextBox2.Text += "========= Z = " + (i + 1) + " ===========";
            richTextBox2.Text += Environment.NewLine;
            richTextBox3.Text += "========= Z = " + (i + 1) + " ===========";
            richTextBox3.Text += Environment.NewLine;
            Work.start1(Convert.ToInt32(textBoxTasks.Text), Convert.ToInt32(textBoxProcs.Text),                 
  Convert.ToInt32(textBoxFrom.Text), Convert.ToInt32(textBoxTo.Text));
            richTextBoxMatrix.Text += "Матрица " + (i+1);
            richTextBoxMatrix.Text += Environment.NewLine;
            richTextBoxMatrix.Text += Work.strWork;

            var processors = new Individ(Convert.ToInt32(textBoxTasks.Text),
                                         Convert.ToInt32(textBoxFrom.Text),
                                         Convert.ToInt32(textBoxTo.Text),
                                         Convert.ToInt32(textBoxProcs.Text));
            var procMonolit = new IndividMonolit(Convert.ToInt32(textBoxTasks.Text),
                                         Convert.ToInt32(textBoxFrom.Text),
                                         Convert.ToInt32(textBoxTo.Text),
                                         Convert.ToInt32(textBoxProcs.Text));
             var procKritWay = new IndividKritWay(Convert.ToInt32(textBoxTasks.Text),
                                         Convert.ToInt32(textBoxFrom.Text),
                                         Convert.ToInt32(textBoxTo.Text),
                                         Convert.ToInt32(textBoxProcs.Text));
            richTextBox1.Text += processors.print();
            richTextBox2.Text += procKritWay.printKritWay();
            richTextBox3.Text += procMonolit.printMonolit();
            processors.proc.balance();
            procKritWay.procKritWay.balanceKritWay();
            procMonolit.procMonolit.balanceMonolit();
            richTextBox1.Text += processors.proc.inf;
            richTextBox2.Text += procKritWay.procKritWay.infKritWay;
            richTextBox3.Text += procMonolit.procMonolit.infMonolit;
        }
}
}
static class Work
{
    public static string strWork;
    static public List<List<int>> arrayTask2d { set; get; }
    static public List<int> arrayTask { set; get; }
    static public void start1(int countWork, int countProc, int t1, int t2)
    {
        strWork = string.Empty;
        int temp = 0;
        arrayTask = new List<int>();
        arrayTask2d = new List<List<int>>();
        List<int> taskRow = new List<int>();

        for (int i = 0; i < countWork; i++)
        {
            temp = Constant.rnd.Next(t2 - t1) + t1 + 1;
            arrayTask.Add(temp);
            taskRow = new List<int>();
            for (int j = 0; j < countProc; j++)
            {
                taskRow.Add(temp);
            }
            arrayTask2d.Add(taskRow);
        }
        for (int i = 0; i < countWork; i++)
        {
            for (int j = 0; j < countProc; j++)
            {
                strWork += arrayTask2d[i][j].ToString() + " ";
            }
            strWork += Environment.NewLine;
        }
    }
}    
    class Proc
    {
public string inf;
    public string infMonolit;
    public string infKritWay;
    private List<List<int>> procs;

public List<int> this[int i]
{
    get
    {
        return procs[i];
    }

    set
    {
        procs[i] = value;
    }

}

public int CountProc
{
    get { return procs.Count; }
}

public int delta
{
    get
    {
        return procs.Max((a) => { return a.Sum(); }) - procs.Min((a) => { return a.Sum(); });
    }
}

public Proc(int countProc)
{
    inf = string.Empty;
        infMonolit = string.Empty;
        infKritWay = string.Empty;
        procs = new List<List<int>>();
    for (int i = 0; i < countProc; i++)
        procs.Add(new List<int>());
}

private int getIndexOfMax()
{
    int result = 0;
    for (int i = 0; i < procs.Count; i++)
    {
        if (procs[i].Sum() > procs[result].Sum())
            result = i;
    }
    return result;
}

public int getIndexOfMin()
{
    int result = 0;
    for (int i = 0; i < procs.Count; i++)
    {
        if (procs[i].Sum() < procs[result].Sum())
            result = i;
    }
    return result;
}

private void addInf()
{
    for (int i = 0; i < procs.Count; i++)
    {
        inf += " Процессор " + (i + 1).ToString()+ ": ";
        for (int j = 0; j < procs[i].Count; j++)
            inf += procs[i][j] + " ";
            inf += "|| sum = " + procs[i].Sum();
        inf += Environment.NewLine;
    }
    inf += "Δ = " + delta + Environment.NewLine;
}
    private void addInfMonolit()
    {
        for (int i = 0; i < procs.Count; i++)
        {
            infMonolit += " Процессор " + (i + 1).ToString() + ": ";
            for (int j = 0; j < procs[i].Count; j++)
                infMonolit += procs[i][j] + " ";
            infMonolit += "|| sum = " + procs[i].Sum();
            infMonolit += Environment.NewLine;
        }
        infMonolit += "Δ = " + delta + Environment.NewLine;
    }
    private void addInfKritWay()
    {
        for (int i = 0; i < procs.Count; i++)
        {
            infKritWay += " Процессор " + (i + 1).ToString() + ": ";
            for (int j = 0; j < procs[i].Count; j++)
                infKritWay += procs[i][j] + " ";
            infKritWay += "|| sum = " + procs[i].Sum();
            infKritWay += Environment.NewLine;
        }
        infKritWay += "Δ = " + delta + Environment.NewLine;
    }


    public void balance()
{

    bool flag = true;
    int max = 0;
    int min = 0;
    int dlt = 0;

    while (flag != false)
    {
        max = getIndexOfMax();
        min = getIndexOfMin();

        for (int i = 0; i < procs[max].Count; i++)
        {
            dlt = procs[max].Sum() - procs[min].Sum();
            if (procs[max][i] < dlt)
            {
                procs[min].Add(procs[max][i]);
                procs[max].RemoveAt(i);
                addInf();
                balance();
            }
        }

        for (int i = 0; i < procs[max].Count; i++)
        {
            for (int j = 0; j < procs[min].Count; j++)
            {
                if (procs[max][i] > procs[min][j] && procs[max][i] - procs[min][j] < delta)
                {
                    //меняем местами
                    int temp = procs[max][i];
                    procs[max][i] = procs[min][j];
                    procs[min][j] = temp;
                    addInf();
                    balance();
                }
            }
        }
        flag = false;
    }
}
    public void balanceKritWay()
    {

        bool flag = true;
        int max = 0;
        int min = 0;
        int dlt = 0;

        while (flag != false)
        {
            max = getIndexOfMax();
            min = getIndexOfMin();

            for (int i = 0; i < procs[max].Count; i++)
            {
                dlt = procs[max].Sum() - procs[min].Sum();
                if (procs[max][i] < dlt)
                {
                    procs[min].Add(procs[max][i]);
                    procs[max].RemoveAt(i);
                    addInfKritWay();
                    balanceKritWay();
                }
            }

            for (int i = 0; i < procs[max].Count; i++)
            {
                for (int j = 0; j < procs[min].Count; j++)
                {
                    if (procs[max][i] > procs[min][j] && procs[max][i] - procs[min][j] < delta)
                    {
                        //меняем местами
                        int temp = procs[max][i];
                        procs[max][i] = procs[min][j];
                        procs[min][j] = temp;
                        addInfKritWay();
                        balanceKritWay();
                    }
                }
            }
            flag = false;
        }
    }
    public void balanceMonolit()
    {

        bool flag = true;
        int max = 0;
        int min = 0;
        int dlt = 0;

        while (flag != false)
        {
            max = getIndexOfMax();
            min = getIndexOfMin();

            for (int i = 0; i < procs[max].Count; i++)
            {
                dlt = procs[max].Sum() - procs[min].Sum();
                if (procs[max][i] < dlt)
                {
                    procs[min].Add(procs[max][i]);
                    procs[max].RemoveAt(i);
                    addInfMonolit();
                    balanceMonolit();
                }
            }

            for (int i = 0; i < procs[max].Count; i++)
            {
                for (int j = 0; j < procs[min].Count; j++)
                {
                    if (procs[max][i] > procs[min][j] && procs[max][i] - procs[min][j] < delta)
                    {
                        //меняем местами
                        int temp = procs[max][i];
                        procs[max][i] = procs[min][j];
                        procs[min][j] = temp;
                        addInfMonolit();
                        balanceMonolit();
                    }
                }
            }
            flag = false;
        }
    }
}
    class Individ
    {
int numbJobs;
int numbProc;

private static uint CountIndivids { get; set; }
//массив для создания расписания
//(массив рандомных чисел каждое из которых соответствует заданию)
public List<int> arrayRandom { set; get; }
public List<int> arrayTasks { set; get; }

public List<int> arrayProc;

public Proc proc;
//public Proc procKritWay;
//максимальный порог рандома
int randLimit;
int x, y;


string inf;
public int Max
{
    get
    {
        return arrayProc.Max();
    }
}

public int NumberOfJobs
{
    get
    {
        return arrayTasks.Count;
    }
}

public int NumberOfProc
{
    get
    {
        return proc.CountProc;
    }
}

public int RandomLimit
{
    get
    {
        return randLimit;
    }
}

public Individ() { }

public Individ(Individ copy)
{
    arrayRandom = new List<int>(copy.arrayRandom);
    arrayTasks = new List<int>(copy.arrayTasks);
    //arrayProc = new List<int>(copy.arrayProc);
    numbJobs = copy.numbJobs;
    numbProc = copy.numbProc;
    randLimit = copy.randLimit;
    x = copy.x;
    y = copy.y;
    inf = copy.inf;
}

public Individ(int numberOfJobs, int X, int Y, int numberOfProc, int randomLimit = 255)
{
    numbJobs = numberOfJobs; numbProc = numberOfProc; randLimit = randomLimit; x = X; y = Y;
    arrayTasks = new List<int>(Work.arrayTask);
    arrayRandom = new List<int>();
    //заполнили массив рандомных чисел и работы
    for (int i = 0; i < numbJobs; i++)
        arrayRandom.Add(Constant.rnd.Next(0, randLimit));

    proc = new Proc(numbProc);
    schedule();
}

private void addInf()
{
    inf = "Случайный разброс на процессоры: ";
    inf += Environment.NewLine;
    for (int i = 0; i < numbProc; i++)
    {
        inf += " Процессор " + (i + 1).ToString() + ":";
        for (int j = 0; j < proc[i].Count; j++)
            inf += proc[i][j] + " ";
            inf += "|| sum = " + proc[i].Sum();
        inf += Environment.NewLine;
    }
    inf += "Δ = " + proc.delta + Environment.NewLine;
}


    //построить расписание
    void schedule()
{
        inf = string.Empty;
        int k = 0;
    for (int i = 0; i < proc.CountProc; i++)
        proc[i].Clear();
    for (int i = 0; i < NumberOfJobs; i++)
    {
        k = Range.rangeToIndexProc(arrayRandom[i], proc.CountProc, randLimit);
        proc[k].Add(arrayTasks[i]);
    }
    addInf();
}

    public string getRandomInString()
{
    string result = string.Empty;
    foreach (var iter in arrayRandom)
        result += iter.ToString() + ' ';
    return result;
}
public string print()
{
    addInf();        
    return inf;
}

private int getIndexMin(List<int> list)
{
    int result = 0;
    for (int i = 0; i < list.Count; i++)
        if (list[i] < list[result])
            result = i;
    return result;
}

    public delegate bool sort(int x, int y);

private void sortProc()
{
    arrayProc.Sort((a, b) => { return a - b; });
}
    }
使用系统;
使用系统集合;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Collections.Generic;
使用System.Windows.Forms;
命名空间LW3_OTPR_表单
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
textBoxZ.Text=“3”;
textBoxTasks.Text=“12”;
textBoxProcs.Text=“4”;
textBoxFrom.Text=“10”;
textBoxTo.Text=“20”;
}
私有无效按钮结果单击(对象发送者,事件参数e)
{
richTextBoxMatrix.Clear();
richTextBox1.Clear();
richTextBox2.Clear();
richTextBox3.Clear();
int z=转换为32(textBoxZ.Text);
对于(int i=0;i{return a.Sum();})-procs.Min((a)=>{return a.Sum();});
}
}
公共进程(int countProc)
{
inf=string.Empty;
infmolit=string.Empty;
infKritWay=string.Empty;
procs=新列表();
for(int i=0;iprocs[result].Sum())
结果=i;
}
返回结果;
}
public int getIndexOfMin()
{
int结果=0;
for(int i=0;i