Don't use dangerous threading methods
Warns about Thread.Abort(), Sleep(), Suspend() and Resume():
- Thread.Abort() is dangerous: http://www.interact-sw.co.uk/iangblog/2004/11/12/cancellation
- Thread.Sleep() is a sign of flawed design: http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx
- Suspend() and Resume() are dangerous and obsolete; workaround: http://stackoverflow.com/questions/382173/what-are-alternative-ways-to-suspend-and-resume-a-thread
- Thread.Abort() is dangerous: http://www.interact-sw.co.uk/iangblog/2004/11/12/cancellation
- Thread.Sleep() is a sign of flawed design: http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx
- Suspend() and Resume() are dangerous and obsolete; workaround: http://stackoverflow.com/questions/382173/what-are-alternative-ways-to-suspend-and-resume-a-thread
How to fix
Suppress the calls listed in suppressCallsTo; use the Task Parallel Library (TPL) instead: https://msdn.microsoft.com/en-us/library/dd460717(v=vs.110).aspx
CQLinq source
This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.
// <Name>Don't use dangerous threading methods</Name>
// <Id>ND2501:DontUseDangerousThreadingMethods</Id>
warnif count > 0
let wrongMethods = ThirdParty.Methods.WithFullNameIn(
"System.Threading.Thread.Abort()",
"System.Threading.Thread.Abort(Object)",
"System.Threading.Thread.Sleep(Int32)",
"System.Threading.Thread.Suspend()",
"System.Threading.Thread.Resume()")
from m in Application.Methods.UsingAny(wrongMethods)
select new {
m,
suppressCallsTo = m.MethodsCalled.Intersect(wrongMethods),
Debt = 30.ToMinutes().ToDebt(),
Severity = Severity.High
}
//<Expl>
// The *{0}* shouldn't call the *{1}*.
// Use instead facilities offered by the *Task Parallel Library (TPL)*.
//</Expl>
//<Desc>
// Warns about *Thread.Abort()*, *Sleep()*, *Suspend()* and *Resume()*:
//
// - *Thread.Abort()* is dangerous:
// http://www.interact-sw.co.uk/iangblog/2004/11/12/cancellation
//
// - *Thread.Sleep()* is a sign of flawed design:
// http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx
//
// - *Suspend()* and *Resume()* are dangerous and obsolete; workaround:
// http://stackoverflow.com/questions/382173/what-are-alternative-ways-to-suspend-and-resume-a-thread
//</Desc>
//<HowToFix>
// Suppress the calls listed in *suppressCallsTo*; use the **Task Parallel
// Library** *(TPL)* instead:
// https://msdn.microsoft.com/en-us/library/dd460717(v=vs.110).aspx
//</HowToFix>
Open in the interactive Rules Explorer · All default rules
.NET Framework Usage / System.Threading: all 6 items
- Don't create threads explicitly ND2500
- Don't use dangerous threading methods ND2501
- Monitor TryEnter/Exit must be both called within the same method ND2502
- ReaderWriterLock AcquireLock/ReleaseLock must be both called within the same method ND2503
- Don't tag instance fields with ThreadStaticAttribute ND2504
- Method non-synchronized that read mutable states ND2505