Page 1 of 1

C# Parallelism on Raspberry Pi doesn't work!

Posted: Tue Jul 21, 2020 11:48 pm
by G44Gonzalo
I'm making a Discord Bot and I need to use C# Task Parallelism.

I develop the program in Windows 10, and then I build it to the Raspberry Pi.
All works correctly, but the Task Parallelism.

As you can see in the image, all is working properly, with one difference: "15 second test started".
That is not running on the right side (Raspberry Pi one).
Image

Function that runs in parallel: (Simplified)

Code: Select all

        private async Task Update()
        {
            while (!connected) {

            }
            while (connected) {
                    if (connected)
                    {
                        await Task.Delay(15000).ConfigureAwait(false);
                        Console.WriteLine("15 Second test started!");
                    }
            }
        }
        
Code that trigger it to start running:

Code: Select all

            _ = Task.Run(() => Update());
            //continue doing other things
            
The code is right, I only show it in case it can help you.

I don't know why, in the Raspberry Pi, it is like ignored.
Any ideas? Thank you :)

Re: C# Parallelism on Raspberry Pi doesn't work!

Posted: Wed Aug 19, 2020 6:43 pm
by ProjectMayu
Task.Run often gets stuck on the PI

Have you tried the following

Code: Select all

        private async Task<bool> Update()
        {
            TaskCompletionSource<bool> result = new TaskCompletionSource<bool>();
            System.Threading.ThreadPool.QueueUserWorkItem(async delegate {
                try
                {
                    await Task.Delay(1000); //Do work
                    result.SetResult(true);
                } catch (Exception ex)
                {
                    result.SetException(ex); //Capture and pass on errors
                }
            });
            return result.Task.Result;
        }