Take the 2-minute tour ×

I am using the c# TaskScheduler to start some tasks and it is throwing a TaskCanceledException somehow. I stepped all the way through the function the task calls and it completes fine and no exceptions are thrown at all. I am using IEnumerable and yield for part of the stuff in the master task function, not sure if that causes issues with tasks, wouldn't think it would. I am so lost right now, I don't see anything wrong with my code and nothing is failing I can see in the task thread.

I simplified my code to the below but this is the structure.

Action[] Jobs = new Action[] { Job1(), Job2(), etc };
Task[] Tasks = new Task[Jobs.Length];

    int I = 0;

    foreach (Action Act in Jobs)
        Tasks[I] = Task.Factory.StartNew(() => Act());
    Task.WaitAll(Tasks, 300000);
catch (AggregateException AgEx) { throw AgEx.Flatten(); } 

I also tried this and it works fine

Thread NewJob = new Thread(() => DoJob());
NewJob.IsBackground = true;
share|improve this question
a for loop seems better than a foreach in this. Also, I and Act? Uppercase variables are horrendous. Use i and act. –  Cole Johnson Jun 29 '12 at 0:21
Can you provide a complete piece of code that shows the problem? What are the exact types of Job1 and Job2? –  James Manning Jun 29 '12 at 0:28
For debugging, get rid of the try/catch. In the debugger with break on throw, where does it throw? What's the stack? You're leaving out too much info/context AFAICT –  James Manning Jun 29 '12 at 0:30
If you just execute the actions in the loop instead of starting tasks, how does it behave? –  James Manning Jun 29 '12 at 0:32
Yea I probably should use a for loop. I don't really want to post all the code because it is private. I will try removing all the try catches. I will try it without task and see what happens tomorrow. I don't think uppercase variables are that bad, they are for single letters like I did. It all depends what you are use too. I happen to hate them, it's all preference. –  kyleb Jun 29 '12 at 5:14
show 3 more comments


Your Answer



Browse other questions tagged or ask your own question.