Preforming a block of code only once
问题 The following code block, preforms loading of an object in C#. public bool IsModelLoaded { get; set; } public override MyObject Load() { if (!IsModelLoaded) { Model = MyService.LoadMyObject(Model); IsModelLoaded = true; } return Model; } My intention is to run this block only once, and hence loading the Model only once. Nevertheless, this code block runs twice from 2 different threads.. How can I make sure that this block runs only once? (on multiplethreads). Thank You. 回答1: Simplest would be