public static void TestWeakEvent() { object a = new object(); bool w1Executed = false; int val = 0; WeakCallback <int> w1 = new WeakCallback <object, int>(a, (A, i) => { w1Executed = true; val = i; }); w1.Execute(1); Assert.True(w1Executed); Assert.AreEqual(1, val); w1Executed = false; a = null; GC.Collect(); GC.WaitForPendingFinalizers(); w1.Execute(2); Assert.AreEqual(1, val); Assert.False(w1Executed); object b = new object(); bool w2Executed = false; WeakCallback <int> w2 = new WeakCallback <object, int>(b, (A, i) => { w2Executed = true; val = i; }); WeakEvent <int> e = new WeakEvent <int>(); e.Register(w1); e.Register(w2); e.Execute(3); Assert.True(w2Executed); }
private static void ExecuteCallback(WeakCallback callback, object processResponse) { if (callback == null) { return; } if (callback.IsAlive) { callback.FireCallback(processResponse); } }
/// <summary> /// Initializes a new instance of the <see cref="NetworkProcessCallbackContainer"/> class. /// </summary> /// <param name="networkProcess"> /// The network process. /// </param> /// <param name="callback"> /// The success callback. /// </param> /// <param name="errorCallback"> /// The error callback. /// </param> public NetworkProcessCallbackContainer( NetworkProcess networkProcess, WeakCallback callback, WeakCallback errorCallback) { if (networkProcess == null) { throw new ArgumentNullException(nameof(networkProcess)); } this.NetworkProcess = networkProcess; this.Callback = callback; this.ErrorCallback = errorCallback; }
public ProjectMenuController(SharedContext context, ConfigParameterList <string> config, INodeFactory conversationNodeFactory, INodeFactory domainNodeFactory, Action <Action> executeInGuiThread, PluginsConfig pluginsConfig, Func <IAudioProviderCustomization> audioCustomization, UpToDateFile.BackEnd backend) { m_context = context; m_executeInGuiThread = executeInGuiThread; m_conversationNodeFactory = conversationNodeFactory; m_domainNodeFactory = domainNodeFactory; m_config = config; m_pluginsConfig = pluginsConfig; m_audioCustomization = audioCustomization; m_backend = backend; m_context.ProjectMoved += WeakCallback <Changed <FileInfo> > .Handler(this, (me, a) => me.ProjectMoved(a.From, a.To)); m_context.CurrentProject.Changed.Register(this, (a, b) => UpdateRecentlyOpenedConfig()); var file = m_config.Value.FirstOrDefault(a => true, a => a, ""); if (File.Exists(file)) { OpenProject(file); } }
/// <summary> /// Adds a network process to the specified priority queue. /// </summary> /// <typeparam name="TProcess"> /// The network process type. /// </typeparam> /// <typeparam name="TResponse"> /// The expected response type. /// </typeparam> /// <typeparam name="TErrorResponse"> /// The expected errored response type. /// </typeparam> /// <param name="process"> /// The network process to execute. /// </param> /// <param name="callback"> /// The action to execute when a response is required. /// </param> /// <param name="errorCallback"> /// The action to execute if the response errors. /// </param> /// <param name="priority"> /// The priority of the process /// </param> public void AddProcess <TProcess, TResponse, TErrorResponse>( TProcess process, Action <TResponse> callback, Action <TErrorResponse> errorCallback, NetworkProcessPriority priority) where TProcess : NetworkProcess { var weakCallback = new WeakCallback(); weakCallback.SetCallback <TResponse>(callback); var weakErrorCallback = new WeakCallback(); weakErrorCallback.SetCallback <TErrorResponse>(errorCallback); var callbackContainer = new NetworkProcessCallbackContainer(process, weakCallback, weakErrorCallback); var queue = this.GetQueueByPriority(priority); queue.AddOrUpdate( callbackContainer.NetworkProcess.QueueId, callbackContainer, (key, val) => callbackContainer); }
/// <summary> /// Initializes a new instance of the <see cref="NetworkProcessCallbackContainer"/> class. /// </summary> /// <param name="networkProcess"> /// The network process. /// </param> /// <param name="callback"> /// The success callback. /// </param> public NetworkProcessCallbackContainer(NetworkProcess networkProcess, WeakCallback callback) : this(networkProcess, callback, null) { }