C# 计算执行时间
我需要计算执行某个进程的时间。例如,我想从一个文件中读取所有行,但如果超过5秒,则显示messagebox,例如。我应该如何以及创建什么计时器来处理这5秒?使用类:System.Diagnostics命名空间的一部分C# 计算执行时间,c#,.net,winforms,C#,.net,Winforms,我需要计算执行某个进程的时间。例如,我想从一个文件中读取所有行,但如果超过5秒,则显示messagebox,例如。我应该如何以及创建什么计时器来处理这5秒?使用类:System.Diagnostics命名空间的一部分 Stopwatch watch = new Stopwatch(); watch.Start(); while (someCond) { if (watch.Elapsed.TotalSeconds >= 5) { MessageBox.Show("P
Stopwatch watch = new Stopwatch();
watch.Start();
while (someCond) {
if (watch.Elapsed.TotalSeconds >= 5) {
MessageBox.Show("Process taking too much time, aborting");
break;
}
//keep looping
}
watch.Stop();
string msg = "Process took " + watch.Elapsed.TotalSeconds + " seconds to complete"
使用类:System.Diagnostics命名空间的一部分
Stopwatch watch = new Stopwatch();
watch.Start();
while (someCond) {
if (watch.Elapsed.TotalSeconds >= 5) {
MessageBox.Show("Process taking too much time, aborting");
break;
}
//keep looping
}
watch.Stop();
string msg = "Process took " + watch.Elapsed.TotalSeconds + " seconds to complete"
考虑以下方法:
var cts = new CancellationTokenSource();
var task = new Task(YourLongRunningOperation, cts.Token);
task.Start();
var delayTask = Task.Delay(5000);
try
{
await Task.WhenAny(task, delayTask);
if(!task.IsCompleted)
{
cts.Cancel();
// You can display a message here.
await task;
}
}
catch(OperationCanceledException cex)
{
// TODO Handle cancelation.
}
catch (AggregateException aex)
{
// TODO Handle exceptions.
}
if(task.IsCanceled && delayTask.IsCompleted)
{
// TODO Display a long running error message.
}
考虑以下方法:
var cts = new CancellationTokenSource();
var task = new Task(YourLongRunningOperation, cts.Token);
task.Start();
var delayTask = Task.Delay(5000);
try
{
await Task.WhenAny(task, delayTask);
if(!task.IsCompleted)
{
cts.Cancel();
// You can display a message here.
await task;
}
}
catch(OperationCanceledException cex)
{
// TODO Handle cancelation.
}
catch (AggregateException aex)
{
// TODO Handle exceptions.
}
if(task.IsCanceled && delayTask.IsCompleted)
{
// TODO Display a long running error message.
}
将读取的文件启动到一个新线程,并在主线程中跟踪运行时间。将读取的文件启动到一个新线程,并在主线程中跟踪运行时间。@prokopchukvitaly您说过我需要计算执行某个进程的时间,这就是我的回答。我将很快回答你的另一个问题。谢谢,但我认为,它只适用于循环。我不能在所有情况下都做循环,我需要计算执行情况time@ProkopchukVitaliy你说我需要计算执行某个进程的时间,这就是我的回答。我将很快回答你的另一个问题。谢谢,但我认为,它只适用于循环。我不能在所有情况下都做循环,我需要计算执行时间,它似乎是在检查时间,只有在读了行之后,我需要像同时做一样,它似乎是在检查时间,只有在读了行之后,我需要像同时做一样
using System.Diagnostic
using System.Window.Forms
//your code
Stopwatch watch = new Stopwatch();
watch.Start();
// start reading lines of a file using file system object
watch.Stop();
if(watch.Elapsed.ElapsedMilliseconds>5000)
{
MessageBox.Show("The process takes more than 5 seconds !!!");
}
else
{
// your business logic
}
var cts = new CancellationTokenSource();
var task = new Task(YourLongRunningOperation, cts.Token);
task.Start();
var delayTask = Task.Delay(5000);
try
{
await Task.WhenAny(task, delayTask);
if(!task.IsCompleted)
{
cts.Cancel();
// You can display a message here.
await task;
}
}
catch(OperationCanceledException cex)
{
// TODO Handle cancelation.
}
catch (AggregateException aex)
{
// TODO Handle exceptions.
}
if(task.IsCanceled && delayTask.IsCompleted)
{
// TODO Display a long running error message.
}
class Program
{
static void Main()
{
bool result = Task.Factory.StartNew(SomePossibleFailingTask).Wait(1000);
if (result == false)
{
Console.WriteLine("Something has gone wrong!");
}
Console.ReadKey();
}
public static void SomePossibleFailingTask()
{
Thread.Sleep(15000);
}
}