Click here to Skip to main content
15,885,757 members
Home / Discussions / C#
   

C#

 
GeneralRe: Thread safety Pin
TimK3-Sep-03 17:56
TimK3-Sep-03 17:56 
GeneralRe: Thread safety Pin
Meysam Mahfouzi3-Sep-03 18:35
Meysam Mahfouzi3-Sep-03 18:35 
GeneralRe: Thread safety Pin
TimK3-Sep-03 19:00
TimK3-Sep-03 19:00 
GeneralRe: Thread safety Pin
scadaguy4-Sep-03 6:57
scadaguy4-Sep-03 6:57 
GeneralRe: Thread safety Pin
TimK4-Sep-03 13:30
TimK4-Sep-03 13:30 
GeneralRe: Thread safety Pin
scadaguy4-Sep-03 7:28
scadaguy4-Sep-03 7:28 
GeneralRe: Thread safety Pin
TimK4-Sep-03 13:19
TimK4-Sep-03 13:19 
GeneralAnd also consider this: Pin
Meysam Mahfouzi4-Sep-03 17:44
Meysam Mahfouzi4-Sep-03 17:44 
Hummmm, but because of one reason, you may need to use lock in your first example.
Consider below situation:

A thread(say thread_A) reaches here:

if (<code>Event </code>!= null){
      <code>Event</code>(<code>this</code>, <code>e</code>);    
   }


thread_A checks and sees that Event is not null, and goes to fire Event(this, e). But before calling it, its time slice terminates. Now suppose that in our main application, by using -= operator the Event gets null. Now it's thread_A's turn to continue. But an exception will be fired by calling Event(this, e). This will not happen be using below code:

lock(this){
if (<code>Event </code>!= null){
      <code>Event</code>(this, e);    
   }
}


I am wondered why below code doesn't lock the Event and the exception would occur:

lock(<code>Event</code>){
if (<code>Event </code>!= null){
      <code>Event</code>(this, e);    
   }
}





Don't forget, that's
Persian Gulf
not Arabian gulf!
GeneralRe: And also consider this: Pin
TimK4-Sep-03 18:11
TimK4-Sep-03 18:11 
GeneralRe: And also consider this: Pin
Meysam Mahfouzi4-Sep-03 18:22
Meysam Mahfouzi4-Sep-03 18:22 
GeneralRe: And also consider this: Pin
TimK4-Sep-03 18:30
TimK4-Sep-03 18:30 
GeneralRe: And also consider this: Pin
Meysam Mahfouzi5-Sep-03 17:15
Meysam Mahfouzi5-Sep-03 17:15 
GeneralRe: And also consider this: Pin
scadaguy5-Sep-03 4:25
scadaguy5-Sep-03 4:25 
GeneralRe: And also consider this: Pin
Meysam Mahfouzi5-Sep-03 17:25
Meysam Mahfouzi5-Sep-03 17:25 
GeneralRe: And also consider this: Pin
scadaguy5-Sep-03 4:28
scadaguy5-Sep-03 4:28 
GeneralRe: And also consider this: Pin
scadaguy5-Sep-03 4:22
scadaguy5-Sep-03 4:22 
GeneralDataGrid alterations... Pin
james-cxx3-Sep-03 13:11
james-cxx3-Sep-03 13:11 
GeneralRe: DataGrid alterations... Pin
A.Wegierski3-Sep-03 19:21
A.Wegierski3-Sep-03 19:21 
GeneralRe: DataGrid alterations... Pin
Braulio Dez3-Sep-03 23:58
Braulio Dez3-Sep-03 23:58 
GeneralRe: DataGrid alterations... Pin
james-cxx4-Sep-03 7:10
james-cxx4-Sep-03 7:10 
GeneralListViews: Sort ascending/descending Pin
tsigo3-Sep-03 10:36
tsigo3-Sep-03 10:36 
GeneralRe: ListViews: Sort ascending/descending Pin
Furty3-Sep-03 20:08
Furty3-Sep-03 20:08 
GeneralRe: ListViews: Sort ascending/descending Pin
tsigo6-Sep-03 11:00
tsigo6-Sep-03 11:00 
GeneralObtaining Caller of a Method in C# Pin
TheLastBastion3-Sep-03 9:21
TheLastBastion3-Sep-03 9:21 
GeneralRe: Obtaining Caller of a Method in C# Pin
leppie3-Sep-03 10:16
leppie3-Sep-03 10:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.