0

I am trying to set up a system preventing my application from working if the USB cable of the device is disconnected. For that, if the person activates a function, instead of crashing the application, I bring the user to a loading screen while waiting for the cable to be reconnected.

I have already done a lot of research without actually finding a solution to correct this error and allow me to make it work as I wish. Here is my code:

public async Task WaitingPort()
{
    var controllerPort = await this.ShowProgressAsync("Seeking device", " "); //Warning starts here
    controllerPort.SetIndeterminate();
    await Task.Run(() =>
    {
        while (port is null)
        {
            port = CheckPort();
            controllerPort.SetMessage("please plub USB");
        }
    });
    await controllerPort.CloseAsync();
}

private SerialPort CheckPort()
{
    string[] listPort = SerialPort.GetPortNames();
    foreach(string namePort in listPort)
    {
        SerialPort port = new SerialPort(namePort, 9600);
        if (!port.IsOpen)
        {
            try
            {
                port.Open();
                port.ReadTimeout = 1500;
                string data = port.Readline();
                if (data.Substring(0, 1) == "A")
                {
                    port.ReadTimeout = 200;
                    port.Write("777"); // to make it stop sending "A"
                    return port;
                }
                else
                {
                port.Close();
                }
            }
            catch (Exception e1)
            {
                port.Close();
            }
        }
    }
    return null;
}

public MainWindow()
{
    WaitingPort(); //WARNING CS4014
    ...
}

private async void Button_ClickOpen(object sender, RoutedEventArgs e)
{
    var controllerPort = await this.ShowProgressAsync("Please wait", " ");
    controllerPort.SetIndeterminate();
    await Task.Run(() =>
    {
        try
        {
            blabla
        }
        catch (Exception)
        {
            WaitingPort(); //WARNING CS4014
        }
    }
}

I've already tried multiple solutions... :

Turning my Void into a Task

_ = WaitingPort(); // hide the error without solving it

WaitingPort().Wait(); // same case here, hiding but not solving

await MyAsyncMethod().ConfigureAwait(false); // same case here, hiding but not solving

Currently, the WaitingPort method only works at startup, otherwise it issues a warning.

I AM NOT TRYING TO HIDE, IGNORE, SUPRESS or whatever THE WARNING like every solutions on stack, but to resolve it, to make it work. Have you guys any idea ?

I really would like my ShowProgressAsync to work properly when called by another methods which are not async

I am not yet very experienced in C#, maybe I'm missing some fundamentals?

Vynz
  • 83
  • 1
  • 13
  • You are eating up your exceptions. Hinders you in finding issues. – Uwe Keim Mar 04 '20 at 07:35
  • What do you mean ? – Vynz Mar 04 '20 at 07:38
  • Do not catch exceptions and ignore the exception information. Better do not catch exceptions at all. – Uwe Keim Mar 04 '20 at 07:38
  • Does this answer your question? [Suppressing "warning CS4014: Because this call is not awaited, execution of the current method continues..."](https://stackoverflow.com/questions/22629951/suppressing-warning-cs4014-because-this-call-is-not-awaited-execution-of-the) – raidensan Mar 04 '20 at 07:41
  • Basically you tell me that I have every interest in hard coding the case where the USB cable is disconnected without using try/catch ? – Vynz Mar 04 '20 at 07:42
  • No @raidensan because as it says, he's asking to supress the warning, I'm not, I need to make it work – Vynz Mar 04 '20 at 07:43
  • What is the problem here? You call `WaitingPort` in catch. To avoid the warning just `await` it. Make the lambda `async () =>` and await the call. Why is this _hiding but not solving_? The warning in ctor is an initialization problem. [Stephen Cleary](https://blog.stephencleary.com/2013/01/async-oop-2-constructors.html) has a very detailed explanation of _how to solve it_. – Sebastian Schumann Mar 04 '20 at 08:55
  • [MSDN](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions#async-lambdas) has a good explanation and sample code of _async lambdas_. – Sebastian Schumann Mar 04 '20 at 08:59
  • Warnings are just that, warnings! It doesn't mean your code won't compile... unless you enabled treat warnings as errors – phuzi Mar 04 '20 at 09:07

1 Answers1

1

I would use some code like this to solve both problems:

public MainWindow()
{
    this.Initialization = this.WaitingPort();
    ...
}

public Task Initialization { get; }

private async void Button_ClickOpen(object sender, RoutedEventArgs e)
{
    await this.Initialization; // Do this in every public method and non-public if they're called out of an 'event'

    var controllerPort = await this.ShowProgressAsync("Please wait", " ");
    controllerPort.SetIndeterminate();
    await Task.Run(async () =>
    {
        try
        {
            blabla
        }
        catch (Exception)
        {
            await WaitingPort();
        }
    }
}

This is the way that Stephen Cleary propagates.

Sebastian Schumann
  • 3,204
  • 19
  • 37
  • now I've got a cross-thread error but this is another problem. Thanks for your help! – Vynz Mar 04 '20 at 10:49