Click here to Skip to main content
11,705,052 members (53,024 online)
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 10:20am
Edited 15-Mar-13 11:55am
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 Sergey Alexandrovich Kryukov 609
1 OriginalGriff 439
2 Andy Lanng 215
3 CPallini 202
4 F-ES Sitecore 160
0 OriginalGriff 8,958
1 Sergey Alexandrovich Kryukov 8,276
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,606


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 15 Mar 2013
Copyright © CodeProject, 1999-2015
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