下面的方法假设在情况0传入的(持续时间为毫秒)下运行,但是我看到的是该方法可能需要2秒钟才能运行400毫秒。 Task.run是否可能需要很长时间才能启动?如果是这样,有没有更好的方法?
private static async void PulseWait(int duration, int axis){
await Task.Run(() =>
{
try
{
var logaction = true;
switch (axis)
{
case 0:
var sw1 = Stopwatch.StartNew();
if (duration > 0) duration += 20; // allowance for the call to the mount
while (sw1.Elapsed.TotalMilliseconds <= duration) { } // wait out the duration
_isPulseGuidingRa = false;
logaction = false;
break;
case 1:
var axis2Stopped = false;
var loopcount = 0;
switch (SkySettings.Mount)
{
case MountType.Simulator:
while (!axis2Stopped && loopcount < 30)
{
loopcount++;
var statusy = new CmdAxisStatus(MountQueue.NewId, Axis.Axis2);
var axis2Status = (AxisStatus)MountQueue.GetCommandResult(statusy).Result;
axis2Stopped = axis2Status.Stopped;
if (!axis2Stopped) Thread.Sleep(10);
}
break;
case MountType.SkyWatcher:
while (!axis2Stopped && loopcount < 30)
{
loopcount++;
var statusy = new SkyIsAxisFullStop(SkyQueue.NewId, AxisId.Axis2);
axis2Stopped = Convert.ToBoolean(SkyQueue.GetCommandResult(statusy).Result);
if (!axis2Stopped) Thread.Sleep(10);
}
break;
default:
throw new ArgumentOutOfRangeException();
}
_isPulseGuidingDec = false;
logaction = false;
break;
}
var monitorItem = new MonitorEntry
{ Datetime = HiResDateTime.UtcNow, Device = MonitorDevice.Telescope, Category = MonitorCategory.Mount, Type = MonitorType.Data, Method = MethodBase.GetCurrentMethod().Name, Thread = Thread.CurrentThread.ManagedThreadId, Message = $"PulseGuide={logaction}" };
MonitorLog.LogToMonitor(monitorItem);
}
catch (Exception)
{
_isPulseGuidingDec = false;
_isPulseGuidingRa = false;
}
});}
日志显示了花费的时间... 33652,2019:07:12:01:15:35.590,13,AxisPulse,Axis1,0.00208903710815278,400,0,True <<-在调用PulseWait之前持续400ms 33653,2019:07:12:01:15:35.591,13,SendRequest,:I1250100 33654,2019:07:12:01:15:35.610,13,ReceiveResponse,:I1250100,= 33655,2019:07:12:01:15:36.026,13,SendRequest,:I1B70100 33656,2019:07:12:01:15:36.067,13,ReceiveResponse,:I1B70100,= 33657,2019:07:12:01:15:36.067,13,SendRequest,:j1 33658,2019:07:12:01:15:36.120,13,ReceiveResponse,:j1,= DDCDBD 33659,2019:07:12:01:15:36.120,13,SendRequest,:j2 33660,2019:07:12:01:15:36.165,13,ReceiveResponse,:j2,= 67CF8A 33661,2019:07:12:01:15:36.467,13,SendRequest,:j1 33662,2019:07:12:01:15:36.484,13,ReceiveResponse,:j1,= 10CEBD 33663,2019:07:12:01:15:36.484,13,SendRequest,:j2 33664,2019:07:12:01:15:36.501,13,ReceiveResponse,:j2,= 67CF8A 33665,2019:07:12:01:15:36.808,13,SendRequest,:j1 33666,2019:07:12:01:15:36.842,13,ReceiveResponse,:j1,= 3CCEBD 33667,2019:07:12:01:15:36.842,13,SendRequest,:j2 33668,2019:07:12:01:15:36.868,13,ReceiveResponse,:j2,= 67CF8A 33669,2019:07:12:01:15:37.170,13,SendRequest,:j1 33670,2019:07:12:01:15:37.188,13,ReceiveResponse,:j1,= 6BCEBD 33671,2019:07:12:01:15:37.188,13,SendRequest,:j2 33672,2019:07:12:01:15:37.204,13,ReceiveResponse,:j2,= 67CF8A 33673,2019:07:12:01:15:37.221,5,b__0,PulseGuide = False <<-PulseWait在启动后1.631毫秒完成
答案 0 :(得分:3)
async
和await
的目的是使事情变得容易。但是,就像使事情变得容易的一切一样,它也具有对发生的事情进行完全控制的代价。通常,这实际上是异步编程的成本。异步编程的重点是释放当前线程,以便当前线程可以执行其他操作。但是,如果在当前线程上完成了其他操作,则必须继续执行操作,直到完成操作。 (即,await
之后的 可能不会在任务完成后立即发生)
因此,尽管异步编程有助于提高总体的性能(例如提高Web应用程序的整体吞吐量性能),但实际上会损害任何 one 特定任务的性能。如果每一毫秒都很重要,那么您也许可以自己执行低级任务,例如创建一个Thread(如果确实需要在单独的线程上运行)。
这是一个简单的示例,演示了这一点:
var s = new Stopwatch();
// Test the time it takes to run an empty method on a
// different thread with Task.Run and await it.
s.Start();
await Task.Run(() => { });
s.Stop();
Console.WriteLine($"Time of Task.Run: {s.ElapsedMilliseconds}ms");
// Test the time it takes to create a new thread directly
// and wait for it.
s.Restart();
var t = new Thread(() => { });
t.Start();
t.Join();
s.Stop();
Console.WriteLine($"Time of new Thread: {s.ElapsedMilliseconds}ms");
输出将有所不同,但是看起来像这样:
Time of Task.Run: 8ms
Time of new Thread: 0ms
在发生许多其他事情的应用程序中,如果其他操作在await
期间使用线程,则8ms可能会更长。
这并不是说您应该使用Thread
。 t.Join()
不是异步操作。它将阻塞线程。因此,如果PulseWait
在UI线程上运行(如果这是一个UI应用程序),它将锁定UI线程,这是糟糕的用户体验。在这种情况下,您可能无法避免使用异步代码的成本。
如果这不是具有UI的应用程序,那么我根本不明白为什么需要在不同的线程上完成所有这些操作。也许你可以...。不可以。