简体   繁体   中英

Multithreading in UWP Windows 10

I am making an UWP app for Windows 10. I want to execute two methods in a separate thread other than the Parent/Default thread, so that my app can be a little faster. But I could not find Thread class in UWP. What I have is Task, but it's not simple as Thread.

The methods are like below.

public List<Rootobject> <Method_name>()
private List<string> <Method_name>()

What am I missing?

You have to use Task instead of Thread because Thread is not supported at the moment (see open ticket https://github.com/dotnet/corefx/issues/2576 ). What is not so easy by creating a Task ?

Task t = Task.Factory.StartNew( () => {
              // Just loop.
              int ctr = 0;
              for (ctr = 0; ctr <= 1000000; ctr++)
              {}
              Console.WriteLine("Finished {0} loop iterations", ctr);
          } );

See the microsoft doc: link Example with your methods:

// Running the method which returns List<string>
Task<List<string>> result = Task.Factory.StartNew(() => ExpensiveMethod());


public List<string> ExpensiveMethod()
{
    return ...;
}

We've developed an open source package for UWP (and .NET Standard in general) which implements the System.Threading.Thread class (and related objects such as exceptions and enums) using threads under the hood.

TaskThreads is freely available (MIT-licensed) on GitHub and via NuGet , and should readily substitute as a drop-in replacement for the MS-provided (missing) STT class for UWP developers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM