Click here to Skip to main content
15,888,590 members
Home / Discussions / C#
   

C#

 
GeneralRe: How can I speed up my code? Pin
turbosupramk319-Jul-16 9:36
turbosupramk319-Jul-16 9:36 
GeneralRe: How can I speed up my code? Pin
Dave Kreskowiak19-Jul-16 10:26
mveDave Kreskowiak19-Jul-16 10:26 
GeneralRe: How can I speed up my code? Pin
turbosupramk319-Jul-16 10:35
turbosupramk319-Jul-16 10:35 
GeneralRe: How can I speed up my code? Pin
Dave Kreskowiak19-Jul-16 12:57
mveDave Kreskowiak19-Jul-16 12:57 
GeneralRe: How can I speed up my code? Pin
turbosupramk320-Jul-16 4:13
turbosupramk320-Jul-16 4:13 
GeneralRe: How can I speed up my code? Pin
Dave Kreskowiak20-Jul-16 5:17
mveDave Kreskowiak20-Jul-16 5:17 
Generalc# please give me a running program for Speechconvert to text Pin
Member 214622319-Jul-16 3:24
Member 214622319-Jul-16 3:24 
GeneralRe: c# please give me a running program for Speechconvert to text Pin
OriginalGriff19-Jul-16 4:07
mveOriginalGriff19-Jul-16 4:07 
QuestionHow can I monitor CPU and monitor utilisation for 1 process and plot in the graph Pin
anujlamba19-Jul-16 2:39
anujlamba19-Jul-16 2:39 
AnswerRe: How can I monitor CPU and monitor utilisation for 1 process and plot in the graph Pin
Richard MacCutchan19-Jul-16 3:11
mveRichard MacCutchan19-Jul-16 3:11 
QuestionSet RequestValidationMode at control level in web config Pin
Member 1264210518-Jul-16 21:22
Member 1264210518-Jul-16 21:22 
AnswerRe: Set RequestValidationMode at control level in web config Pin
Slacker00719-Jul-16 2:09
professionalSlacker00719-Jul-16 2:09 
SuggestionAbout Async Event Pin
Super Lloyd18-Jul-16 14:47
Super Lloyd18-Jul-16 14:47 
GeneralRe: About Async Event Pin
Brisingr Aerowing18-Jul-16 16:16
professionalBrisingr Aerowing18-Jul-16 16:16 
GeneralRe: About Async Event Pin
Super Lloyd18-Jul-16 16:57
Super Lloyd18-Jul-16 16:57 
GeneralRe: About Async Event Pin
Marc Clifton19-Jul-16 1:07
mvaMarc Clifton19-Jul-16 1:07 
GeneralRe: About Async Event Pin
Super Lloyd19-Jul-16 2:44
Super Lloyd19-Jul-16 2:44 
GeneralRe: About Async Event Pin
Marc Clifton19-Jul-16 1:50
mvaMarc Clifton19-Jul-16 1:50 
GeneralRe: About Async Event Pin
Richard Deeming19-Jul-16 1:55
mveRichard Deeming19-Jul-16 1:55 
AnswerRe: About Async Event Pin
Super Lloyd19-Jul-16 3:07
Super Lloyd19-Jul-16 3:07 
GeneralRe: About Async Event Pin
Super Lloyd19-Jul-16 3:19
Super Lloyd19-Jul-16 3:19 
GeneralRe: About Async Event Pin
Super Lloyd19-Jul-16 14:32
Super Lloyd19-Jul-16 14:32 
GeneralRe: About Async Event Pin
Richard Deeming20-Jul-16 1:51
mveRichard Deeming20-Jul-16 1:51 

It is important to notice that you cannot easily unsubscribe from an event if you used an anonymous function to subscribe to it. To unsubscribe in this scenario, it is necessary to go back to the code where you subscribe to the event, store the anonymous method in a delegate variable, and then add the delegate to the event. In general, we recommend that you do not use anonymous functions to subscribe to events if you will have to unsubscribe from the event at some later point in your code.

I suspect your first sample works because you only have one anonymous method, which translates to a single hidden method:
C#
Func<Task> handler = HiddenAnonymousMethod1;
if (source != null)
    source.AlarmReceived -= handler;
source = value;
if (source != null)
    source.AlarmReceived += handler;

In the second example, even though both anonymous methods are the same, they are probably translated to two different hidden methods:
C#
if (source != null)
    source.AlarmReceived -= HiddenAnonymousMethod1;
source = value;
if (source != null)
    source.AlarmReceived += HiddenAnonymousMethod2;

You'd need to check the IL to verify that this is the case.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: About Async Event Pin
Super Lloyd20-Jul-16 2:08
Super Lloyd20-Jul-16 2:08 
GeneralRe: About Async Event Pin
Richard Deeming19-Jul-16 1:54
mveRichard Deeming19-Jul-16 1:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.