c#中的事件注册范围

本文关键字:注册 范围 事件 | 更新日期: 2023-09-27 18:17:55

是否可能由于GC释放资源而不会调用'已完成'事件处理程序?

public void StartVideo(WerCamera camera)
    {
        Credential creadential = new Credential() { Email = CurrentUser.Email, Password = CurrentUser.Password, SessionNumber = SessionNumber};
        CommandsClient client = new CommandsClient();
        client.StartVideoCompleted += client_StartVideoCompleted;
        client.StartVideoAsync(int.Parse(camera.Id), creadential, ClientInfo);
        client.CloseAsync();
    }

c#中的事件注册范围

是的,这是可能的,因为一旦函数返回,您将失去对客户机的所有引用。

client.StartVideoCompleted += client_StartVideoCompleted;最终持有对client_StartVideoCompleted函数的对象的引用,但这不是互惠的。您需要以某种方式保留对已创建客户端的引用。