implement it thread-safe

This commit is contained in:
Gregor Biswanger
2017-11-04 00:16:14 +01:00
parent 0ad6358d16
commit 7c13d19e7f
12 changed files with 108 additions and 16 deletions

View File

@@ -296,7 +296,13 @@ namespace ElectronNET.API
{
if (_app == null)
{
_app = new App();
lock (_syncRoot)
{
if(_app == null)
{
_app = new App();
}
}
}
return _app;
@@ -304,6 +310,7 @@ namespace ElectronNET.API
}
private static App _app;
private static object _syncRoot = new Object();
private JsonSerializer _jsonSerializer = new JsonSerializer()
{

View File

@@ -6,6 +6,7 @@ namespace ElectronNET.API
internal static class BridgeConnector
{
private static Socket _socket;
private static object _syncRoot = new Object();
public static Socket Socket
{
@@ -13,15 +14,27 @@ namespace ElectronNET.API
{
if(_socket == null && HybridSupport.IsElectronActive)
{
_socket = IO.Socket("http://localhost:" + BridgeSettings.SocketPort);
_socket.On(Socket.EVENT_CONNECT, () =>
lock (_syncRoot)
{
Console.WriteLine("BridgeConnector connected!");
});
if (_socket == null && HybridSupport.IsElectronActive)
{
_socket = IO.Socket("http://localhost:" + BridgeSettings.SocketPort);
_socket.On(Socket.EVENT_CONNECT, () =>
{
Console.WriteLine("BridgeConnector connected!");
});
}
}
}
else if(_socket == null && !HybridSupport.IsElectronActive)
{
_socket = IO.Socket(new Uri("http://localhost"), new IO.Options { AutoConnect = false });
lock (_syncRoot)
{
if (_socket == null && !HybridSupport.IsElectronActive)
{
_socket = IO.Socket(new Uri("http://localhost"), new IO.Options { AutoConnect = false });
}
}
}
return _socket;

View File

@@ -2,6 +2,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System;
using System.Threading.Tasks;
namespace ElectronNET.API
@@ -12,6 +13,7 @@ namespace ElectronNET.API
public sealed class Clipboard
{
private static Clipboard _clipboard;
private static object _syncRoot = new Object();
internal Clipboard() { }
@@ -21,7 +23,13 @@ namespace ElectronNET.API
{
if (_clipboard == null)
{
_clipboard = new Clipboard();
lock (_syncRoot)
{
if (_clipboard == null)
{
_clipboard = new Clipboard();
}
}
}
return _clipboard;

View File

@@ -13,6 +13,7 @@ namespace ElectronNET.API
public sealed class Dialog
{
private static Dialog _dialog;
private static object _syncRoot = new Object();
internal Dialog() { }
@@ -22,7 +23,13 @@ namespace ElectronNET.API
{
if (_dialog == null)
{
_dialog = new Dialog();
lock (_syncRoot)
{
if(_dialog == null)
{
_dialog = new Dialog();
}
}
}
return _dialog;

View File

@@ -10,6 +10,7 @@ namespace ElectronNET.API
public sealed class GlobalShortcut
{
private static GlobalShortcut _globalShortcut;
private static object _syncRoot = new Object();
internal GlobalShortcut() { }
@@ -19,7 +20,13 @@ namespace ElectronNET.API
{
if (_globalShortcut == null)
{
_globalShortcut = new GlobalShortcut();
lock (_syncRoot)
{
if (_globalShortcut == null)
{
_globalShortcut = new GlobalShortcut();
}
}
}
return _globalShortcut;

View File

@@ -14,6 +14,7 @@ namespace ElectronNET.API
public sealed class IpcMain
{
private static IpcMain _ipcMain;
private static object _syncRoot = new Object();
internal IpcMain() { }
@@ -23,7 +24,13 @@ namespace ElectronNET.API
{
if(_ipcMain == null)
{
_ipcMain = new IpcMain();
lock (_syncRoot)
{
if(_ipcMain == null)
{
_ipcMain = new IpcMain();
}
}
}
return _ipcMain;

View File

@@ -16,6 +16,7 @@ namespace ElectronNET.API
public sealed class Menu
{
private static Menu _menu;
private static object _syncRoot = new Object();
internal Menu() { }
@@ -25,7 +26,13 @@ namespace ElectronNET.API
{
if (_menu == null)
{
_menu = new Menu();
lock (_syncRoot)
{
if(_menu == null)
{
_menu = new Menu();
}
}
}
return _menu;

View File

@@ -15,6 +15,7 @@ namespace ElectronNET.API
public sealed class Notification
{
private static Notification _notification;
private static object _syncRoot = new Object();
internal Notification() { }
@@ -24,7 +25,13 @@ namespace ElectronNET.API
{
if (_notification == null)
{
_notification = new Notification();
lock (_syncRoot)
{
if (_notification == null)
{
_notification = new Notification();
}
}
}
return _notification;

View File

@@ -105,6 +105,7 @@ namespace ElectronNET.API
private event Action<Display, string[]> _onDisplayMetricsChanged;
private static Screen _screen;
private static object _syncRoot = new Object();
internal Screen() { }
@@ -114,7 +115,13 @@ namespace ElectronNET.API
{
if (_screen == null)
{
_screen = new Screen();
lock (_syncRoot)
{
if (_screen == null)
{
_screen = new Screen();
}
}
}
return _screen;

View File

@@ -14,6 +14,7 @@ namespace ElectronNET.API
public sealed class Shell
{
private static Shell _shell;
private static object _syncRoot = new Object();
internal Shell() { }
@@ -23,7 +24,13 @@ namespace ElectronNET.API
{
if (_shell == null)
{
_shell = new Shell();
lock (_syncRoot)
{
if (_shell == null)
{
_shell = new Shell();
}
}
}
return _shell;

View File

@@ -201,6 +201,7 @@ namespace ElectronNET.API
// TODO: Implement macOS Events
private static Tray _tray;
private static object _syncRoot = new Object();
internal Tray() { }
@@ -210,7 +211,13 @@ namespace ElectronNET.API
{
if (_tray == null)
{
_tray = new Tray();
lock (_syncRoot)
{
if (_tray == null)
{
_tray = new Tray();
}
}
}
return _tray;

View File

@@ -2,6 +2,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@@ -14,6 +15,7 @@ namespace ElectronNET.API
public sealed class WindowManager
{
private static WindowManager _windowManager;
private static object _syncRoot = new Object();
internal WindowManager() { }
@@ -23,7 +25,13 @@ namespace ElectronNET.API
{
if (_windowManager == null)
{
_windowManager = new WindowManager();
lock (_syncRoot)
{
if (_windowManager == null)
{
_windowManager = new WindowManager();
}
}
}
return _windowManager;