public void TestMethod1() {
object _lock = new object();
Action a1 = () => {
if ( Monitor.TryEnter( _lock ) ) {
try {
for ( int i = 0; i < 10; i++ ) {
Debug.Print( "a1\t" + i );
}
} catch ( Exception ex ) {
Debug.Print( " a1 occur error : " + ex.Message );
} finally {
Monitor.Exit( _lock );
}
}
};
Action a2 = () => {
if ( Monitor.TryEnter( _lock ) ) {
try {
for ( int i = 0; i < 10; i++ ) {
Debug.Print( "a2\t" + i );
}
} catch ( Exception ex ) {
Debug.Print( "a2 occur error : " + ex.Message );
} finally {
Monitor.Exit( _lock );
}
}
};
Parallel.Invoke( a1, a2 );
}