begininvoke を使用してメソッドを非同期的に呼び出すこのサンプル コードがあります。これを Web フォームのボタン クリック イベントで実行しています。
ボタンをクリックすると、ユーザーは別のページにリダイレクトされ、そこで結果を待ちます。
AuthorizePayment メソッドは実行に時間がかかり、int コードを返します。その int 値をセッションまたは Cookie のどこかに保存したい (ただし表示しない) そのコードを追加するためにセッションにアクセスすると、null 例外がスローされます。この結果をセッションまたは Cookie に保存するにはどうすればよいですか?
何か案が?
public class CreditCardAuthorizationManager
{
// Delegate, defines signature of method(s) you want to execute asynchronously
public delegate int AuthorizeDelegate(string creditcardNumber,
DateTime expiryDate,
double amount);
// Method to initiate the asynchronous operation
public void StartAuthorize()
{
AuthorizeDelegate ad = new AuthorizeDelegate(AuthorizePayment);
IAsyncResult ar = ad.BeginInvoke(creditcardNumber,
expiryDate,
amount,
new AsyncCallback(AuthorizationComplete),
null);
}
// Method to perform a time-consuming operation (this method executes
// asynchronously on a thread from the thread pool)
private int AuthorizePayment(string creditcardNumber,
DateTime expiryDate,
double amount)
{
int authorizationCode = 0;
// Open connection to Credit Card Authorization Service ...
// Authorize Credit Card (assigning the result to authorizationCode) ...
// Close connection to Credit Card Authorization Service ...
return authorizationCode;
}
// Method to handle completion of the asynchronous operation
public void AuthorizationComplete(IAsyncResult ar)
{
// See "Managing Asynchronous Completion with the EndInvoke Method"
// later in this chapter.
}
}