从字典中获取值

时间:2014-06-05 07:20:08

标签: c# dictionary

我正在与Round Robin(RR)合作。我正在构建随机的Round-Robin值。我随机获得了RR的时间。在第一次运行中,它显示了我在列表中的当前值。但是当它第二次运行时,它只是说 索引没有出现在字典键 中。我正在尝试解决这个问题,我已经添加了时间和量子时间,算法减少了字典中的值并返回字典,值为> 0.

Dictionary<int, int> waytosave = new Dictionary<int, int>();

    List<int> randomListxCoor = new List<int>();
    List<int> randomListyCoor = new List<int>();



    int xCoor;
    int yCoor;

    Random coor = new Random();
    Random setTimeRandom = new Random();
    int randomTimeSetting;

    int NodeIndex = 0;
    Graphics graphDraw;
    Graphics drawLine;
    int TimeToProcess = 0;
    string NodeName = "";
    int QuantumTime = 1;
    int TotalRouterTime = 0;
    int NextNodeToProcess = 0;
    public Form1()
    {
        InitializeComponent();
    this.graphDraw  = this.panel1.CreateGraphics();
        this.drawLine = this.panel1.CreateGraphics();

    }

    private void btnRun_Click(object sender, EventArgs e)
    {
        int nodesNumber = (int)this.numNodes.Value;
        SolidBrush getNodesDrawn = new SolidBrush(Color.LightYellow);
        for (int x = 1; x <= nodesNumber; x++)
        {
            xCoor = coor.Next(0, 700);
            yCoor = coor.Next(0, 730);
            if (!randomListxCoor.Contains(xCoor))
            {
                randomListxCoor.Add(xCoor);

            }
            if (!randomListyCoor.Contains(xCoor))
            {
                randomListyCoor.Add(yCoor);

            }

            randomTimeSetting = setTimeRandom.Next(1, 10);

            //Draw the line from main Node to the other Nodes

            Pen linePen = new Pen(Color.DarkMagenta, 2);

            drawLine.DrawLine(linePen, 350, 360, xCoor, yCoor);
            drawLine = this.panel1.CreateGraphics();
            graphDraw.FillEllipse(getNodesDrawn, xCoor - 5, yCoor - 5, 15, 12);



            //Add the values t the Dictionaries and the List.

            waytosave.Add(x, randomTimeSetting);
        }
    }
    private void btnRunTIme_Click(object sender, EventArgs e)
    {
        this.QuantumTime = (int)this.numQuanrum.Value;
        this.ComputeTotalNodeTime();
        this.timerRoundRobin.Enabled = true;
    }

    private void timerRoundRobin_Tick(object sender, EventArgs e)
    {
        this.NodeIndex = this.GetNextNodeToProcess();
        if (this.NodeIndex >= 0)
        {
            this.waytosave[this.NodeIndex] -= this.QuantumTime;

此时此刻它已经崩溃了。我试图修复迭代次数,但如果我改变迭代次数,它将不会通过字典中的所有值。

                if (this.waytosave[this.NodeIndex] < 0)
                                          {
                this.waytosave[this.NodeIndex] = 0;
            }
            this.NodeName = this.NodeIndex.ToString();
            this.TimeToProcess = this.waytosave[this.NodeIndex];

            this.txtOutput.Text += "\r\r\n" + " Time Remaining:   "
                                             + this.TimeToProcess
                                             + "Router name:     "
                                             + this.NodeName;
        }
        else
        {
            this.timerRoundRobin.Enabled = false;

            this.txtOutput.Text += "\r\r\n" + "  Ends x " + TotalRouterTime.ToString();

            return;

        }
    }

    private int GetNextNodeToProcess()
    {
        int NextNodeIndex = -1;
        if (NextNodeToProcess >= this.waytosave.Count)
        { NextNodeToProcess = 0; }
        for (int i = NextNodeToProcess; i < this.waytosave.Count; i++)
        {
            if (this.waytosave[i] > 0)
            {
                NextNodeIndex = i;
                break;
            }

        }
        NextNodeToProcess++;
        return NextNodeIndex;
    }

    private void ComputeTotalNodeTime()
    {
        this.TotalRouterTime = 0;
        foreach (KeyValuePair<int, int> item in this.waytosave)
        {
            this.TotalRouterTime += item.Value;

            this.txtOutput.Text += "\r\r\n" + " Time Remaining:   "
                                         + item.Value
                                         + "Router name:     "
                                         + item.Key;
        }

    }

我试图从几天来修复这个价值,但我没有成功。无法从字典中读取第一个值的问题。我怎么解决这个问题。你能不能给我一些。

0 个答案:

没有答案