e79aa3c0ed
Former-commit-id: a2155e9bd80020e49e72e86c44da02a8ac0e57a4
55 lines
1.7 KiB
C#
55 lines
1.7 KiB
C#
//-----------------------------------------------------------------------------
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
//-----------------------------------------------------------------------------
|
|
|
|
namespace System.Runtime
|
|
{
|
|
using System;
|
|
using System.Threading;
|
|
|
|
// An AsyncResult that schedules work for later on the IOThreadScheduler
|
|
abstract class ScheduleActionItemAsyncResult : AsyncResult
|
|
{
|
|
static Action<object> doWork = new Action<object>(DoWork);
|
|
|
|
// Implement your own constructor taking in necessary parameters
|
|
// Constructor needs to call "Schedule()" to schedule work
|
|
// Cache all parameters
|
|
// Implement OnDoWork to do work!
|
|
|
|
protected ScheduleActionItemAsyncResult(AsyncCallback callback, object state) : base(callback, state) { }
|
|
|
|
protected void Schedule()
|
|
{
|
|
ActionItem.Schedule(doWork, this);
|
|
}
|
|
|
|
static void DoWork(object state)
|
|
{
|
|
ScheduleActionItemAsyncResult thisPtr = (ScheduleActionItemAsyncResult)state;
|
|
Exception completionException = null;
|
|
try
|
|
{
|
|
thisPtr.OnDoWork();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (Fx.IsFatal(ex))
|
|
{
|
|
throw;
|
|
}
|
|
completionException = ex;
|
|
}
|
|
|
|
thisPtr.Complete(false, completionException);
|
|
}
|
|
|
|
protected abstract void OnDoWork();
|
|
|
|
public static void End(IAsyncResult result)
|
|
{
|
|
AsyncResult.End<ScheduleActionItemAsyncResult>(result);
|
|
}
|
|
}
|
|
}
|