none
LifeCycles of Thread.IsBackGround, Task, Thread and SynchronizedContext RRS feed

  • Question

  • I have several things to understand to confirm in my summaries, please read them and see questions below:

    1) Application LifeCycle:The life cycle of a whole application from its start to end by closing or disposing it……ect.

    2) SynchronizedContext:The life cycle is as long as "Application LifeCycle" this is the MAIN thread that controls with UI or the whole application's activities.

    3)Thread.IsBackGround:A simple thread is seperated from the application itself, which means this can run by itself. However, if the "BackGround" = true, this means the lifecycle of the thread is as long as the Application LifeCycle or SynchronizedContext. This means when your app is closed or disposed, the thread is also disposed.

    4)For Web application, each request has an AspNetSynchronizedContext. So you cannot use something like Thread.IsBackGround/Task/ThreadPool……ect. to do a very complicated task unless you use something like "Join", "Wait"……ect. Otherwises maybe the thread is closed before the real action is processed. The "AspNextSynchronizedContext" belongs to EACH Request instead of the whole IIS LifeCycle.

    Questions:

    a) Please check each of my summaries, if you think anything to talk about or point out my mistakes, welcome to do that.

    b) if Point 4 is right. How do we use Task, Thread into a Web/WebApi/WCF-based application?

    c) Does "Console Application" has a SynchronizedContext?Sometimes I find if I use "IsBackGround" thread (true for the property) I MUST use "Console.WriteLine" to avoid the end of lifecycle. So is there anything related between SynchronizedContext and the Application LifeCyle"? Are they same?

    Reguards!


    ASP.NET Forum
    StackOverFlow
    FreeRice Donate
    Issues to report





    Sunday, July 9, 2017 12:27 PM

Answers

  • Hi ThankfulHeart,

    Thank you for posting here.

    >>a) Please check each of my summaries, if you think anything to talk about or point out my mistakes, welcome to do that..

    4)For Web application, each request has an AspNetSynchronizedContext. So you cannot use something like Thread.IsBackGround/Task/ThreadPool……ect. to do a very complicated task unless you use something like "Join", "Wait"……ect. Otherwises maybe the thread is closed before the real action is processed. The "AspNextSynchronizedContext" belongs to EACH Request instead of the whole IIS LifeCycle.

    Here are some links about how to use background thread in asp.net application.

    http://labs.bjfocus.co.uk/2014/07/running-a-background-task-inside-an-asp-net-website/

    >>b) if Point 4 is right. How do we use Task, Thread into a Web/WebApi/WCF-based application?

    You would like to check the following link. It shows example about how to run background tasks in asp.net.

    https://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx

    >>c) Does "Console Application" has a SynchronizedContext?Sometimes I find if I use "IsBackGround" thread (true for the property) I MUST use "Console.WriteLine" to avoid the end of lifecycle. So is there anything related between SynchronizedContext and the Application LifeCyle"? Are they same?

    The console application doesn’t have a SynchronizationContext. When your console application’s Main method is invoked, SynchronizationContext.Current will return null. That means that if you invoke an asynchronous method in your console app, unless you do something special(you explicitly override that with SynchronizationContext.SetSynchronizationContext), your asynchronous methods will not have thread affinity: the continuations within those asynchronous methods could end up running “anywhere.”

    For more details, please refer to the MSDN article.

    https://blogs.msdn.microsoft.com/pfxteam/2012/01/20/await-synchronizationcontext-and-console-apps/

    Best Regards,

    Wendy

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. 

    Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet. 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Tuesday, July 11, 2017 6:18 AM
    Moderator