2017-04-05 8 views
-1

私はFastline Chartを選択しようとしています。私はちょうど同じチャートに平均をプラスしたデータをプロットしたいと思います。簡単にする必要がありますが、エラーが発生する "mscorlib.dllで 'System.Reflection.TargetInvocationException'型の未処理の例外が発生しました。私は[AverageTime「]と呼ばれる第二シリーズコメントアウトする場合Fastlineチャートに複数の線をプロットできません

機能は、以下の通りである私は、コンパイルをし、1行で動作します。

機能に注意してください、私が使用非同期(.NET 4.5)である」のawait私が待っているスレッドを持っていると推測すると、これと何か関係があるかどうかは分かりません。

どのようにすればいいのですか?問題?

private async void btn_timing_send_Click(object sender, EventArgs e) 
    { 
     int recursive_counter = 0, num = 0, num2 = 0; 
     double total_time = 0, average_time = 0, min_time = 0, max_time = 0, ave_plot = 0; 

     num2 = Int32.Parse(txtbx_iterations.Text); // number of required iterations 

     /* 
     * Just a word on timings - Windows is bad at doing timings in lower Microseconds and below so we need to do a few things to try and get 
     * accurate timings. 
     * 
     * As the general rule, when testing comms with completion time in the range of several microseconds it's recommended to implement 
     * the loop running 10,000...100,000 iterations in order to increase the accuracy of the measurement 
     */ 

     string str1 = ""; 

     Stream stm = tcpclnt.GetStream(); 
     byte[] payload; 
     string ID = txtbx_timing_send_data.Text; 
     str1 = "No Connection"; 
     ID = ID.ToCharArray().Aggregate("", (result, c) => result += ((!string.IsNullOrEmpty(result) && (result.Length + 1) % 3 == 0) ? " " : "") + c.ToString());//add space every two chars to make hex 
     payload = ID.Split().Select(s => Convert.ToByte(s, 16)).ToArray(); //split the bytes into the array 

     if (ckbx_plot.Checked) 
     { 
      chrt_timings.Series["ResponseTimes"].ChartType = SeriesChartType.FastLine; //set type 
      chrt_timings.Series["ResponseTimes"].Color = Color.Blue; //set colour 

      chrt_timings.Series["AverageTime"].ChartType = SeriesChartType.FastLine; //set type 
     // chrt_timings.Series["AverageTime"].Color = Color.Red; //set colour 
     // chrt_timings.Series["AverageTime"].BorderDashStyle = ChartDashStyle.Dash; 

      chrt_timings.Legends.Clear(); // We do not need a legend 
      chrt_timings.ChartAreas[0].AxisX.IsMarginVisible = false; 
     } 

     do 
     { 
      try 
      { 
       Stopwatch timer = Stopwatch.StartNew(); 
       long frequency = Stopwatch.Frequency; 
       long nanosecPerTick = (1000L * 1000L * 1000L)/frequency; 

       long startTick = timer.ElapsedTicks; //start of timed section 
       stm.Write(payload, 0, payload.Length); 


       byte[] input = new byte[tcpclnt.ReceiveBufferSize]; 
       int k = stm.Read(input, 0, tcpclnt.ReceiveBufferSize); 
       Array.Resize(ref input, k); //crop the array to the amount of items it read in 
       str1 = string.Join(" ", input.Select(b => string.Format("{0:X2} ", b))); //format as hex bytes 

       long stopTick = timer.ElapsedTicks; //end of timed section 
       var timestamp = Convert.ToDouble((stopTick - startTick) * nanosecPerTick)/1000000; 

       timer.Reset(); 

       rchtxbox_timings.SelectionColor = Color.LimeGreen; 
       rchtxbox_timings.AppendText(str1 + "\r"); 
       rchtxbox_timings.SelectionColor = Color.Yellow; 
       rchtxbox_timings.AppendText(timestamp + "ms\r\r"); 
       rchtxbox_timings.ScrollToCaret(); 

       if (num == 0) min_time = timestamp; 

       if (num2 > 1) 
       { 
        total_time = total_time + timestamp; 

        if (max_time < timestamp) max_time = timestamp; 
        if (min_time > timestamp) min_time = timestamp; 
       } 



        if (chkBx_LogData.Checked) 
       { 
        using (StreamWriter sw = new StreamWriter(timing_filename, true)) 
        { 
         str1 = str1.Replace(" ", ""); //take out the spaces 
         sw.WriteLine(str1 + "," + timestamp.ToString() + "\r"); 
        } 
       } 

       //Plot graph if required 
       if (ckbx_plot.Checked) 
       { 
        ave_plot = timestamp/num; 

        if (ckbx_restrict_graph.Checked) 
        { 
         if (chrt_timings.Series["ResponseTimes"].Points.Count() >= Convert.ToInt16(txtbx_axispoints.Text)) chrt_timings.Series["ResponseTimes"].Points.RemoveAt(0); 
         chrt_timings.Series["ResponseTimes"].Points.Add(timestamp); 
         chrt_timings.Series["ResponseTimes"].ToolTip = timestamp.ToString(); 

         // if (chrt_timings.Series["AverageTime"].Points.Count() >= Convert.ToInt16(txtbx_axispoints.Text)) chrt_timings.Series["Average Time"].Points.RemoveAt(0); 
         // chrt_timings.Series["AverageTime"].Points.Add(ave_plot); 

         chrt_timings.ResetAutoValues(); 
        } 
        else 
        { 
         recursive_counter++; 
         chrt_timings.Series["ResponseTimes"].Points.AddXY(recursive_counter, timestamp); 
         chrt_timings.Series["ResponseTimes"].ToolTip = timestamp.ToString(); 

         // chrt_timings.Series["AverageTime"].Points.AddXY(ave_plot, timestamp); 
        } 
       } 

       num = num + 1; 
       timestamp = 0; 
       await Task.Delay(Int32.Parse(txtbx_timing_delay.Text)); 

      } 
      catch (Exception d) 
      { 
       rchTxtBx_output.AppendText("red..... " + d.StackTrace); 
      } 
     } while (num2 > num); 

     if (num2 > 1) 
     { 
      //write out min, max and ave times 
      average_time = total_time/num; 
      rchtxbox_timings.SelectionColor = Color.LightBlue; 
      rchtxbox_timings.AppendText("\rMinimum Time = " + min_time + "\r"); 
      rchtxbox_timings.SelectionColor = Color.LightBlue; 
      rchtxbox_timings.AppendText("Maximum Time = " + max_time + "\r"); 
      rchtxbox_timings.SelectionColor = Color.LightBlue; 
      rchtxbox_timings.AppendText("Average Time = " + average_time + "\r\r"); 
      rchtxbox_timings.ScrollToCaret(); 
     } 
    } 
