Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I run this code and Parallel == true all tasks access the properties of one task as they are static, so all the tasks that run will access the same proberties and the program will crash.

C#
private List<BackupTask> backupTasks = new List<BackupTask>();

public void RunBackup()
        {
            if (Parallel == false)
            {
                foreach (var item in backupTasks)
                {
                    item.RunBackup();
                }
            }
            else
            {
                List<Task> tasks = new List<Task>();
                foreach (var item in backupTasks)
                {
                    var t = Task.Factory.StartNew(() =>
                    {
                        item.RunBackup();
                    });
                    tasks.Add(t);
                }
                Task.WaitAll(tasks.ToArray());
            }
        }
Posted
Comments
Jibesh 7-Jan-13 14:19pm    
You need to provide some more details about the crash and what's happening inside the method item.RunBackup, one method is to put the lock statement around your shared resources.

C#
public void RunBackup()
        {
            if (Parallel == false)
            {
                foreach (var item in backupTasks)
                {
                    item.RunBackup();
                }
            }
            else
            {
                //List<Task> tasks = new List<Task>();
                //foreach (var item in backupTasks)
                //{
                //    var t = Task.Factory.StartNew(() =>
                //    {
                //        item.RunBackup();
                //    });
                //    tasks.Add(t);
                //}
                //Task.WaitAll(tasks.ToArray());
                var tasks = new Task[backupTasks.Count];
                for (var i = 0; i < backupTasks.Count; i++)
                {
                    var x = i;
                    tasks[i] = Task.Factory.StartNew(() => backupTasks[x].RunBackup());
                }
                Task.WaitAll(tasks);
            }
        }
 
Share this answer
 
Comments
Rob Philpott 28-Jan-13 11:34am    
Yes - surely this is one of those late execution nasties, and the magic that fixes it is var x = i;
Use the lock statement[^] - the link includes examples.
 
Share this answer
 
Comments
_Nizar 8-Jan-13 2:48am    
Using lock statement will be benefit if I'm using one object and i access its method from many threads.
But in my case I'm using one object per Task they share nothing, so why the access the same property (class variable).
I want to publish my program here but after i have fixed this problem.
_Nizar 8-Jan-13 4:31am    
the tasks are independent from each other
OriginalGriff 8-Jan-13 4:34am    
Except you say that they are using static data...
"all tasks access the properties of one task as they are static, so all the tasks that run will access the same proberties"

Which means they aren't independent.
_Nizar 9-Jan-13 5:37am    
The data are not static.
Now after some investigation I found that when I run the code using the debugger so I give all the tasks time to finish before the next task will begin very thing work fine.
But if I run the code using also the debugger but with out giving the firs task time to finish before the next task the last task runs twice so a conflicting happen and the first task never runs.
I'm ready to give my program for investigation.
I have only this problem ales it is working fine in sequence.

thank you for your support

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900