我真的可以在这方面提供一些帮助 - 我完全陷入困境:-(
我已经在我开发的搜索组件中实现了异步模式(希望是正确的)。这是从异步 Asp 调用的。网页,使用 RegisterAsyncTask。
当我的组件完成其工作时,它会调用提供的回调,传递一个 AsyncResult 并将 AsyncState 设置为一个值。调用回调时,调试器确认该值不为空。
当 AsyncResult作为参数到达页面主线程上的回调函数时 - AsyncState 属性为 null。
这是搜索组件的相关部分:
public virtual IAsyncResult BeginSearchAsync(
Object sender,
EventArgs e,
AsyncCallback cb,
object state)
{
_callback = cb;
//... Some code
// Start the asynchronous operation.
WorkerEventHandler workerDelegate = new WorkerEventHandler(SearchWorker);
return workerDelegate.BeginInvoke(
paramz.terms,
paramz.sourceName,
asyncOp,
cb,
null);
}
protected void OnSearchCompleted(SearchCompleteEventArgs e)
{
if (_callback != null)
{
//SearchAsyncResult implements IAsyncResult
SearchAsyncResult result = new SearchAsyncResult();
result.IsCompleted = true;
result.CompletedSynchronously = true;
result.AsyncState = "TEST";
//result.AsyncState = e;
//at this point RESULT.ASYNCSTATE IS NEVER NULL!!!
_callback(result);
}
}
这是调用页面的代码隐藏代码(此页面设置了 Async=true 和 AsyncTimeout 值):
void searchSubmit_Click(object sender, EventArgs e)
{
foreach (RadPanelItem item in resultsPanelBar.GetAllItems())
{
//... Some Code
SearchEngine engine = new SearchEngine();
var task = new PageAsyncTask(engine.BeginSearchAsync, EndSearch, null, srchParameters, true);
RegisterAsyncTask(task);
}
}
void EndSearch(IAsyncResult asyncResult)
{
if (asyncResult.AsyncState == null)
return;
//asyncResult.AsyncState: IS ALWAYS NULL!!!
SearchCompleteEventArgs result
= (SearchCompleteEventArgs)asyncResult.AsyncState;
}