Szkolenie
Moduł
Implement Asynchronous Tasks - Training
Learn how to implement asynchronous tasks in C# apps using the `async` and `await` keywords and how to run asynchronous tasks in parallel.
Ta przeglądarka nie jest już obsługiwana.
Przejdź na przeglądarkę Microsoft Edge, aby korzystać z najnowszych funkcji, aktualizacji zabezpieczeń i pomocy technicznej.
Uwaga
Dostęp do tej strony wymaga autoryzacji. Może spróbować zalogować się lub zmienić katalogi.
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zmienić katalogi.
W tym przykładzie pokazano, jak używać System.Threading.Tasks.Task<TResult> klasy do zwracania wartości z Result właściwości . Aby użyć tego przykładu, należy upewnić się, że katalog C:\Users\Public\Pictures\Sample Pictures istnieje i że zawiera pliki.
using System;
using System.Linq;
using System.Threading.Tasks;
class Program
{
static void Main()
{
// Return a value type with a lambda expression
Task<int> task1 = Task<int>.Factory.StartNew(() => 1);
int i = task1.Result;
// Return a named reference type with a multi-line statement lambda.
Task<Test> task2 = Task<Test>.Factory.StartNew(() =>
{
string s = ".NET";
double d = 4.0;
return new Test { Name = s, Number = d };
});
Test test = task2.Result;
// Return an array produced by a PLINQ query
Task<string[]> task3 = Task<string[]>.Factory.StartNew(() =>
{
string path = @"C:\Users\Public\Pictures\Sample Pictures\";
string[] files = System.IO.Directory.GetFiles(path);
string[] result = (from file in files.AsParallel()
let info = new System.IO.FileInfo(file)
where info.Extension == ".jpg"
select file).ToArray();
return result;
});
foreach (string name in task3.Result)
Console.WriteLine(name);
}
class Test
{
public string Name { get; set; }
public double Number { get; set; }
}
}
Imports System.Threading.Tasks
Module Module1
Sub Main()
ReturnAValue()
Console.WriteLine("Press any key to exit.")
Console.ReadKey()
End Sub
Sub ReturnAValue()
' Return a value type with a lambda expression
Dim task1 = Task(Of Integer).Factory.StartNew(Function() 1)
Dim i As Integer = task1.Result
' Return a named reference type with a multi-line statement lambda.
Dim task2 As Task(Of Test) = Task.Factory.StartNew(Function()
Dim s As String = ".NET"
Dim d As Integer = 4
Return New Test With {.Name = s, .Number = d}
End Function)
Dim myTest As Test = task2.Result
Console.WriteLine(myTest.Name & ": " & myTest.Number)
' Return an array produced by a PLINQ query.
Dim task3 As Task(Of String()) = Task(Of String()).Factory.StartNew(Function()
Dim path = "C:\Users\Public\Pictures\Sample Pictures\"
Dim files = System.IO.Directory.GetFiles(path)
Dim result = (From file In files.AsParallel()
Let info = New System.IO.FileInfo(file)
Where info.Extension = ".jpg"
Select file).ToArray()
Return result
End Function)
For Each name As String In task3.Result
Console.WriteLine(name)
Next
End Sub
Class Test
Public Name As String
Public Number As Double
End Class
End Module
Właściwość Result blokuje wątek wywołujący do momentu zakończenia zadania.
Aby dowiedzieć się, jak przekazać wynik System.Threading.Tasks.Task<TResult> klasy do zadania kontynuacji, zobacz Łączenie zadań przy użyciu zadań kontynuacji.
Opinia o produkcie .NET
.NET to projekt typu open source. Wybierz link, aby przekazać opinię:
Szkolenie
Moduł
Implement Asynchronous Tasks - Training
Learn how to implement asynchronous tasks in C# apps using the `async` and `await` keywords and how to run asynchronous tasks in parallel.