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

C#

 
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 
I think you misunderstood my question.

I'm not trying to determine when the Windows Service is stopped... I want to know when all TASKS I created have stopped.

Review
My app will allow me to run code defined in separate assemblies (services). The host is a Windows Service. On start it loads classes from a manifest and creates instances of these classes (a service) and runs the Start method in a separate thread.

At any point a "service" could be loaded or removed. The Windows Service will always be running, but there could be any number of service threads running in it at any time. I add a new service like this (which you already helped me with)
private void AddService()
{
    // Create an instance of a service and add it to the list of services
    MyService service = new MyService();
    _services.Add(service);

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

    }, service.CancellationTokenSource.Token);

    /*
      ... ContinueWith code omitted
    */

}

Problem
Because I can add new service classes at any time, meaning install an assembly, create an instance of a type in it, and run its Start method in a new thread, how can I reliably know when ALL threads that I stated are stoppped?

Solution?
I looked at Task.WhenAll. To use this I would need to store each Task I create in a list. Then, later, in StopAllServices, I would have:
public void StopAllServices()
{
    // This I already have. Call Stop on each service. _services are classes I created that I run in a task
    foreach (var service in _services)
    {
        service.Stop();
    }

    //<==== This I would need to add...
    Task waitAllTask = Task.WhenAll(_tasks.ToArray());     
    try
    {
        waitAllTask.Wait();
    }
    catch { }
}

The problem is, what keeps waitAllTask in scope? Can I just store it as a class field?

When a new service is added (and run in another new Task), how does it get included in the WaitAll array? if I am continually added new Tasks to the waitAll array, will Task.WaitAll handle it? Also, isn't WaitAll a blocking call?

Is there a better approach to this?

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

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 
GeneralRe: Error shortcut Pin
mahdiiiiyeh8-Apr-17 2:09
mahdiiiiyeh8-Apr-17 2:09 
GeneralRe: Error shortcut Pin
OriginalGriff8-Apr-17 2:18
mveOriginalGriff8-Apr-17 2:18 

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.