+0

を追加します。 – qxg

+0

さて、いくつかの行がコメントアウトされています。問題を作り出すのはどちらですか?また、ポイント数とその他の数値をテキストボックスから一度だけ引き出し、それらを変数に保管してください!パフォーマンスの向上、デバッグの改善 – TaW

答えて

0

多くのヘッドスクラッチの後、私は 問題。 1つの小さな点がありませんでした。これは、グラフ作成キャンバスのメンバーとして宣言された2番目のシリーズ、Series ["AverageTime"]を持たなかったことです。

これを修正するには、[プロパティ]に行き、コレクションをクリックしてメンバーを追加してください。AverageTimeそれが完了したら、チャートキャンバスにポイントを描画することができました。可能な場合は、最小限のREPROを作成してください

は、だから今は

if (ckbx_plot.Checked) 
    { 
     chrt_timings.Series["ResponseTimes"].ChartType = SeriesChartType.FastLine; //set type 
     chrt_timings.Series["ResponseTimes"].Color = Color.Blue; //set colour 

     chrt_timings.Series["AverageTime"].ChartType = SeriesChartType.FastLine; //set type 
     chrt_timings.Series["AverageTime"].Color = Color.Red; //set colour 
     chrt_timings.Series["AverageTime"].BorderDashStyle = ChartDashStyle.Dash; 

     chrt_timings.Legends.Clear(); // We do not need a legend 
     chrt_timings.ChartAreas[0].AxisX.IsMarginVisible = false; 
    } 

以下の行を初期化し、今私は、データ

if (ckbx_restrict_graph.Checked) 
       { //shows just set number of points, add a new point remove an old point 
        if (chrt_timings.Series["ResponseTimes"].Points.Count() >= Convert.ToInt16(txtbx_axispoints.Text)) chrt_timings.Series["ResponseTimes"].Points.RemoveAt(0); 
        chrt_timings.Series["ResponseTimes"].Points.Add(timestamp); 
        chrt_timings.Series["ResponseTimes"].ToolTip = timestamp.ToString(); 

         if (chrt_timings.Series["AverageTime"].Points.Count() >= Convert.ToInt16(txtbx_axispoints.Text)) chrt_timings.Series["Average Time"].Points.RemoveAt(0); 
         chrt_timings.Series["AverageTime"].Points.Add(ave_plot); 

        chrt_timings.ResetAutoValues(); 
       } 
       else 
       { //squash all points onto the same canvas 
        recursive_counter++; 
        chrt_timings.Series["ResponseTimes"].Points.AddXY(recursive_counter, timestamp); 
        chrt_timings.Series["ResponseTimes"].ToolTip = timestamp.ToString(); 

        chrt_timings.Series["AverageTime"].Points.AddXY(ave_plot, timestamp); 
       } 
関連する問題