Click here to Skip to main content
16,021,430 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# Task Faulted Problem Pin
Pete O'Hanlon11-Apr-17 5:09
mvePete O'Hanlon11-Apr-17 5:09 
GeneralRe: C# Task Faulted Problem Pin
Kevin Marois11-Apr-17 5:30
professionalKevin Marois11-Apr-17 5:30 
GeneralRe: C# Task Faulted Problem Pin
Kevin Marois11-Apr-17 5:34
professionalKevin Marois11-Apr-17 5:34 
GeneralRe: C# Task Faulted Problem Pin
Kevin Marois11-Apr-17 5:49
professionalKevin Marois11-Apr-17 5:49 
GeneralRe: C# Task Faulted Problem Pin
Pete O'Hanlon11-Apr-17 7:01
mvePete O'Hanlon11-Apr-17 7:01 
GeneralRe: C# Task Faulted Problem Pin
Kevin Marois11-Apr-17 7:06
professionalKevin Marois11-Apr-17 7:06 
GeneralRe: C# Task Faulted Problem Pin
Pete O'Hanlon11-Apr-17 7:55
mvePete O'Hanlon11-Apr-17 7:55 
GeneralRe: C# Task Faulted Problem Pin
Kevin Marois12-Apr-17 6:52
professionalKevin Marois12-Apr-17 6:52 
OK, one more thing. I now have this
private void AddService()
{
    // Create an instance of a service and add it to the list of services
    MyService service = new MyService();
    _services.Add(service);

    // Subscribe to the services' status event
    service.StatusChanged += Service_StatusChanged;

    // Write a message to the UI
    AddStatus(string.Format("Service '{0}' created", service.ServiceId));

    // Start the task
    Task task = Task.Factory.StartNew(() =>
    {
        service.Start();

    }, service.CancellationTokenSource.Token);

    // Handle task completion
    task.ContinueWith(antecedent => 
    {
        AddStatus(string.Format("Service '{0}' completed", service.ServiceId));

    }, TaskContinuationOptions.OnlyOnRanToCompletion);

    // Handle task cancellation
    task.ContinueWith(antecedent => 
    {
        AddStatus(string.Format("Service '{0}' cancelled", service.ServiceId));

    }, TaskContinuationOptions.OnlyOnCanceled);

    // Handle task faulted
    task.ContinueWith(antecedent => 
    {
        AddStatus(string.Format("Service '{0}' errored", service.ServiceId));
        AddStatus(task.Exception.ToString());

    }, TaskContinuationOptions.OnlyOnFaulted);

    // Task is finished
    task.ContinueWith(antecedent =>
    {
        // Remove the service
        App.Current.Dispatcher.Invoke(() =>
        {
            var serviceModel = Services.FirstOrDefault(x => x.ServiceId == service.ServiceId);
            Services.Remove(serviceModel);

            // Remove the model from the UI
            SelectedService = null;

            AddStatus("Service removed");
        });

        _services.Remove(service);
    });
}

As you know, I'm storing my services in a list called "_services". Works great. I also have a Stopped method:
private void Stopped()
{
    IsStopped = true;

    foreach (var service in _runningServices)
    {
        service.Stop();
    }

    _runningServices.Clear();
}

Given how I'm storing the service instances, how can I tell when all have finally stopped? I see this but I'm not sure this is what I want.

Bear in mind that new tasks can be started and added to the _services collection at any time. The Stopped sets IsStopped to True, after which no new services can start. But there could be any number of services running that have yet to complete.

Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.


modified 12-Apr-17 13:04pm.

GeneralRe: C# Task Faulted Problem Pin
Pete O'Hanlon12-Apr-17 21:45
mvePete O'Hanlon12-Apr-17 21:45 
GeneralRe: C# Task Faulted Problem Pin
Kevin Marois13-Apr-17 5:34
professionalKevin Marois13-Apr-17 5:34 
QuestionXML inputs to telnet console in c# Pin
nikhil201510-Apr-17 2:47
nikhil201510-Apr-17 2:47 
QuestionRe: XML inputs to telnet console in c# Pin
Richard MacCutchan10-Apr-17 2:53
mveRichard MacCutchan10-Apr-17 2:53 
AnswerRe: XML inputs to telnet console in c# Pin
Pete O'Hanlon10-Apr-17 2:55
mvePete O'Hanlon10-Apr-17 2:55 
Questionc# Events Redirection in Designer.cs and Visual Studio 2017 More Problems. Pin
zequion7-Apr-17 20:57
professionalzequion7-Apr-17 20:57 
AnswerRe: c# Events Redirection in Designer.cs and Visual Studio 2017 More Problems. Pin
Richard MacCutchan7-Apr-17 21:33
mveRichard MacCutchan7-Apr-17 21:33 
AnswerRe: c# Events Redirection in Designer.cs and Visual Studio 2017 More Problems. Pin
Gerry Schmitz9-Apr-17 8:56
mveGerry Schmitz9-Apr-17 8:56 
AnswerRe: c# Events Redirection in Designer.cs and Visual Studio 2017 More Problems. Pin
BillWoodruff11-Apr-17 3:51
professionalBillWoodruff11-Apr-17 3:51 
AnswerRe: c# Events Redirection in Designer.cs and Visual Studio 2017 More Problems. Pin
Pete O'Hanlon11-Apr-17 4:13
mvePete O'Hanlon11-Apr-17 4:13 
QuestionError shortcut Pin
mahdiiiiyeh7-Apr-17 19:22
mahdiiiiyeh7-Apr-17 19:22 
QuestionRe: Error shortcut Pin
Richard MacCutchan7-Apr-17 21:31
mveRichard MacCutchan7-Apr-17 21:31 
AnswerRe: Error shortcut Pin
mahdiiiiyeh8-Apr-17 0:27
mahdiiiiyeh8-Apr-17 0:27 
GeneralRe: Error shortcut Pin
Richard MacCutchan8-Apr-17 0:30
mveRichard MacCutchan8-Apr-17 0:30 
AnswerRe: Error shortcut Pin
OriginalGriff7-Apr-17 21:33
mveOriginalGriff7-Apr-17 21:33 
GeneralRe: Error shortcut Pin
mahdiiiiyeh8-Apr-17 0:21
mahdiiiiyeh8-Apr-17 0:21 
GeneralRe: Error shortcut Pin
OriginalGriff8-Apr-17 0:52
mveOriginalGriff8-Apr-17 0:52 

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.