2

Currently i have a problem that this piece of code gives me a task that is always on waiting for activation status.

public static void GetAvailablePorts(List<string> ports, int timeOut)
    {
        var selector = SerialDevice.GetDeviceSelector();
        var task = DeviceInformation.FindAllAsync(selector).AsTask();
        Int64 i = 0;
        bool done = false;
        while (!done)
        {
            Debug.WriteLine(String.Format("Index: {0}, State: {1}, Id: {2}", i, task.Status.ToString(), task.Id));
            i++;
            if(TaskStatus.RanToCompletion == task.Status)
                done = true;
        }

        var devices = task.Result;
        foreach (var d in devices)
        {
            ports.Add(d.Id);
        }
    }

Here are some debug information while running this piece on my end. Does anyone know what the problem is.

In debug output(the end):

 Index: 7496, State: WaitingForActivation, Id: 21
 Index: 7497, State: WaitingForActivation, Id: 21
 Index: 7498, State: WaitingForActivation, Id: 21
 Index: 7499, State: WaitingForActivation, Id: 21
 Index: 7500, State: WaitingForActivation, Id: 21
 Index: 7501, State: WaitingForActivation, Id: 21
 Index: 7502, State: WaitingForActivation, Id: 21
 Index: 7503, State: WaitingForActivation, Id: 21

Task window of a different run but should serve the same purpose: Task Window

Update:

If i do this it doesn't go into the continueWith 90% of the time. Bit weird.

var selector = SerialDevice.GetDeviceSelector();
        return DeviceInformation.FindAllAsync(selector).AsTask().ContinueWith((Task<DeviceInformationCollection> previous) =>
        {
            var devices = previous.Result;
            foreach (var d in devices)
            {
                ports.Add(d.Id);
            }
            string b = "";
        });

update 2:

After some time. 15 min? i get this error back:

A COM call (IID: {45180254-082E-5274-B2E7-AC0517F44D07}, method index: 8) to an ASTA (thread 9164) appears deadlocked and was timed out.)

Anyone know what is going on?

jermey
  • 179
  • 2
  • 10
  • 1
    You should start the task. – Hamid Pourjam Oct 21 '16 at 11:08
  • If you are running on a single thread while (!done) will block that thread and nothing will be able to change RanToCompletion status. You should not do this use await or .Continue for async programing – Filip Cordas Oct 21 '16 at 11:10
  • Its a promise style task. shouldn't that task not start automically? – jermey Oct 21 '16 at 11:10
  • if i use await it will just hang so i refactored my code this way to see what is going on. – jermey Oct 21 '16 at 11:16
  • I think you have some deadlock inside FindAllAsync method. – YuvShap Oct 21 '16 at 11:18
  • If you press Ctrl+Shift+D in Visual Studio you will get task debug window so you can see how many pending tasks do you have most likely it's a deadlock but could be a bug with the device you are testing on try on a different device. – Filip Cordas Oct 21 '16 at 11:26
  • Filip Cordas on my last piece of my post i gave you a image of the task window. – jermey Oct 21 '16 at 11:30

1 Answers1

0

You will need some changes in your code.

Instead of public static void GetAvailablePorts you should write public async Task GetAvailablePorts

Then later on instead of var devices = task.Result; you should write var devices = await task;

A good reference to understand the async / await pattern can be found here: https://markheath.net/post/async-antipatterns

jfmg
  • 2,626
  • 1
  • 24
  • 32