locked
Concrete Example for foreground thread and background thread RRS feed

  • Question

  • Hello,

    1.

    What business context is relevant to use foreground thread (FT) and background thread (BT)?

    Any concrete example for each one?

    2.

    What context and concrete example is relevant to use FT and BT at the same time?

    Thank you!

    Saturday, August 22, 2020 7:04 AM

All replies

  • Hello,

    Do you have an operation in mind for asking, knowing this may provide better context to your question.

    In the meantime reading the documentation would be advised and threading in C# ebook.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, August 22, 2020 11:08 AM
  • I strongly believe in knowledge management and business context is often applied in relation to technical issue. The problem what I don't understand is that 

    1.

    What business context is relevant to use foreground thread (FT) and background thread (BT)?

    Any concrete example for each one?

    2.

    What context and concrete example is relevant to use FT and BT at the same time?

    Thank you!

    Saturday, August 22, 2020 11:52 AM
  • Hi Sakura,

    When the application is closed, the foreground thread will continue to execute until the execution is complete, and the background thread will immediately stop executing.

    When performing IO operations, we don't want to see that only part of the information is written, because this may cause data loss. At this time, it is best to use the foreground thread.

    And if there is a thread used to perform the polling service, when the application is closed, it loses its usefulness, and it can be set as a background thread.

    Best Regards,

    Timon


    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.

    Monday, August 24, 2020 7:25 AM
  • The biggest difference between a foreground and background thread is simply in its role. Background threads are for background work. If the process is a background worker then mark it as background. Otherwise leave it as foreground.

    When a process shuts down any running foreground threads will prevent the process from terminating but background threads will be shut down. In general beyond the main thread (which is a foreground thread) you should create most threads as background.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, August 24, 2020 2:37 PM
  • The biggest difference between a foreground and background thread is simply in its role. Background threads are for background work. If the process is a background worker then mark it as background. Otherwise leave it as foreground. 

    How do you know when it is relevant for a task or assignment that should be applied for foreground or background work?

    Maybe async assignment (for instance retrieve data from the database when you have pressed the button to retrieve a list of products. DUring the retrieving time, a  small message says 'loading') as a background work?

    Tuesday, September 15, 2020 5:51 AM
  • Async (as in await/async with tasks) will never have you change the foreground/background status as you don't own or manage those threads. They are, by default, running on the thread pool which is automatically a background thread.

    If work can be done async and you cannot use tasks then yes background thread is most appropriate. However you cannot display a UI on any thread other than the UI thread. UI threads are foreground (for obvious reasons). Thus if you want to display a "loading" message in the UI that will happen in the foreground thread while the actual loading will happen in the background thread.

    Note that if you're using tasks (which is the best choice for this kind of problem) then you don't need to do anything special as all this can be done in a single method with a UI call, followed by an await on an async method (that runs on the background thread) and more UI calls. To do this using threads you'd update the UI on the UI thread (that triggered the work), spawn a background thread to do the async work and return from the function. At some point in the future the background thread would need to use a sync object (like semaphore, mutex or event) to notify the UI thread that it is done so the UI thread can update the UI. It is a lot more work for little gain.

    As for whether a thread should be foreground or background, it depends on the work. If the work is UI then it is foreground (in the UI thread that created the UI elements). If not then start with background. If that work has to complete before the process terminates then switch it to foregound.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, September 15, 2020 1:32 PM