Waiting for custom features

I am trying to make out new async functions in C #, and so far the strangest thing I've noticed is that each example for asynchronous functions has a function that expects another async function defined in the framework, but none of them have custom code.

For example, I want to create an object from each line in a text file, but asynchronously so that the user interface thread does not hang:

async Task Read()
{
    string[] subjectStrings = File.ReadAllLines(filePath);
    for (int i = 0; i < subjectStrings.Length; i++)
    {
        Task<Subject> function = new Task<Subject>(code => new Subject((string)code), subjectStrings[i]);
        try
        {
            Subject subject = await function;
            subjects.Add(subject);
        }
        catch (Exception ex)
        {
            debugWriter.Write("Error in subject " + subjectStrings[i]);
            continue;
        }
    }
}

As you can see, I define a task that creates a new object Subjectbased on a line in a text file, and then waits for this task. If I do this, the debugger will stand on a line awaitand then just stop. As far as I know, more code does not run.

async, Task.ContinueWith() , .

, :

  • ? async, - ?
  • async? await, async, async , ?
+3
5

? async, - ?

await Task.Run await Task.Factory.StartNew . new Task , . , Start .

async? , async, async , ?

"root" :

  • : Wait Task.

  • GUI: async void.

  • ASP.NET MVC: Task.

+1

, .

Task.Run new Task, .

, , ... Subject , , .

+5

async, - ?

. , ; async.

async . Task.Run ( ), TaskCompletionSource<T> , Task.Factory.FromAsync.

async? , async, async , ?

. async . ; Main. async WinForms, WPF, Silverlight, Windows Store, ASP.NET MVC, WebAPI, SignalR, iOS, Android Windows Phone, .

async await , Task.WhenAll Task.WhenAny. async, ; , async IObservable<T>.

+1

. async/await, . , await , , , .:

var result = await Task.Run(YourLongOperation);

, , , - , . , , .

, async/await s, , , . , / ( , ).

0

™, , .

, .

class SubjectFactory
{
    public IEnumerable<Subject> Read(string filePath)
    {
        string[] subjectStrings = File.ReadAllLines(filePath);

        return Parse(subjectStrings);
    }

    private IEnumerable<Subject> Parse(IEnumerable<string> subjects)
    {
        string code = "XYZ";

        foreach ( var subject in subjects )
        {
            yield return new Subject(code, subject);
        }
    }
}

, Subject , File.ReadAllLines. ? - .

, ?

File.ReadAllLinesAsync(), .

public async Task<IEnumerable<Subject>> ReadAsync(string filePath)
{ // Doesn't exist!
    string[] subjectStrings = await File.ReadAllLinesAsync(filePath);

    return this.Parse(subjectStrings);
}

, , . , .

private async Task<string[]> ReadAllLinesAsync(string filePath)
{
    ArrayList allLines = new ArrayList();

    using ( var streamReader = new StreamReader(File.OpenRead(filePath)) )
    {
        string line = await streamReader.ReadLineAsync();

        allLines.Add(line);
    }

    return (string[]) allLines.ToArray(typeof(string));
}

, , ReadAllLinesAsync().

public async Task<IEnumerable<Subject>> ReadAsync(string filePath)
{
    // call with 'this' instead of 'File'
    string[] subjectStrings = await this.ReadAllLinesAsync(filePath);

    return Parse(subjectStrings);
}

When using all of this in your WPF application, all you need to do is the following:

var filePath             = @"X:\subjects\";
var subjectFactory       = new SubjectFactory();
var subjectsCollection   = await subjectFactory.ReadAsync(filePath);
var observableCollection = new ObservableCollection<Subject>(subjectsCollection);
0
source

All Articles