Reputation: 2967
I want to run some part of my command line programm in parallel with multiple threads and I am afraid that there might be some static variable left that I must fix (e.g. by making it [ThreadStatic]
). Is there any tool or easy way to find these in my project?
Of course simply searching for "static"
does not help much: I have lots of static methods that work great and finde with any number of threads
Upvotes: 18
Views: 6333
Reputation: 361
First of all you have to open Find in Files
windows from Edit->Find and Replace->Find in Files
menu or Ctrl+Shift+F shortcut keys.
Then you have to use this regular expressions to search all of yours static variables in Current Project
or in Entire Solution
:
static \w*[ \t]*\b(\w+|[\w-[0-9]]\w)\b[ \t]+\b(\w+|[\w-[0-9]]\w*)\b[ \t]*[=;]
Mind check the option Use Regular Expressions
in Find options
section.
This regex also supported readonly, virtual, ...
Upvotes: 4
Reputation: 300
Note: dasblinkenlight's answer works only on Visual Studio 2010 and older.
Below is the translation for Visual Studio 2012 and newer:
static(?([^\r\n])\s)+(\b(_\w+|[\w-[0-9_]]\w*)\b)(?([^\r\n])\s)+(\b(_\w+|[\w-[0-9_]]\w*)\b)(?([^\r\n])\s)*[=;]
Translation was made referencing: http://msdn.microsoft.com/en-us/library/2k3te2cs(v=vs.110).aspx
Upvotes: 18
Reputation: 727047
Searching your files for static:b+:i:b+:i:b*[=;]
with regexp option in Visual Studio should turn up static variables for you. It will also bring operators ==
, but they should be relatively easy to filter out visually.
Upvotes: 12
Reputation: 62544
Besides of IDE tricks a real warrior's way would be using Assembly.Load()
to load application DLL into the memory and then using reflection search through all the types for public/private/protected static fields.
:)
Upvotes: 3
Reputation: 19611
The easiest way to find all of your static fields in one place is likely going to be via the Class View panel in Visual Studio (Ctrl + W, C
by default in C# mode--also under thew View
menu).
You can set some primitive filters. Unfortunately, static is not one of them. However, you might be able to use the available filters to slim down the results, based on your coding style.
Alternatively, you could make a program that uses reflection to pull out each static field and check its attributes. If ThreadStaticAttribute
is not among them, have it spit out a message. (This is along the lines of another answer, from which you might be able to obtain more details.)
Upvotes: 1