1

先週デリゲートとの作業を開始し、バックグラウンドでグリッドビューの非同期を更新しようとしています。すべてがうまくいき、エラーなどはありませんが、EndInvoke の後に結果が得られません。誰かが私が間違っていることを知っていますか?

コード スニペットを次に示します。

    public delegate string WebServiceDelegate(DataKey key);

    protected void btnCheckAll_Click(object sender, EventArgs e)
    {
        foreach (DataKey key in gvTest.DataKeys)
        {
            WebServiceDelegate wsDelegate = new WebServiceDelegate(GetWebserviceStatus);
            wsDelegate.BeginInvoke(key, new AsyncCallback(UpdateWebserviceStatus), wsDelegate);
        }
    }

    public string GetWebserviceStatus(DataKey key)
    {
        return String.Format("Updated {0}", key.Value);
    }

    public void UpdateWebserviceStatus(IAsyncResult result)
    {
        WebServiceDelegate wsDelegate = (WebServiceDelegate)result.AsyncState;

        Label lblUpdate = (Label)gvTest.Rows[???].FindControl("lblUpdate");
        lblUpdate.Text = wsDelegate.EndInvoke(result);
    }
4

1 に答える 1

0

同じ非同期呼び出しを呼び出した順序で使用してテストを実行しました。ここは問題なく動きます。Label コントロールへのハンドルの取得に問題があると思われます。その行をいくつかの行に分割して、ハンドルが適切に取得されていることを確認してください。Rows は実際に行を返しますか? FindControl は目的のコントロールを返しますか? おそらく、両方の関数でそれを確認する必要があります。

また、補足として、Rows へのインデックス作成と FindControl の使用を 1 回だけ検討することをお勧めします。IAsyncResult に渡すオブジェクトを、ハンドルをラベルに保存できるオブジェクトに置き換える必要があります。次に、一度実行して割り当ててから、UpdateWebserviceStatus で使用できます。

編集:このコードを試してください。

        public delegate void WebServiceDelegate(DataKey key);

    protected void btnCheckAll_Click(object sender, EventArgs e)
    {
        foreach (DataKey key in gvTest.DataKeys)
        {
            WebServiceDelegate wsDelegate = new WebServiceDelegate(GetWebserviceStatus);
            wsDelegate.BeginInvoke(key, new AsyncCallback(UpdateWebserviceStatus), wsDelegate);
        }
    }

    public void GetWebserviceStatus(DataKey key)
    {
        DataRow row = gvTest.Rows[key.Value];
        System.Diagnostics.Trace.WriteLine("Row {0} null", row == null ? "is" : "isn't");

        Label lblUpdate = (Label)row.FindControl("lblUpdate");
        System.Diagnostics.Trace.WriteLine("Label {0} null", lblUpdate == null ? "is" : "isn't");

        lblUpdate.Text = string.Format("Updated {0}", key.Value);
    }

    public void UpdateWebserviceStatus(IAsyncResult result)
    {
    WebServiceDelegate wsDelegate = (WebServiceDelegate)result.AsyncState;
    DataKey key = wsDelegate.EndInvoke(result);
    }
于 2010-04-06T13:52:46.013 に答える