私は 1 つの Web ページ MyWebPage.aspx を持っています。これは、ロード中に 2 つの Web サービスからのデータを独自のアルゴリズムとともに表示する必要があります。
1) WebServiceI.SomeMethod() -> Takes 10 seconds aprx. to respond.
2) WebServiceII.SomeMethod() -> Takes 10 seconds aprx. to respond.
3) My Algorithm -> Takes 5 second aprx to respond.
これを同期的に呼び出すと、読み込みに 10+10+5 = 25 秒かかります。
そこで、IAsyncResult/AsyncCallbackを使った「非同期呼び出し方式」を提案されました。これで、すべてが同時に呼び出され、ページが最大 10 秒で読み込まれるようになります。
だから私は今、「開始/終了」の方法でそれらを呼び出します...
public partial class MyWebPage : System.Web.UI.Page
{
WebServiceI WebServiceIObject = new WebServiceI();
WebServiceII WebServiceIIObject = new WebServiceII();
protected void Page_Load(object sender, EventArgs e)
{
//BeginSomeMethod(AsyncCallback callback, object asyncState)[<- Method Signature]
WebServiceIObject.BeginSomeMethod(OnEndGetWebServiceISomeMethodResult, null);
//BeginSomeMethod(AsyncCallback callback, object asyncState)[<- Method Signature]
WebServiceIIObject.BeginSomeMethod(OnEndGetWebServiceIISomeMethodResult, null);
/* My Algorithm 5 seconds*/
DataSet DS = GetDataSetFromSomeWhere();
MyGataGrid.DataSource = DS.tables[0];
MyGataGrid.DataBind();
/* My Algorithm 5 seconds*/
//System.Threading.Thread.Sleep(6000);
}
//Will be called after 10 seconds
void OnEndGetWebServiceISomeMethodResult(IAsyncResult asyncResult)
{
string WebServiceISomeMethodResult = WebServiceIObject.EndSomeMethod(asyncResult);
MyLabelI.Text = WebServiceISomeMethodResult;
//EventLog MyLog = new EventLog("Application"); MyLog.Source = "MySourceI";
//MyLog.WriteEntry(DateTime.Now.ToString());
}
//Will be called after 10 seconds
void OnEndGetWebServiceIISomeMethodResult(IAsyncResult asyncResult)
{
string WebServiceIISomeMethodResult = WebServiceIIObject.EndSomeMethod(asyncResult);
MyLabelII.Text = WebServiceIISomeMethodResult;
//EventLog MyLog = new EventLog("Application"); MyLog.Source = "MySourceII";
//MyLog.WriteEntry(DateTime.Now.ToString());
}
}
上記の例の問題は、ページが 5 秒後に読み込まれるため、MyLabelI と MyLabelII Text が設定されないことです。
EventLog への書き込みでチェックされるように、両方の End メソッドが正しく呼び出されます。これを解決するにはどうすればよいですか...「すべてが一度に開始され、すべてが完了するまで待機します...」のようなものです。実行中のスレッドが 5 秒以上待機すると、必要に応じてコードが実行されることを理解しています..
AsyncWaitHandle の使用方法...