Adding Overlays using Task and Lambda Expressions

Hello,
I have these tasks just to call the methods and make the program faster. Those methods work perfectly when they are called out of the Task. They add different overlays to my display images (I have 5 displays). I have just added some console.writelines to check where the program stops working.

Task task1 = Task.Run(() =>
            { 
                    Console.WriteLine("Pre Task1");  
                    LeftCamera(); 
                    Console.WriteLine("Task1"); 
            });

            Task task2 = Task.Run(() => { Console.WriteLine("Pre Task2"); RightCamera(); Console.WriteLine("Task2"); });
            Task.WaitAll(task1, task2);

            Task task5 = Task.Run(() => { Console.WriteLine("Pre Task5"); ChimneyHeightCalc(); Console.WriteLine("Task5"); });
            Task task6 = Task.Run(() => { Console.WriteLine("Pre Task6"); ThreadWidthCalc(); Console.WriteLine("Task6"); });

            Task.WaitAll(task5, task6);

            Task task3 = Task.Run(() => { Console.WriteLine("Pre Task3"); BoreEdgeAndLabel(); Console.WriteLine("Task3"); });
            Task task4 = Task.Run(() => { Console.WriteLine("Pre Task4"); CompleteHandle(); Console.WriteLine("Task4"); });

            Console.WriteLine("Task methods DONE!");
            Console.WriteLine(watch.ElapsedMilliseconds);
            watch.Stop();
            watch.Reset();

The program just stops working (hang up) when any of the methods is going to add an overlay, for example here:

if (ChkCrossHairs.Checked)
            {
                Console.WriteLine("LC 5970");
                Display1.Overlays.Add(new CrossHairOverlay("", false, Color.WhiteSmoke, false, new Point(Display1.Image.Width / 2, Display1.Image.Height / 2), new Size(Display1.Image.Width, Display1.Image.Height))); 
            }

Do you have any idea of what is going on here?
Thank you!

From what you describe this sounds like a C# problem rather than a CVB issue.

I cant tell from your code, where exactly the Overlays are added thus I can not tell you where to look for the problem but I can provide you with the following link:

Using Async, Await and Task

I suspect there might be a missing await or something similar in your code.

Cheers
Chris

You might also find this topic helpful:

https://stackoverflow.com/questions/33591659/task-freezes-the-gui

Hi!

From my experience with Windows Forms any operation on the user interface from a thread that is not the application’s UI thread will deadlock the application (or, more strictly speaking, the UI thread). Most controls have a property called InvokeRequired whose actual value depends on whether you query it from the UI thread (false) or from a non-UI thread (true).

I would suggest you try and move the Display1.Overlay.Add into a Lambda that you pass to the Display.BeginInvoke method - if the thread context was the issue then you should see no more deadlock.

@Chris a properly placed await would have a similar effect if it brings the thread context of the Add call back to the UI thread.

1 Like

Hello Chris, thank you.
By the way It’s not a C# problem because the methods work perfectly and the Task are just calling those methods with a simple lambda expression. No async methods so no await. I think is more like Illusive is suggesting…something odd is happening with the UI.
Thank you again Chris!

And awaiting your tasks might get them back to the correct thread context :wink:

Hello Illusive,
I will try that, this is very strange!
Task stops my UI adding overlays and Parallel.Invoke lambda exactly the same thing. It is strange because yesterday I wrote a little program with Task lambda calling 5 methods and it worked perfectly!
Thank you very much guys I will try to solve it

1 Like

Let us know if you found the problem.

1 Like