gpt4 book ai didi

c# - IAsyncResult 的属性isCompleted 是如何更新的?

转载 作者:行者123 更新时间:2023-11-30 17:13:37 24 4
gpt4 key购买 nike

当我异步调用一个方法时(使用模式 BeginXxx/EndXxx),我在调用 BeginXxx 后得到一个 IAsyncResult 结果。如果方法 BeginXxxx 或 EndXxx 均未引用结果变量,属性“isCompleted”(在返回的结果变量中)如何更新?

例如:

// Create the delegate.
AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);

// Initiate the asychronous call.
IAsyncResult result = caller.BeginInvoke(3000, out threadId, null, null);

// Poll while simulating work.
while(result.IsCompleted == false) {
Thread.Sleep(250);
Console.Write(".");
}

最佳答案

BeginInvoke 向您返回 IAsyncResult,因此它将具有对它的引用。这将在内部创建并发回给您。

例如FileStream中的BeginRead创建FileStreamAsyncResult然后返回:

private unsafe FileStreamAsyncResult BeginReadCore(byte[] bytes, int offset, int numBytes, AsyncCallback userCallback, object stateObject, int numBufferedBytesRead)
{
NativeOverlapped* overlappedPtr;
FileStreamAsyncResult ar = new FileStreamAsyncResult {
_handle = this._handle,
_userCallback = userCallback,
_userStateObject = stateObject,
_isWrite = false,
_numBufferedBytes = numBufferedBytesRead
};
ManualResetEvent event2 = new ManualResetEvent(false);
ar._waitHandle = event2;

.....

if (hr == 0x6d)
{
overlappedPtr->InternalLow = IntPtr.Zero;
ar.CallUserCallback();
return ar;
}

关于c# - IAsyncResult 的属性isCompleted 是如何更新的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9603893/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com