Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a list of task

C#
List<Task> lstTasks = new List<Task>();


I am adding a method to execute into the lstTasks as below

C#
lstTasks.Add(GetTasks(() => GetTasksToExecute()));


GetTasks() method is as below

C#
private Task GetTasks<T>(Func<T> WorkMethod)
{
   return Task.Factory.StartNew(WorkMethod);
}


GetTasksToExecute method is as below

C#
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
C#
private 
methods in my class to
C#
private static
which do not access instance member of that class?

One more important thing:

According to answer posted in the below post: "
SQL
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
Updated 15-Mar-13 11:55am
v3

1 solution

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" :)
It depends. The thread safety of any method depends on all of the data it references. (E.g., accessed from a singleton class.)
 
Share this answer
 
Comments
Member 3660629 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)



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