/// <summary>
        /// Set the device status.
        /// </summary>
        /// <param name="state">the state</param>
        /// <param name="statusText">status text</param>
        public void SetDeviceState(DeviceState state, string statusText = null)
        {
            if (deviceControl == null || deviceControl.IsDisposed)
            {
                return;
            }

            if (deviceControl.InvokeRequired)
            {
                if (!deviceControl.IsDisposed)
                {
                    try
                    {
                        SetDeviceStateCallback callback = new SetDeviceStateCallback(SetDeviceState);
                        deviceControl?.Invoke(callback, new object[] { state, statusText });
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"SetDeviceState: {ex.Message}");
                    }
                }
            }
            else
            {
                // Restart when recovering from a connection error.
                if (state != DeviceState.ConnectError && wasPlayingWhenConnectError)
                {
                    wasPlayingWhenConnectError = false;
                    if (state != DeviceState.Playing)
                    {
                        Task.Run(() => {
                            OnClickPlayPause();
                        });
                    }
                }
                else if (state == DeviceState.ConnectError && deviceState == DeviceState.Playing)
                {
                    wasPlayingWhenConnectError = true;
                }

                if (state == DeviceState.ConnectError && IsGroup())
                {
                    deviceState = DeviceState.Disposed;
                    deviceControl?.Dispose();
                    menuItem?.Dispose();
                }
                else
                {
                    deviceState = state;
                    deviceControl?.SetStatus(state, statusText);
                    if (deviceControl != null)
                    {
                        deviceControl.Visible = true;
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Set the device status.
        /// </summary>
        /// <param name="state">the state</param>
        /// <param name="statusText">status text</param>
        public void SetDeviceState(DeviceState state, string statusText = null)
        {
            if (deviceControl == null || deviceControl.IsDisposed || discoveredDevice == null)
            {
                return;
            }

            if (deviceControl.InvokeRequired)
            {
                if (!deviceControl.IsDisposed)
                {
                    try
                    {
                        SetDeviceStateCallback callback = new SetDeviceStateCallback(SetDeviceState);
                        deviceControl?.Invoke(callback, new object[] { state, statusText });
                    }
                    catch (Exception ex)
                    {
                        logger.Log(ex, "Device.SetDeviceState");
                    }
                }
            }
            else
            {
                // Restart when recovering from a connection error.
                if (state != DeviceState.ConnectError && wasPlayingWhenConnectError)
                {
                    wasPlayingWhenConnectError = false;
                    ResumePlaying();
                }
                else if (state == DeviceState.ConnectError && GetDeviceState() == DeviceState.Playing)
                {
                    wasPlayingWhenConnectError = true;
                }

                DoFirewallCheckSaveTimes(state);

                discoveredDevice.DeviceState = state;
                deviceControl?.SetStatus(discoveredDevice.DeviceState, statusText);
            }
        }