Click here to Skip to main content
11,933,253 members (63,311 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# .NET C#4.0

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
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?[^]
Posted 15-Mar-13 11:20am
Edited 15-Mar-13 12:55pm

1 solution

Rate this: bad
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.)
MP12 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151126.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