Note that there are some explanatory texts on larger screens.

plurals
  1. PORemoving the event handler on wcf service call
    primarykey
    data
    text
    <p>I recently came across this code:</p> <pre><code> public static class ClientBaseExtender { /// &lt;summary&gt; /// Tries to execute async service call. If &lt;see cref="TimeoutException"/&gt; occured retries again. /// &lt;/summary&gt; /// &lt;typeparam name="TChannel"&gt;ServiceClient class.&lt;/typeparam&gt; /// &lt;typeparam name="TArgs"&gt;Type of service client method return argument.&lt;/typeparam&gt; /// &lt;param name="client"&gt;ServiceClient instance.&lt;/param&gt; /// &lt;param name="tryExecute"&gt;Delegate that execute starting of service call.&lt;/param&gt; /// &lt;param name="onCompletedSubcribe"&gt;Delegate that subcribes an event handler to the OnCompleted event of the service client method.&lt;/param&gt; /// &lt;param name="onCompleted"&gt;Delegate that executes when service call is succeeded.&lt;/param&gt; /// &lt;param name="onError"&gt;Delegate that executes when service call fails.&lt;/param&gt; /// &lt;param name="maxAttempts"&gt;Maximum attempts to execute service call before error if &lt;see cref="TimeoutException"/&gt; occured (by default 5).&lt;/param&gt; public static void ExecuteAsyncRepeatedly&lt;TChannel, TArgs&gt;(this ClientBase&lt;TChannel&gt; client, Action tryExecute, Action&lt;EventHandler&lt;TArgs&gt;&gt; onCompletedSubcribe, EventHandler&lt;TArgs&gt; onCompleted, EventHandler&lt;TArgs&gt; onError, int maxAttempts) where TChannel : class where TArgs : AsyncCompletedEventArgs { int attempts = 0; var serviceName = client.GetType().Name; onCompletedSubcribe((s, e) =&gt; { if (e.Error == null) // Everything is OK { if (onCompleted != null) onCompleted(s, e); ((ICommunicationObject)client).Close(); Debug.WriteLine("[{1}] Service '{0}' closed.", serviceName, DateTime.Now); } else if (e.Error is TimeoutException) { attempts++; if (attempts &gt;= maxAttempts) // Final timeout after n attempts { Debug.WriteLine("[{2}], Final Timeout occured in '{0}' service after {1} attempts.", serviceName, attempts, DateTime.Now); if (onError != null) onError(s, e); client.Abort(); Debug.WriteLine("[{1}] Service '{0}' aborted.", serviceName, DateTime.Now); return; } // Local timeout Debug.WriteLine("[{2}] Timeout occured in '{0}' service (attempt #{1}).", serviceName, attempts, DateTime.Now); Debug.WriteLine("[{2}] Attempt #{0} to execute call to '{1}' service.", attempts + 1, serviceName, DateTime.Now); tryExecute(); // Try again. } else { if (onError != null) onError(s, e); client.Abort(); Debug.WriteLine("[{1}] Service '{0}' aborted.", serviceName, DateTime.Now); } }); Debug.WriteLine("[{2}] Attempt #{0} to execute call to '{1}' service.", attempts + 1, serviceName, DateTime.Now); tryExecute(); // First attempt to execute } } public void GetData() { var client = new MyServiceClient(); client.ExecuteAsyncRepeatedly(() =&gt; client.MyOperationAsync(...), (EventHandler&lt;MyOperationCompletedEventArgs&gt; handler) =&gt;client.MyOperationCompleted += handler, (s, e) =&gt; // OnCompleted { Do(e.Result); }, (s, e) =&gt; // OnError { HandleError(e.Error); } ); </code></pre> <p>}</p> <p>The problem is, I have a button that fires this code off. When the button is pushed more than once the handler gets added again and again. This is a problem because the code will fire as many times as the user has pushed the button. How can I remove the handler created with the lambda expression in this code so it will only run once?</p> <p>Thanks!</p> <p>EDIT:</p> <p>I'm calling the code like this from my button click command:</p> <pre><code> _dataService.GetData(GetDataCompleted); private void GetDataComplete(Data data) { //do something with data } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload