Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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# 时间跨度是如何工作的?_C#_List_Sorting_Timespan - Fatal编程技术网

C# 时间跨度是如何工作的?

C# 时间跨度是如何工作的?,c#,list,sorting,timespan,C#,List,Sorting,Timespan,我有一个计时器,当标签中的文本出现时如何开始,当你点击按钮时停止。现在我想停止reaktiontime并将其保存在一个列表中,我想对其进行排序,以便在标签中给出最快和最短的reaktiontime。但在我的代码中,每次在messagebox中显示00:00:00时:( 公共部分类表单1:表单 { 随机r=新随机(); 随机Farbe=新随机(); bool Start_鼠标单击; IList ReaktionsZeitList=新列表(){}; IList myColors=new[]{Colo

我有一个计时器,当标签中的文本出现时如何开始,当你点击按钮时停止。现在我想停止reaktiontime并将其保存在一个列表中,我想对其进行排序,以便在标签中给出最快和最短的reaktiontime。但在我的代码中,每次在messagebox中显示00:00:00时:(

公共部分类表单1:表单
{
随机r=新随机();
随机Farbe=新随机();
bool Start_鼠标单击;
IList ReaktionsZeitList=新列表(){};
IList myColors=new[]{Color.Red,Color.Blue,Color.Green,Color.White,Color.Yellow};
字符串[]颜色={“Rot”、“Blau”、“Grün”、“Gelb”、“Weiß”};
int Timeleft=5;
int summe=0,z;
字符串REAKTIONSSEIT;
int Zeit;
int-Spielzuege=0;
日期时间1;
日期时间2;
时间跨度ts;
私有无效btnRot_单击(对象发送者,事件参数e)
{
斯皮尔祖格=斯皮尔祖格+1;
停下来;
如果(开始\鼠标单击==真)
{
int summe=0,z;
lblAnzeige.Text=“”;

而(summe我认为您的问题在于
DateTime
的分辨率小于您试图测量的间隔

请尝试以下操作,而不是使用
DateTime

创建名为
Stopwatch
的私有
Stopwatch
字段

Stopwatch stopwatch = new Stopwatch();
然后将记号处理程序更改为:

private void timMessung_Tick(object sender, EventArgs e)
{
    if (timMessung.Enabled == true)
    {
        stopwatch.Restart();
    }
    else
    {
        ts.stopwatch.Elapsed();
    }
}
但是,我也不确定重新启动秒表时的逻辑。(我将
stopwatch.restart()
)您真的要在此时继续重新启动秒表吗?

使用:

ts = Time1.subtract(Time2);
如何计算日期时间差异:


在第一次运行时,我试图修改您的代码,使其能够运行。但我发现其中存在太多(编码风格)问题,因此我只编写了一个全新的示例。它还有一些小问题(例如按钮的大小;颜色按钮可能出现两次)。但它希望向您展示
TimeSpan
和所有其他部分如何协同工作

Form1.cs文件:

public partial class Form1 : Form
{
    private Random _Random;
    private List<TimeSpan> _ReactionTimes;
    private Stopwatch _Stopwatch;

    public Form1()
    {
        InitializeComponent();
        _Stopwatch = new Stopwatch();
        _Random = new Random(42);
        _ReactionTimes = new List<TimeSpan>();
    }

    private Button CreateButton(Color color)
    {
        var button = new Button();
        button.Click += OnColorButtonClick;
        button.BackColor = color;
        button.Text = color.Name;

        return button;
    }

    private Button GetRandomButton()
    {
        var randomIndex = _Random.Next(0, flowLayoutPanel.Controls.Count);
        return (Button)flowLayoutPanel.Controls[randomIndex];
    }

    private Color GetRandomColor()
    {
        var randomKnownColor = (KnownColor)_Random.Next((int)KnownColor.AliceBlue, (int)KnownColor.ButtonFace);
        return Color.FromKnownColor(randomKnownColor);
    }

    private void InitializeColorButtons(int numberOfColors)
    {
        var buttons = Enumerable.Range(1, numberOfColors)
                                .Select(index => GetRandomColor())
                                .Select(color => CreateButton(color));

        foreach (var button in buttons)
        {
            flowLayoutPanel.Controls.Add(button);
        }
    }

    private void OnButtonStartClick(object sender, EventArgs e)
    {
        InitializeColorButtons((int)numericUpDownColors.Value);
        StartMeasurement();
    }

    private void OnColorButtonClick(object sender, EventArgs e)
    {
        var button = (Button)sender;

        if (button.Text != labelColorToClick.Text)
        {
            errorProviderWrongButton.SetIconPadding(button, -20);
            errorProviderWrongButton.SetError(button, "Sorry, wrong button.");
            return;
        }

        StopMeasurement();
        _ReactionTimes.Add(_Stopwatch.Elapsed);
        UpdateSummary();
    }

    private void StartMeasurement()
    {
        buttonStart.Enabled = false;
        numericUpDownColors.Enabled = false;
        labelColorToClick.Text = GetRandomButton().Text;

        _Stopwatch.Reset();
        _Stopwatch.Start();
    }

    private void StopMeasurement()
    {
        _Stopwatch.Stop();
        errorProviderWrongButton.Clear();
        flowLayoutPanel.Controls.Clear();

        numericUpDownColors.Enabled = true;
        buttonStart.Enabled = true;
        labelColorToClick.Text = String.Empty;
    }

    private void UpdateSummary()
    {
        labelSummary.Text = String.Format("Current: {0:0.000}    Minimum: {1:0.000}    Maximum: {2:0.000}", _ReactionTimes.Last().TotalSeconds, _ReactionTimes.Min().TotalSeconds, _ReactionTimes.Max().TotalSeconds);
    }
}
公共部分类表单1:表单
{
私人随机(u Random),;
私有列表\u反应时间;
私人秒表(秒表);;
公共表格1()
{
初始化组件();
_秒表=新秒表();
_随机=新随机(42);
_反应时间=新列表();
}
专用按钮CreateButton(彩色)
{
var按钮=新按钮();
按钮。单击+=颜色按钮单击;
button.BackColor=颜色;
button.Text=color.Name;
返回按钮;
}
私有按钮GetRandomButton()
{
var randomIndex=_Random.Next(0,flowLayoutPanel.Controls.Count);
返回(按钮)flowLayoutPanel.控件[随机索引];
}
私有颜色GetRandomColor()
{
var randomnowncolor=(KnownColor)_Random.Next((int)KnownColor.AliceBlue,(int)KnownColor.ButtonFace);
返回颜色.FromKnownColor(随机KnownColor);
}
私有void初始化颜色按钮(int numberOfColors)
{
变量按钮=可枚举范围(1,NumberOfColor)
.Select(index=>GetRandomColor())
.选择(颜色=>CreateButton(颜色));
foreach(按钮中的var按钮)
{
flowLayoutPanel.Controls.Add(按钮);
}
}
私有void OnButtonStartClick(对象发送者,事件参数e)
{
初始化颜色按钮((int)numericUpDownColors.Value);
开始测量();
}
private void OnColorButtonClick(对象发送方,事件参数e)
{
var按钮=(按钮)发送器;
if(button.Text!=labelColorToClick.Text)
{
ErrorProviderErrorButton.SetIconAdding(按钮,-20);
设置错误(按钮“对不起,错误的按钮”);
返回;
}
停止测量();
_添加(_Stopwatch.appeased);
更新摘要();
}
私有无效开始测量()
{
buttonStart.Enabled=false;
numericUpDownColors.Enabled=false;
labelColorToClick.Text=GetRandomButton().Text;
_秒表复位();
_秒表。开始();
}
私人空隙测量()
{
_秒表;
ErrorProviderErrorButton.Clear();
flowLayoutPanel.Controls.Clear();
numericUpDownColors.Enabled=true;
buttonStart.Enabled=true;
labelColorToClick.Text=String.Empty;
}
私有void更新摘要()
{
labelSummary.Text=String.Format(“当前:{0:0.000}最小:{1:0.000}最大:{2:0.000}”,_ReactionTimes.Last().TotalSeconds,_ReactionTimes.Min().TotalSeconds,_ReactionTimes.Max().TotalSeconds);
}
}
Form1.Designer.cs文件:

partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.flowLayoutPanel = new System.Windows.Forms.FlowLayoutPanel();
        this.labelColorToClick = new System.Windows.Forms.Label();
        this.buttonStart = new System.Windows.Forms.Button();
        this.labelSummaryHeader = new System.Windows.Forms.Label();
        this.labelSummary = new System.Windows.Forms.Label();
        this.errorProviderWrongButton = new System.Windows.Forms.ErrorProvider(this.components);
        this.numericUpDownColors = new System.Windows.Forms.NumericUpDown();
        ((System.ComponentModel.ISupportInitialize)(this.errorProviderWrongButton)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.numericUpDownColors)).BeginInit();
        this.SuspendLayout();
        // 
        // flowLayoutPanel
        // 
        this.flowLayoutPanel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
        this.flowLayoutPanel.Location = new System.Drawing.Point(12, 41);
        this.flowLayoutPanel.Name = "flowLayoutPanel";
        this.flowLayoutPanel.Size = new System.Drawing.Size(560, 386);
        this.flowLayoutPanel.TabIndex = 0;
        // 
        // labelColorToClick
        // 
        this.labelColorToClick.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
        this.labelColorToClick.Location = new System.Drawing.Point(174, 12);
        this.labelColorToClick.Name = "labelColorToClick";
        this.labelColorToClick.Size = new System.Drawing.Size(398, 23);
        this.labelColorToClick.TabIndex = 1;
        this.labelColorToClick.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
        // 
        // buttonStart
        // 
        this.buttonStart.Location = new System.Drawing.Point(93, 12);
        this.buttonStart.Name = "buttonStart";
        this.buttonStart.Size = new System.Drawing.Size(75, 23);
        this.buttonStart.TabIndex = 2;
        this.buttonStart.Text = "Start";
        this.buttonStart.UseVisualStyleBackColor = true;
        this.buttonStart.Click += new System.EventHandler(this.OnButtonStartClick);
        // 
        // labelSummaryHeader
        // 
        this.labelSummaryHeader.Location = new System.Drawing.Point(12, 430);
        this.labelSummaryHeader.Name = "labelSummaryHeader";
        this.labelSummaryHeader.Size = new System.Drawing.Size(75, 23);
        this.labelSummaryHeader.TabIndex = 3;
        this.labelSummaryHeader.Text = "Summary:";
        this.labelSummaryHeader.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
        // 
        // labelSummary
        // 
        this.labelSummary.Location = new System.Drawing.Point(93, 430);
        this.labelSummary.Name = "labelSummary";
        this.labelSummary.Size = new System.Drawing.Size(479, 23);
        this.labelSummary.TabIndex = 4;
        this.labelSummary.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
        // 
        // errorProviderWrongButton
        // 
        this.errorProviderWrongButton.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.NeverBlink;
        this.errorProviderWrongButton.ContainerControl = this;
        // 
        // numericUpDownColors
        // 
        this.numericUpDownColors.Location = new System.Drawing.Point(12, 14);
        this.numericUpDownColors.Minimum = new decimal(new int[] {
        2,
        0,
        0,
        0});
        this.numericUpDownColors.Name = "numericUpDownColors";
        this.numericUpDownColors.Size = new System.Drawing.Size(75, 20);
        this.numericUpDownColors.TabIndex = 5;
        this.numericUpDownColors.Value = new decimal(new int[] {
        10,
        0,
        0,
        0});
        // 
        // Form1
        // 
        this.ClientSize = new System.Drawing.Size(584, 462);
        this.Controls.Add(this.numericUpDownColors);
        this.Controls.Add(this.labelSummary);
        this.Controls.Add(this.labelSummaryHeader);
        this.Controls.Add(this.buttonStart);
        this.Controls.Add(this.labelColorToClick);
        this.Controls.Add(this.flowLayoutPanel);
        this.Name = "Form1";
        ((System.ComponentModel.ISupportInitialize)(this.errorProviderWrongButton)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.numericUpDownColors)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel;
    private System.Windows.Forms.Label labelColorToClick;
    private System.Windows.Forms.Button buttonStart;
    private System.Windows.Forms.Label labelSummaryHeader;
    private System.Windows.Forms.Label labelSummary;
    private System.Windows.Forms.ErrorProvider errorProviderWrongButton;
    private System.Windows.Forms.NumericUpDown numericUpDownColors;


}
部分类表单1
{
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.components=new System.ComponentModel.Container();
this.flowLayoutPanel=new System.Windows.Forms.flowLayoutPanel();
this.labelColorToClick=new System.Windows.Forms.Label();
this.buttonStart=new System.Windows.Forms.Button();
this.labelSummaryHeader=new System.Windows.Forms.Label();
this.labelSummary=new System.Windows.Forms.Label();
此为.errorProviderWron
partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.flowLayoutPanel = new System.Windows.Forms.FlowLayoutPanel();
        this.labelColorToClick = new System.Windows.Forms.Label();
        this.buttonStart = new System.Windows.Forms.Button();
        this.labelSummaryHeader = new System.Windows.Forms.Label();
        this.labelSummary = new System.Windows.Forms.Label();
        this.errorProviderWrongButton = new System.Windows.Forms.ErrorProvider(this.components);
        this.numericUpDownColors = new System.Windows.Forms.NumericUpDown();
        ((System.ComponentModel.ISupportInitialize)(this.errorProviderWrongButton)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.numericUpDownColors)).BeginInit();
        this.SuspendLayout();
        // 
        // flowLayoutPanel
        // 
        this.flowLayoutPanel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
        this.flowLayoutPanel.Location = new System.Drawing.Point(12, 41);
        this.flowLayoutPanel.Name = "flowLayoutPanel";
        this.flowLayoutPanel.Size = new System.Drawing.Size(560, 386);
        this.flowLayoutPanel.TabIndex = 0;
        // 
        // labelColorToClick
        // 
        this.labelColorToClick.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
        this.labelColorToClick.Location = new System.Drawing.Point(174, 12);
        this.labelColorToClick.Name = "labelColorToClick";
        this.labelColorToClick.Size = new System.Drawing.Size(398, 23);
        this.labelColorToClick.TabIndex = 1;
        this.labelColorToClick.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
        // 
        // buttonStart
        // 
        this.buttonStart.Location = new System.Drawing.Point(93, 12);
        this.buttonStart.Name = "buttonStart";
        this.buttonStart.Size = new System.Drawing.Size(75, 23);
        this.buttonStart.TabIndex = 2;
        this.buttonStart.Text = "Start";
        this.buttonStart.UseVisualStyleBackColor = true;
        this.buttonStart.Click += new System.EventHandler(this.OnButtonStartClick);
        // 
        // labelSummaryHeader
        // 
        this.labelSummaryHeader.Location = new System.Drawing.Point(12, 430);
        this.labelSummaryHeader.Name = "labelSummaryHeader";
        this.labelSummaryHeader.Size = new System.Drawing.Size(75, 23);
        this.labelSummaryHeader.TabIndex = 3;
        this.labelSummaryHeader.Text = "Summary:";
        this.labelSummaryHeader.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
        // 
        // labelSummary
        // 
        this.labelSummary.Location = new System.Drawing.Point(93, 430);
        this.labelSummary.Name = "labelSummary";
        this.labelSummary.Size = new System.Drawing.Size(479, 23);
        this.labelSummary.TabIndex = 4;
        this.labelSummary.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
        // 
        // errorProviderWrongButton
        // 
        this.errorProviderWrongButton.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.NeverBlink;
        this.errorProviderWrongButton.ContainerControl = this;
        // 
        // numericUpDownColors
        // 
        this.numericUpDownColors.Location = new System.Drawing.Point(12, 14);
        this.numericUpDownColors.Minimum = new decimal(new int[] {
        2,
        0,
        0,
        0});
        this.numericUpDownColors.Name = "numericUpDownColors";
        this.numericUpDownColors.Size = new System.Drawing.Size(75, 20);
        this.numericUpDownColors.TabIndex = 5;
        this.numericUpDownColors.Value = new decimal(new int[] {
        10,
        0,
        0,
        0});
        // 
        // Form1
        // 
        this.ClientSize = new System.Drawing.Size(584, 462);
        this.Controls.Add(this.numericUpDownColors);
        this.Controls.Add(this.labelSummary);
        this.Controls.Add(this.labelSummaryHeader);
        this.Controls.Add(this.buttonStart);
        this.Controls.Add(this.labelColorToClick);
        this.Controls.Add(this.flowLayoutPanel);
        this.Name = "Form1";
        ((System.ComponentModel.ISupportInitialize)(this.errorProviderWrongButton)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.numericUpDownColors)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel;
    private System.Windows.Forms.Label labelColorToClick;
    private System.Windows.Forms.Button buttonStart;
    private System.Windows.Forms.Label labelSummaryHeader;
    private System.Windows.Forms.Label labelSummary;
    private System.Windows.Forms.ErrorProvider errorProviderWrongButton;
    private System.Windows.Forms.NumericUpDown numericUpDownColors;


}