public static Exception TryInitialize(string dbusAddress = null) { var oldContext = SynchronizationContext.Current; try { var dbusContext = new DBusSyncContext(); SynchronizationContext.SetSynchronizationContext(dbusContext); var conn = new Connection(new ClientConnectionOptions(dbusAddress ?? Address.Session) { AutoConnect = false, SynchronizationContext = dbusContext }); // Connect synchronously conn.ConnectAsync().Wait(); // Initialize a brand new sync-context dbusContext.Initialized(); Connection = conn; } catch (Exception e) { return(e); } finally { SynchronizationContext.SetSynchronizationContext(oldContext); } return(null); }
public static Connection TryGetConnection(string dbusAddress = null) { var oldContext = SynchronizationContext.Current; try { var dbusContext = new DBusSyncContext(); SynchronizationContext.SetSynchronizationContext(dbusContext); var conn = new Connection(new ClientConnectionOptions(dbusAddress ?? Address.Session) { AutoConnect = false, SynchronizationContext = dbusContext }); // Connect synchronously conn.ConnectAsync().Wait(); // Initialize a brand new sync-context dbusContext.Initialized(); Connection = conn; } catch (Exception e) { Logger.TryGet(LogEventLevel.Error, "DBUS") ?.Log(null, "Unable to connect to DBus: " + e); } finally { SynchronizationContext.SetSynchronizationContext(oldContext); } return(Connection); }