C# vs2013 Starting a task within a task

I have recently come across code that starts a task within another task. This is in code I am to take over maintenance of.

There appeared to be an issue with the main thread being altered due to this and being set as a background thread when one of the tasks completed. This in turn locked up the GUI on the application.
Has anyone heard of this behaviour before?

I will not be giving an example of this particular problem, this is more of an information gathering exercise.
Up to now I have created ThreadWorker classes (as recommended by MS) to do my multi threading but will be using tasks in the future if there are no downsides.
LVL 1
jetbetAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SriVaddadiCommented:
UI controls should not be changed from any other thread. There is a pattern on how you should update the ui. You should just be writing to the message queue. This link might help you.

Please note that this is a vast discussion and hence may be if you could more information it might be helpful to resolve your issue quickly.
jetbetAuthor Commented:
I have always used the following style to update the GUI from other threads.


uiContext = state as SynchronizationContext;

private void UpdateResultList()
        {
            try
            {

                uiContext.Post(new SendOrPostCallback((o) =>
                {
                    mainForm.UpdateResultList();
                }
                                    ), null);

            }
            catch
            {

            }
        }

Open in new window


The external developer has used tasks and this seems like a good way of managing threads.
The question though is
"Does anyone know of issues when tasks are created inside other tasks. Especially anything that could turn the main thread into a background thread?"
SriVaddadiCommented:
tasks inside other tasks (child tasks) could either run independently or depend on the the parent task.

Not sure what you mean by turning main thread into background?  could you please explain a little more.
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

jetbetAuthor Commented:
The external developer whose code I am to maintain had an issue where the GUI would lock up.
His investigations led him to believe that the issue was caused by a nested task returning and that somehow this caused the main thread to become a background thread.

This is not something that I have heard of myself, and just wanted to know if someone else had come across this.
SriVaddadiCommented:
UI thread  can never become background thread.   It looks like the tasks are not handled correctly.  This could very well be the case of detached child tasks.

Here is an article that might help you to fix your code.  I will not able to suggest the fix unless i see the code and find out what is wrong with it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jetbetAuthor Commented:
Thanks for your input.

I will compare revisions in the code and see what was changed. Good to know that the GUI can never become a background thread. I was pretty sure of this myself but good to have the confirmation
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.