static void VoterCallback(Response<long> response) { if (response.Status == ResponseStatus.Success) Interlocked.Increment(ref VoteResults[(int)response.Result]); else Interlocked.Increment(ref VoteResults[3]); }
/// <summary> /// This is our callback method for the Vote procedure. /// Note it is strongly typed to receive a response that carries a single INTEGER /// </summary> /// <param name="response">The procedure response object</param> static void VoterCallback(Response<long> response) { // The response object carries a lot of information with it: // - The name of the procedure and parameters passed // - Details of the response status as returned by the server // - In case of failure, Exception details about the reason // - Finally, what you really care about, a stringly-typed "Result" property that is, well... the result // of the procedure call // // It is good practice to check that the procedure was successful before getting the result (if you don't // and the response failed, you will then get an exception! // Callbacks are ALWAYS triggered. If there is a failure, you can retrieve its details from the Response // object - there will not be an Exception throw without you first having a chance to deal with the matter. // You can either you the pattern below, or leverage the .TryGetResult(out result) method also available to // you on the Response object. // // Note that because asynchronous calls are, by nature, multi-threaded, you need to code in a thread-safe // manner if you modify shared resources. // So here, we make sure we use the Interlocked model to increment our counters, a safe, lightweight // framework for updating shared values. if (response.Status == ResponseStatus.Success) Interlocked.Increment(ref VoteResults[(int)response.Result]); else Interlocked.Increment(ref VoteResults[3]); }
/// <summary> /// Push a new request to the internal stack where it will wait until timeout or the server responds. /// </summary> /// <param name="response">The prepared response state object to push into the stack.</param> public void AddItem(Response response) { Interlocked.Increment(ref this._Size); lock(this.Lock) this.Cache.Add(response.ExecutionId, response); }