サーバーの応答を待機し、ラムダを使用して取得したときに何かを実行するコードがあります。また、このラムダのクラスivar _timedOutをチェックして、何をすべきかを確認します。ラムダが作成された後、呼び出される前に、クラス内の別の場所で_timedOutが変更された場合、ラムダは_timedOutのどの値を参照しますか?
私はこれに対する答えを求めてSOをトロールしましたが、どの答えもこの特定のクエリに対応していないようです。コード-
public class MyClass
{
public MyClass()
{
_databaseService = //...database stuff
_uploadService = //...uploads info
_serverService = //...gets stuff from the server
_uploadService.UploadingStatusChanged += UploadStatusChanged;
}
private bool _timedOut = false;
private void GetFinalInfo()
{
FinalInfo finalInfo = _databaseService.GetFinalInfo();
if (finalInfo == null) // still have no finalInfo
{
_serverService.GetLatestFinalInfo((response, theFinalInfo) =>
{
if (!_timedOut) // this could be changed elsewhere in the class while we're waiting for the server response
{
if (response == ServerResponse.Successful)
{
_databaseService.AddFinalInfo(theFinalInfo);
// navigate to next screen
}
else
{
// do something else
}
}
});
}
else
{
// navigate to next screen
}
}
}
private void UploadStatusChanged(object s, MyEventArgs e)
{
// do stuff & call GetFinalInfo if good
}
助けてくれてありがとう!