Execute two separate methods at the same time

I am trying to execute two different methods at the same time using Backgroundworker(or, if this is not possible, using another method) to improve performance. The idea is to do heavy calculations in methods.

Is it possible at all?

private readonly BackgroundWorker _worker = new BackgroundWorker();
private readonly BackgroundWorker _worker2 = new BackgroundWorker();

public MainWindow()
{
    InitializeComponent();

    _worker.WorkerReportsProgress = true;
    _worker.WorkerSupportsCancellation = true;
    _worker.DoWork += worker_DoWork;
    _worker.RunWorkerAsync();

    _worker2.WorkerReportsProgress = true;
    _worker2.WorkerSupportsCancellation = true;
    _worker2.DoWork += worker_DoWork2;
    _worker2.RunWorkerAsync();
}

private void worker_DoWork(object sender, DoWorkEventArgs e)
{
    // Do something
}

private void worker_DoWork2(object sender, DoWorkEventArgs e)
{
    //  Do something simultaneously with worker_DorWork
}
+4
source share
6 answers

Yes it is possible. Today, many applications consume multiple threads for parallel operation. When doing this kind of work, you also need to notice the traps.

, , WorkerA, WorkerB, (, Mutex, Semaphore, ManualResetEvent ..), .

, , , , 1 .

: , , Task Parallel Library, .NET 4.0. , Task Parallel .

Edit

, Task vs BackgroundWorker:

BackgroundWorker:

_worker.WorkerReportsProgress = true;
_worker.WorkerSupportsCancellation = true;
_worker.DoWork += worker_DoWork;
_worker.RunWorkerAsync();

:

var task = Task.Run(worker_DoWork);

.NET 4.0:

var task = Task.Factory.StartNew(worker_DoWork);

, , Task, .

+1

, IO .

, , BackgroundWorker Task ( ).

Task t1 = Task.Run( () => HeavyMethod1() );
Task t2 = Task.Run( () => HeavyMethod2() );

await Task.WaitAll(new Task[] {t1, t2});
+7

, , , , , . (, , , 2 CPU )).

- ( , , :)). ThreadPool .NET , ( http://msdn.microsoft.com/en-us/library/0ka9477y(v=vs.110).aspx).

EDIT:

@PatrickHofman , .

+2

, Microsoft .NET Framework .NET 4.5? , . , TextReader.ReadToEndAsync Method ( StreamReader.ReadToEndAsync ):

String result;
using (StreamReader reader = File.OpenText("existingfile.txt"))
{
    Console.WriteLine("Opened file.");
    result = await reader.ReadToEndAsync();
    Console.WriteLine("Contains: " + result);
}

. Asynchronous File I/O MSDN.

+1

TPL . . . . Task.WaitAll(), , , , TPL , .Result, .

var startTime = DateTime.Now;

Task<int> longCalculationResult = Task.Factory.StartNew<int>(PerformSomeLongCalculations);
Task<int> longerCalculationResult = Task.Factory.StartNew<int>(PerformSomeEvenLongerCalculations);

//Task.WaitAll(); // Wait for both calculations to finish

// or you can access results of both tasks, which will wait for them to finish in the background

Console.WriteLine("{0}, {1}", longCalculationResult.Result, longerCalculationResult.Result);

var timeTaken = (DateTime.Now - startTime).TotalSeconds;

Console.WriteLine("It took {0} seconds to calculate the results.", timeTaken);
+1

, , , . parallelism . , Google " .NET" . . , , .

0

All Articles