# Adding Overlays using Task and Lambda Expressions

**URL:** <https://forum.commonvisionblox.com/t/adding-overlays-using-task-and-lambda-expressions/1592>\
**Category:** Programming Questions\
**Tags:** csharp\
**Created:** [January 26, 2022, 10:52am UTC](https://forum.commonvisionblox.com/t/adding-overlays-using-task-and-lambda-expressions/1592 "2022-01-26T10:52:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![George](https://avatars.discourse-cdn.com/v4/letter/g/71e660/32.png) [@George](https://forum.commonvisionblox.com/u/George)\
**Post date:** [January 26, 2022, 10:52am UTC](https://forum.commonvisionblox.com/t/adding-overlays-using-task-and-lambda-expressions/1592/1 "2022-01-26T10:52:01Z")

</div>

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.

```auto
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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![Chris](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/chris/32/1154_2.png) [@Chris](https://forum.commonvisionblox.com/u/Chris)\
**Post date:** [January 26, 2022, 2:17pm UTC](https://forum.commonvisionblox.com/t/adding-overlays-using-task-and-lambda-expressions/1592/2 "2022-01-26T14:17:41Z")

</div>

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](https://grantwinney.com/using-async-await-and-task-to-keep-the-winforms-ui-more-responsive/)

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

Cheers  
Chris

---

<div class="post-metadata">

**Author:** ![Chris](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/chris/32/1154_2.png) [@Chris](https://forum.commonvisionblox.com/u/Chris)\
**Post date:** [January 26, 2022, 2:18pm UTC](https://forum.commonvisionblox.com/t/adding-overlays-using-task-and-lambda-expressions/1592/3 "2022-01-26T14:18:43Z")

</div>

You might also find this topic helpful:

[https://stackoverflow.com/questions/33591659/task-freezes-the-gui](https://stackoverflow.com/questions/33591659/task-freezes-the-gui)

---

<div class="post-metadata">

**Author:** ![illusive](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/illusive/32/61_2.png) [@illusive](https://forum.commonvisionblox.com/u/illusive)\
**Post date:** [January 26, 2022, 5:11pm UTC](https://forum.commonvisionblox.com/t/adding-overlays-using-task-and-lambda-expressions/1592/4 "2022-01-26T17:11:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![George](https://avatars.discourse-cdn.com/v4/letter/g/71e660/32.png) [@George](https://forum.commonvisionblox.com/u/George)\
**Post date:** [January 27, 2022, 8:41am UTC](https://forum.commonvisionblox.com/t/adding-overlays-using-task-and-lambda-expressions/1592/5 "2022-01-27T08:41:32Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Chris](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/chris/32/1154_2.png) [@Chris](https://forum.commonvisionblox.com/u/Chris)\
**Post date:** [January 27, 2022, 8:42am UTC](https://forum.commonvisionblox.com/t/adding-overlays-using-task-and-lambda-expressions/1592/6 "2022-01-27T08:42:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![George](https://avatars.discourse-cdn.com/v4/letter/g/71e660/32.png) [@George](https://forum.commonvisionblox.com/u/George)\
**Post date:** [January 27, 2022, 8:49am UTC](https://forum.commonvisionblox.com/t/adding-overlays-using-task-and-lambda-expressions/1592/7 "2022-01-27T08:49:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chris](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/chris/32/1154_2.png) [@Chris](https://forum.commonvisionblox.com/u/Chris)\
**Post date:** [January 27, 2022, 8:50am UTC](https://forum.commonvisionblox.com/t/adding-overlays-using-task-and-lambda-expressions/1592/8 "2022-01-27T08:50:08Z")

</div>

Let us know if you found the problem.
