ASP.net BeginInvoke无法正常工作

时间:2016-04-28 14:56:22

标签: c# asp.net delegates begininvoke

我确信这个问题会重复,但即使尝试按照几个例子后的多种方式进行更改,我也无法让这段代码正常工作。 当我尝试运行此代码时,出现错误

  

对象引用未设置为对象的实例

尝试在回调函数中触发EndInvoke。事实是,该计划甚至没有进入MyMethod

public partial class Ana_UI_CONTROLS_PrintReport : System.Web.UI.UserControl
{
    public delegate DataTable 
      GetDataDelegate(string a, string b, string c, string d, DateTime date);

    public GetDataDelegate myDelegate;

    public void AsyncExport()
    {
        myDelegate = new GetDataDelegate(MyMethod);
        myDelegate.BeginInvoke(
          a, b, c, d, date, new AsyncCallback(LoadDataTableGridCallBack), null);
    }

    private DataTable MyMethod(string a, string b, string c, string d, DateTime date)
    {
        //do something
        return result;
    }

    private void LoadDataTableGridCallBack(IAsyncResult result)
    {
        AsyncResult aResult = (AsyncResult)result;
        GetDataDelegate myDelegate = (GetDataDelegate)aResult.AsyncDelegate;

        DataTable ris = new DataTable();
        try
        {
            ris = myDelegate.EndInvoke(result);
        }
        catch (Exception ex)
        {
            //handle exception.
        }
    }
}

0 个答案:

没有答案