计算文本框中值的总时间。我想计算值的时间
count the total time of values in textbox. i want to calculate the time of the value
我在文本框中有一些值。我想计算文本框中该值的时间。意味着该值在文本框中存在多长时间。
值是布尔类型。它可以是 1 或 0。我想计算每个值的时间跨度及其差异。
您发布的代码不多,但我会试一试。
我在您的代码中没有看到任何 bool
变量。
您可能应该有一个可以保存当前状态的地方。
由于您将值写入 TextBox
,您可以启动 MMbach 建议的计时器,该计时器紧随行后:
sqlserver_status.Text = "Active";
// start timer here
每当您进一步编写此状态更改时,您将停止计时器并检查经过的时间。
您也可以为此使用 StopWatch class。
它有一个名为 Elapsed
的 属性,其中:
Gets the total elapsed time measured by the current instance.
如果您需要 运行 它在后台,我建议您使用 Timer
。
下面是一个用System.Diagnostics.Stopwatch
实现的小Demo。
对这个问题还有更多的认识。实现的好坏总是取决于你的程序结构。
这是一个小型控制台应用程序,您可以在其中决定何时更改State
变量。它将监控您的决策过程。
public class TimeDemo
{
// Property to catch the timespan
public TimeSpan TimeOfState { get; set; }
// Full Property for the state
private bool state;
public bool State
{
get { return state; }
set
{
// whenever a new state value is set start measuring
state = value;
this.TimeOfState = StopTime();
}
}
// Use this to stop the time
public System.Diagnostics.Stopwatch StopWatch { get; set; }
public TimeDemo()
{
this.StopWatch = new System.Diagnostics.Stopwatch();
}
//Method to measure the elapsed time
public TimeSpan StopTime()
{
TimeSpan t = new TimeSpan(0, 0, 0);
if (this.StopWatch.IsRunning)
{
this.StopWatch.Stop();
t = this.StopWatch.Elapsed;
this.StopWatch.Restart();
return t;
}
else
{
this.StopWatch.Start();
return t;
}
}
public void Demo()
{
Console.WriteLine("Please press Enter whenever you want..");
Console.ReadKey();
this.State = !this.State;
Console.WriteLine("Elapsed Time: " + TimeOfState.ToString());
Console.WriteLine("Please press Enter whenever you want..");
Console.ReadKey();
this.State = !this.State;
Console.WriteLine("Elapsed Time: " + TimeOfState.ToString());
Console.WriteLine("Please press Enter whenever you want..");
Console.ReadKey();
this.State = !this.State;
Console.WriteLine("Elapsed Time: " + TimeOfState.ToString());
}
}
也许你可以根据自己的情况进行调整。
我在文本框中有一些值。我想计算文本框中该值的时间。意味着该值在文本框中存在多长时间。
值是布尔类型。它可以是 1 或 0。我想计算每个值的时间跨度及其差异。
您发布的代码不多,但我会试一试。
我在您的代码中没有看到任何 bool
变量。
您可能应该有一个可以保存当前状态的地方。
由于您将值写入 TextBox
,您可以启动 MMbach 建议的计时器,该计时器紧随行后:
sqlserver_status.Text = "Active";
// start timer here
每当您进一步编写此状态更改时,您将停止计时器并检查经过的时间。
您也可以为此使用 StopWatch class。
它有一个名为 Elapsed
的 属性,其中:
Gets the total elapsed time measured by the current instance.
如果您需要 运行 它在后台,我建议您使用 Timer
。
下面是一个用System.Diagnostics.Stopwatch
实现的小Demo。
对这个问题还有更多的认识。实现的好坏总是取决于你的程序结构。
这是一个小型控制台应用程序,您可以在其中决定何时更改State
变量。它将监控您的决策过程。
public class TimeDemo
{
// Property to catch the timespan
public TimeSpan TimeOfState { get; set; }
// Full Property for the state
private bool state;
public bool State
{
get { return state; }
set
{
// whenever a new state value is set start measuring
state = value;
this.TimeOfState = StopTime();
}
}
// Use this to stop the time
public System.Diagnostics.Stopwatch StopWatch { get; set; }
public TimeDemo()
{
this.StopWatch = new System.Diagnostics.Stopwatch();
}
//Method to measure the elapsed time
public TimeSpan StopTime()
{
TimeSpan t = new TimeSpan(0, 0, 0);
if (this.StopWatch.IsRunning)
{
this.StopWatch.Stop();
t = this.StopWatch.Elapsed;
this.StopWatch.Restart();
return t;
}
else
{
this.StopWatch.Start();
return t;
}
}
public void Demo()
{
Console.WriteLine("Please press Enter whenever you want..");
Console.ReadKey();
this.State = !this.State;
Console.WriteLine("Elapsed Time: " + TimeOfState.ToString());
Console.WriteLine("Please press Enter whenever you want..");
Console.ReadKey();
this.State = !this.State;
Console.WriteLine("Elapsed Time: " + TimeOfState.ToString());
Console.WriteLine("Please press Enter whenever you want..");
Console.ReadKey();
this.State = !this.State;
Console.WriteLine("Elapsed Time: " + TimeOfState.ToString());
}
}
也许你可以根据自己的情况进行调整。