private static void InitializeMessage() { if (null == outputMessageFunction) { lock (outputMessageFunctionLock) { if (null == outputMessageFunction) { FreeImage.ValidateAvailability(); try { // Create a delegate (function pointer) to 'OnMessage' outputMessageFunction = delegate(FREE_IMAGE_FORMAT fif, string message) { LastErrorMessage = message; // Get a local copy of the multicast-delegate OutputMessageFunction m = FreeImageEngine.message; // Check the local copy instead of the static instance // to prevent a second thread from setting the delegate // to null, which would cause a nullreference exception if (m != null) { // Invoke the multicast-delegate m.Invoke(fif, message); } }; // Set the callback FreeImage.SetOutputMessage(outputMessageFunction); } catch { outputMessageFunction = null; throw; } } } } }