我的StopWatch错误(GetTimestamp)

时间:2013-04-21 13:39:09

标签: c# stopwatch

我有这个代码,它将是一个秒表。

为什么无法使用实例引用访问?

错误:无法使用实例引用访问成员'System.Diagnostics.Stopwatch.GetTimestamp()';用类型名称来限定它。

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

namespace Pocitadlo
{
public partial class Form1 : Form
{
    public int minutys = 0, test = 0;
    Thread sekund;
    TimeSpan ts;
    Stopwatch stopky = new Stopwatch();

    public Form1()
    {
        InitializeComponent();
    }

    public void START_Click(object sender, EventArgs e)
    {
            sekund = new Thread(Sekund_pocet);
            this.START.Visible = false;
            this.STOP.Visible = true;
            sekund.Start();
    }

    public void Sekund_pocet()
    {
        stopky.Start();
        stopky.Stop();
        ts = stopky.GetTimestamp();
        string time = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
        cas.BeginInvoke(new Action(() => cas.Text = Convert.ToString(time)));
    }

    private void STOP_Click(object sender, EventArgs e)
    {
        this.STOP.Visible = false;
        this.START.Visible = true;
        test = 1;
    }
}
}

1 个答案:

答案 0 :(得分:5)

GetTimestamp()是静态方法,因此是错误。你应该改为ts = Stopwatch.GetTimestamp()