Invalid object reference when another thread executes a method on it (.NET)

(I'm interested in the .NET CLR)

What exactly happens when one thread modifies an object that refers to a variable while another thread executes a method on the original object?

For example, let's say that the type Foohas a variable of the “Bar” type Bar, and Bardoes not have state at the class level (at least for now, I would like this script to be simple):

// Thread 1
foo.Bar = new Bar();
foo.Bar.SomeMethod();

Meanwhile, before that, execution ends ...

// Thread 2
foo.Bar = new Bar();

What happens to a method that is executing (was?) Executing in Thread 1? Is everything you need to complete already on the stack?

Has this changed if, instead, in this case 2?

foo.Bar = null;
+3
3

SomeMethod(), - this - , ( , , ) .

, .

+3

- : .

, , , : ", !" .SomeMethod()

, foo.Bar.SomeMethod() , Thread 2 () null; ( null ref).

; .SomeMethod .

, , , .SomeMethod , ? . . - .

+1

, , , ?

, , , .

-1

Source: https://habr.com/ru/post/1715376/


All Articles