Click here to Skip to main content
15,879,535 members
Home / Discussions / C#
   

C#

 
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 
If this:

AlarmReceived += () => Task.Run(() => Console.WriteLine("A"));

is the correct syntax, then this:

AlarmReceived -= () => Task.Run(() => Console.WriteLine("A"));

doesn't unwire the delegate because it's a new instance. This "simpler" case (the way I might have tried it, because I didn't know about the multicast thingy) also doesn't unwire the alarms:
static void Main(string[] args)
{
    new Program();
    Console.ReadLine();
}

public Program()
{
    DoIt2();
    Console.WriteLine("Waiting for alarms to process...");
}

List<Action> alarmActions = new List<Action>();

public void AddAlarm(Action act)
{
    alarmActions.Add(act);
}

public void RemoveAlarm(Action act)
{
    alarmActions.Remove(act);
}

public async void DoIt2()
{
    Console.WriteLine("Adding alarms");
    AddAlarm(() => Alarm1());
    AddAlarm(() => Alarm2());
    await Task.Run(() => ProcessAlarms());
    Console.WriteLine("Removing alarms");
    RemoveAlarm(() => Alarm1());
    RemoveAlarm(() => Alarm2());
    await Task.Run(() => ProcessAlarms());
}

public void ProcessAlarms()
{
    alarmActions.ForEach(a => a());
}

public void Alarm1()
{
    System.Threading.Thread.Sleep(1000);
    Console.WriteLine("Alarm 1");
}

public void Alarm2()
{
    Console.WriteLine("Alarm 2");
}

The only way to remove them is to instantiate the Action so the action isn't an anonymous method.
Action a = () => Alarm1();
Action b = () => Alarm2();
Console.WriteLine("Adding alarms");
AddAlarm(a);
AddAlarm(b);
await Task.Run(() => ProcessAlarms());
Console.WriteLine("Removing alarms");
RemoveAlarm(a);
RemoveAlarm(b);
await Task.Run(() => ProcessAlarms());

That approach is risky because, as the previous example showed, you could certainly wire up the alarm as an anonymous method, and the unwary programmer might think you can unwire it that way too, but you can't.

In your case, because AlarmReceived is an event of Task, I don't see how you can get away from an anonymous method:
Task a = Task.Run(() =>
{
    System.Threading.Thread.Sleep(1000);
    Console.WriteLine("A");
}));

AlarmReceived += () => a;

The funny thing is, this ACTS like the event has been unwired because the task has been run!

Given:
        public async void DoIt()
        {
            Task a = Task.Run(() =>
            {
                System.Threading.Thread.Sleep(1000);
                Console.WriteLine("A");
            });

<pre>
        Task b = Task.Run(() => Console.WriteLine("B"));

        AlarmReceived += () => a;
        AlarmReceived += () => b;
        Console.WriteLine("Processing alarms");
        await OnAlarm();

        Console.WriteLine("Unwire");
        AlarmReceived -= () => a;
        AlarmReceived -= () => b;
        await OnAlarm();
    }

    internal async Task OnAlarm()
    {
        if (eAlarmReceived == null)
        {
            Console.WriteLine("eAlarmReceived is null!");
            return;
        }

        // REMARK: ToList() to capture the event list at the moment of the call
        var eTasks = eAlarmReceived.GetInvocationList().Cast<Func<Task>>().ToList();

        if (eTasks.Count == 0)
        {
            Console.WriteLine("Nothing to do!");
        }
        else
        {
            Console.WriteLine("Tasks = " + eTasks.Count);
            foreach (var t in eTasks)
                await t();
        }
    }</pre>

The output is:
Processing alarms
B
Tasks = 2
Waiting for alarms to process...
A
Unwire
Tasks = 2

But again, the use of the anonymous method for AlarmReceived means you can't unwire it.

Am I doing something different in the use case than you are?

Marc
Imperative to Functional Programming Succinctly

Contributors Wanted for Higher Order Programming Project!

Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

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 
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 
GeneralRe: About Async Event Pin
Super Lloyd19-Jul-16 2:55
Super Lloyd19-Jul-16 2:55 
QuestionHow can I run multiple powershell commands in a C#/powershell runspace? Pin
turbosupramk318-Jul-16 4:02
turbosupramk318-Jul-16 4:02 
QuestionRe: How can I run multiple powershell commands in a C#/powershell runspace? Pin
Richard MacCutchan18-Jul-16 4:22
mveRichard MacCutchan18-Jul-16 4:22 
AnswerRe: How can I run multiple powershell commands in a C#/powershell runspace? Pin
turbosupramk318-Jul-16 4:35
turbosupramk318-Jul-16 4:35 
GeneralRe: How can I run multiple powershell commands in a C#/powershell runspace? Pin
Richard MacCutchan18-Jul-16 4:51
mveRichard MacCutchan18-Jul-16 4:51 
GeneralRe: How can I run multiple powershell commands in a C#/powershell runspace? Pin
turbosupramk318-Jul-16 4:58
turbosupramk318-Jul-16 4:58 
GeneralRe: How can I run multiple powershell commands in a C#/powershell runspace? Pin
Richard MacCutchan18-Jul-16 6:15
mveRichard MacCutchan18-Jul-16 6:15 
GeneralRe: How can I run multiple powershell commands in a C#/powershell runspace? Pin
turbosupramk318-Jul-16 7:49
turbosupramk318-Jul-16 7:49 
GeneralRe: How can I run multiple powershell commands in a C#/powershell runspace? Pin
turbosupramk319-Jul-16 4:10
turbosupramk319-Jul-16 4:10 
QuestionClose Form Popup before opening MessageBox.Show and the form use delegate ? Pin
Member 245846717-Jul-16 22:47
Member 245846717-Jul-16 22:47 

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.