C × 8 – using declaration and asynchronous flow

Time:2019-11-6

These two themes have nothing to do with each other, but I’m afraid that the home page will be removed if the article is too short.

Using statement

usingStatement block 

although.NET CoreGarbage collector at runtime(GC)To be responsible for memory cleaning, but we still need to make sure that unmanaged resources should be cleaned up when they are no longer in use. Previously implemented forIDisposableInterface objects, we often use using Statement block to do this: 

class 
O references 
static void Mgin(stringC] 
gx•gs 
using (var 
= new DbConnection( 
db 
Console.WriteLine(

 

At this time, its output is as follows: 

Start using db. 
Db Connection Disposed 
End of the main method

 

It’s still a bit troublesome to write like this. It’s better to be simpler. But if notusingStatement, the resource will not be cleaned up: 

Mgin(stringC] 
static void 
args 
new DbConnection( ) 
db 
var 
.WriteLine(

 

Its output does notdisposedThat paragraph: 

Start using db. 
End of the main method

 

usingstatement 

But from C# 8At first, we can useusingDeclaration to do this is simpler than the previous using statement block. See the example directly: 

static v 
Elgin(stringC] 
gx•gs 
= new DbConnection() 
db 
using var 
Console.WriteLine(

It is to use the using declaration before defining variables. 

 

In this way, when the main method is finished, the DB resource can be cleaned up: 

Start using db. 
End of the main method 
b Connection Disposed

You can see that the DB is disposed, but you’ve certainly found a difference: the order of the last two lines of output has changed. 

 

When using the using statement block, the cleaning action occurs at the end of the using block: 

using (var 
= new DbConnection( 
db 
Console.WriteLine(

 

After using the using declaration, when the cleanup action goes beyond the scope of DB, that is, when leaving the main method: 

static void Elgin(stringC] 
gx•gs 
new DbConnection() 
db 
using var 
Console.WriteLine(

 

usage

The using statement block and using declaration should be used in combination with specific situations… 

For more details, please click:Official course

 

Asynchronous streams

Example 

private static int Threadld 
Thread . CurrentThread . ManagedThreadId ; 
O references 
static void Elgin(stringC] 
gx•gs 
new NumberFactory(); 
var factory = 
Console. WriteLine($

This is a very simple console program. It has one.NumberFactoryIt can generate a string of numbers according to the parameters passed(IEnumerable<int>) Then print out each number in this program, and display the current thread ID in the front. 

 

InsideNumberFactoryIt’s also very simple: 

public class NumberFactory 
I reference 
public IEnumerable<int> GenerateNumbers(int count) 
for (int i = 
0; i < count; 
Task. Delay(1ØØØ) .Wait(); 
yield return i + 1;

Here I made a delay to simulate the effect of reading external resources. 

 

Run the program (the article can not test the Pause effect of output one by one): 

Enumerating numbers.. . 
Received number: 1.

 

Enumerating numbers.. . 
Received number: 
Received number: 
Received number: 
Received number: 
Received number: 
4. 
All numbers received .

You can see that the ID of the thread is1。 Because it works like this, the program blocks the thread so that numberFactoryTo do its work. So this is not ideal. The best way is to use the asynchronous programming model of C ා, but in C ා# 8Before that, it couldn’t be done. But fromC# 8In the beginning, we can do that. 

 

Asynchronous StreamAsynchronous flow 

First, modify numberfactory, and in task.Delay(1000)Add the await keyword to replace.Wait()Method, and then modify the return type toIAsyncEnumberable, and add the async keyword before: 

public async IAsyncEnumerable<int> GenerateNumbers(int count) 
for (int i = 
0; i < count; 
await Task.Delay(1ØØØ); 
yield return i + 1;

 

Back to the main method, you need to make two changes: 

static async Task Mgin(stringC] 
gx•gs 
new NumberFactory(); 
var factory = 
Console.WriteLine($

First, inforeachAdd before loopawaitKeyword, which may seem strange, but that’s how we traverse the asynchronous flow. Notice thatforeachAdd await in front, not infactory.GenerateNumbers(5)Add await at the front. 

Then, you need to change the return type of the main method to task and add the async keyword. 

 

Finally, run the program to see the effect: 

As you can see, the ID of a thread sometimes changes, which is the effect we want. In this case, the flow is asynchronous. When it await task, the thread can do other work. When the program continues to execute, it may end in other threads. 

For more details, please click:Official course