-1
public MainPage()
{
    Method_1();
    Method_2();
    Method_3();
    Method_4();
    Method_5();
    Method_6();

}
  1. I am writing a Windows Phone 8.1 App (WINRT XAML). How to make these methods execute at a time from constructor? What I mean to ask is about multithreading, i want these methods to get executed sidebyside and not one after another.
  2. Does this effect loading of application? will app load fast/slow?
Atif Shabeer
  • 553
  • 6
  • 18
  • 2
    Do you need to do it all in the constructor? If the operations are time consuming, how about postponing them until the page is loaded? Also, some of the things you're doing may be UI related, meaning that even if you dispatch the operation to another thread, you would still have to dispatch it back to UI thread for rendering. In other words, your question needs more details and context to get any kind of meaningful answer. – Igor Ralic Jan 22 '15 at 11:53
  • The above 6 methods are non UI related. Sir, ok i will execute it in this.Loaded += LoadMethods(); But in LoadMethods() function they will still load 1 by 1 . Can you show me how to execute these paralely, i mean using multithreading – Atif Shabeer Jan 22 '15 at 12:01

2 Answers2

3

First off, you don't want to execute those methods in the constructor if they are long running methods. Wait until the page is loaded:

private async void Page_Loaded(object sender, RoutedEventArgs e)
{
    Task m1task = Method_1();
    Task m2task = Method_2();
    Task m3task = Method_3();
    Task all = Task.WhenAll(m1Task, m2Task, m3Task);
    await all;
}

The code will load off these operations to another thread and as long as your methods are properly implemented your UI will stay responsive (so don't use wait() for instance).

This is what a sample method could look like:

private async Task Method_1() {
    // Long running operation goes here
}

If you have some heavy computations to do, wrap them into Task.Run(() => { // Code });It's really essential that you're aware of the concepts of asynchronous programming. You might want to read on here:

Do you have to put Task.Run in a method to make it async?

await vs Task.Wait - Deadlock?

When correctly use Task.Run and when just async-await

But seriously, you're writing that your methods are not UI related. You might be better off running those somewhere else (e.g. in your ViewModels or even in a background task / service).

Community
  • 1
  • 1
Fred
  • 3,324
  • 1
  • 19
  • 29
0

Mark the methods as Async with return type Task.

eg. public async Task method1(){}

You won't be able to fire any UI activities from them, but they'll run outside of the main thread.

Kaelan Fouwels
  • 1,155
  • 1
  • 13
  • 28
  • 3
    Just marking a method async doesn't mean it goes on a different thread. – Igor Ralic Jan 22 '15 at 12:43
  • It's certainly enough to keep the UI responsive. – Fred Jan 22 '15 at 12:55
  • @Fred What do you mean? As igrali said aync Task also can run on UI thread and it can easily block UI with some heavy computation if it is not properly implemented. – Romasz Jan 22 '15 at 13:12
  • @Romasz I think it doesn't if the method is awaited. The compiler will generate a state-machine and run the code on another thread. The UI should stay responsive. I wasn't talking about performance - just responsiveness. – Fred Jan 22 '15 at 13:32
  • @Fred No. If you don't redirect your Task to ThreadPool it will run on UI thread. Consider such a Task: `private async Task DoSomething() { Task.Delay(5000).Wait(); await Task.Delay(1000); }` and run it for example upon button click: `await DoSomething()` - it will block the UI thread for 5 seconds. – Romasz Jan 22 '15 at 13:39