Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET C#4.0
Hi,
 
I have a list of task
 
List<Task> lstTasks = new List<Task>();
 
I am adding a method to execute into the lstTasks as below
 
lstTasks.Add(GetTasks(() => GetTasksToExecute()));
 
GetTasks() method is as below
 
private Task GetTasks<T>(Func<T> WorkMethod)
{
   return Task.Factory.StartNew(WorkMethod);
}
 
GetTasksToExecute method is as below
 
private void GetTasksToExecute()
{
    //Some code here
 
}
 

When i executed FxCop, it gave me issue CA1822 for GetTasks() and GetTasksToExecute() method
 
To fix this issue, i found that these 2 method needs to be marked as static
 
My Question:
 
1. If i mark them as static will it cause any issue in TPL?
2. Is it good to exclude this rule for these 2 methods (using code line [SuppressMessage]....) ?
3. Does this rule means i need to modify all the
private 
methods in my class to
private static
which do not access instance member of that class?
 
One more important thing:
 
According to answer posted in the below post: "
2.As you are not using the instance from the method, it doesn't affect the status of thread safety. As the method only uses the data that is sent to it, it is thread safe.
"
 
Is this true?
 

http://stackoverflow.com/questions/6483720/code-analysis-c-sharp-net-ca1822[^]
Posted 15-Mar-13 11:20am
Edited 15-Mar-13 12:55pm
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Answers to your questions:
1. I've only used TPL a very little bit, but I can see no reason that making those methods static will have any impact on use under TPL.
2. It is not a good idea to suppress this rule, unless you discover a specific reason that making the methods static causes a significant difficulty.
3. Yes, just make static any methods that do not reference class instance data or non-static methods.
 
Answer to "bonus question" Smile | :)
It depends. The thread safety of any method depends on all of the data it references. (E.g., accessed from a singleton class.)
  Permalink  
Comments
MP12 at 18-Mar-13 9:55am
   
Thanks Matt

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

  Print Answers RSS
0 OriginalGriff 406
1 /\jmot 180
2 Suraj Sahoo | Coding Passion 170
3 Afzaal Ahmad Zeeshan 159
4 BillWoodruff 154
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,634
3 Maciej Los 5,024
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 15 Mar 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