Click here to Skip to main content
15,887,214 members
Home / Discussions / C#
   

C#

 
GeneralMessage Closed Pin
22-Apr-17 17:55
sasolanki00722-Apr-17 17:55 
AnswerRe: Create an ASMX web service and a web client to simulate a stock exchange and a user customizable stocks ticker Pin
OriginalGriff22-Apr-17 19:48
mveOriginalGriff22-Apr-17 19:48 
GeneralMessage Closed Pin
26-Apr-17 19:16
sasolanki00726-Apr-17 19:16 
GeneralRe: Create an ASMX web service and a web client to simulate a stock exchange and a user customizable stocks ticker Pin
OriginalGriff26-Apr-17 20:17
mveOriginalGriff26-Apr-17 20:17 
QuestionRe: Create an ASMX web service and a web client to simulate a stock exchange and a user customizable stocks ticker Pin
ZurdoDev11-Apr-17 4:34
professionalZurdoDev11-Apr-17 4:34 
AnswerRe: ASMX Web Services Pin
Gerry Schmitz11-Apr-17 7:26
mveGerry Schmitz11-Apr-17 7:26 
AnswerRe: ASMX Web Services Pin
sasolanki00730-Apr-17 17:57
sasolanki00730-Apr-17 17:57 
QuestionC# Task Faulted Problem Pin
Kevin Marois10-Apr-17 8:39
professionalKevin Marois10-Apr-17 8:39 
I have the following code:
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
    var newTask = Task.Factory.StartNew(() =>
    {
        service.Start();

    }, service.CancellationTokenSource.Token)
        .ContinueWith(task =>
        {
            switch (task.Status)
            {
                // The task ran to completion
                case TaskStatus.RanToCompletion:
                    AddStatus(string.Format("Service '{0}' completed", service.ServiceId));
                    break;

                // The task was cancelled
                case TaskStatus.Canceled:
                    AddStatus(string.Format("Service '{0}' cancelled", service.ServiceId));
                    break;

                // The task threw an exception
                case TaskStatus.Faulted:
                    AddStatus(string.Format("Service '{0}' errored", service.ServiceId));
                    AddStatus(task.Exception.ToString());
                    break;
            }

            // Remove the service
            App.Current.Dispatcher.Invoke(() =>
            {
                // Remove the model from the UI
                Services.Remove(SelectedService);
                SelectedService = null;
            });

            // Remove the service from the list of services
            _services.Remove(service);

            AddStatus(string.Format("Service '{0}' removed", service.ServiceId));
        });
}

Towards the bottom is a section called "Remove the service". This removes the UI model from the list and removes the service class from the internal list of services.

If the Task.Status is RanToCompletion or Canceled, the the remove code works fine. However, if Task.Status is Faulted then the UI portion inside the Invoke doesn't work. It hits the
Services.Remove(SelectedService);
SelectedService = null;

but doesn't actually remove the model.

Anyone know what's wrong?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.

AnswerRe: C# Task Faulted Problem Pin
User 418025410-Apr-17 10:09
User 418025410-Apr-17 10:09 
GeneralRe: C# Task Faulted Problem Pin
Kevin Marois10-Apr-17 10:32
professionalKevin Marois10-Apr-17 10:32 
GeneralRe: C# Task Faulted Problem Pin
User 418025410-Apr-17 10:58
User 418025410-Apr-17 10:58 
AnswerRe: C# Task Faulted Problem Pin
Pete O'Hanlon10-Apr-17 20:21
mvePete O'Hanlon10-Apr-17 20:21 
GeneralRe: C# Task Faulted Problem Pin
Kevin Marois11-Apr-17 4:23
professionalKevin Marois11-Apr-17 4:23 
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 
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 

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.