Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# C#4.0
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.
 
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 7-Jan-13 2:57am
_Nizar1.1K
Comments
jibesh at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use the lock statement[^] - the link includes examples.
  Permalink  
Comments
Member 6676 at 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.
Member 6676 at 8-Jan-13 4:31am
   
the tasks are independent from each other
OriginalGriff at 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.
Member 6676 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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);
            }
        }
  Permalink  
Comments
Rob Philpott at 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;

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

  Print Answers RSS
0 OriginalGriff 7,205
1 DamithSL 5,114
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,747
4 Kornfeld Eliyahu Peter 4,474


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100