Skip to content

Windows Foreground threads not working as expected #28929

Open
@nk-alex

Description

@nk-alex

Description

I'm developing a .NET Maui application targeting Windows. This application must perform a synchronization process without blocking UI thread. Also this process must keep going until it's done. So if application gets killed, this thread must keep running.

Since I have no experience with .NET Maui targeting Windows, I start searching docs and I found out about foreground&background threads.

The interesting part from these docs, are the foreground threads. "Background threads are identical to foreground threads with one exception: a background thread does not keep the managed execution environment running." So if you keep your thread foreground type, then even if you close your application, the foreground thread will keep running (Just what I need).

This is my implementation:

Thread foregroundThread = new Thread(new ThreadStart(SynchronizationProcess));
foregroundThread.IsBackground = false;
foregroundThread.Priority = ThreadPriority.Highest;
foregroundThread.Start();


public async void SynchronizationProcess()
{
    int num_loops = 100;

    using (StreamWriter outputFile = new StreamWriter(Path.Combine(GetPath("synchronization.txt"))))
    {
        while (num_loops > 0)
        {
            outputFile.AutoFlush = true;
            outputFile.WriteLine("Remaining loops " + num_loops);
            Debug.WriteLine("Remaining loops " + num_loops);
            await Task.Delay(1000);
            num_loops = num_loops - 1;
        }

        outputFile.WriteLine("Process done");
        Debug.WriteLine("Process done");
    }
}

This is just for testing purpose. My application creates a files into AppData folder and keeps on writing lines until process is done. This is how I'm testing it:

  • This process is started on my application startup.
  • The foreground thread writes on my file successfully.
  • Then I close the application and check the file again. No more lines are added to the file once I close my application, which means, I guess, all threads including this foreground thread are killed.

What can I do to achieve this behaviour for Maui targetting windows?
I would really like to avoid using Windows BackgroundServices if I could.

Steps to Reproduce

  1. Create a new app targeting just windows.
  2. Create a windows foreground thread.
  3. Start this foreground thread from your application.
  4. Close the application. According to docs, this foreground thread should keep on working until its job it's done.

Link to public reproduction project repository

https://github.com/nk-alex/ForegroundThread

Version with bug

9.0.50 SR5

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

windows 10.0.19041.0

Did you find any workaround?

No response

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Labels

    platform/windows 🪟s/needs-attentionIssue has more information and needs another looks/verifiedVerified / Reproducible Issue ready for Engineering Triaget/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions