Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

async await c#

// simple method structure
public static  Task<int> FooAsync(int num)
{
   num+=5;
   return Task.FromResult(num);
}
// calling function structure
public static async void Solve()
{
    var temp = await FooAsync(i);
}
Comment

async using c#

using(HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com")))
{
    await this.responseLogger.LogResponseAsync(response);

    return true;
}
Comment

c# async and await example

Live async and await the demontration of the image described in the source link.
Comment

C# Async Function simple

// Sample 3: Async Task, simple version

// Starts and wait for the Task, get the result.
string result = await Read();
Console.WriteLine("Result: " + result);

// A Async Funktion thats return a Task<String>
static async Task<string> Read()
{
    string rv = "";
    Task task = new Task(ReadTask);

    task.Start();
    // Wait for finish
    await task;

    return rv; // Convertet to Task<string>

    // Without Return-Value, is an Action<>
    void ReadTask()
    {
        while (!System.IO.File.Exists("G:FILE")) System.Threading.Thread.Sleep(100);
        rv = System.IO.File.ReadAllText("G:FILE");
    }
}
Comment

C# Async Function with await

// Sample 1: Async Task, with await for result
// Search for C# Async Function without await

// Starts the Task, that wait for G:FILE, then read all content and ends.
Task<string> ReadTask = Read();
Console.WriteLine("Before file exists...");
await ReadTask; // Wait for Result the Task; Create a file "G:FILEx", write Content, Rename to "FILE"

string res = ReadTask.Result;
Console.WriteLine("After:" + res);

// A Async Funktion thats return a Task<String>
static async Task<string> Read()
{
    string rv = "";
    Task task = new Task(ReadTask);

    task.Start();
    // Wait for finish
    await task;

    return rv; // Convertet to Task<string>

    // Without Return-Value, is an Action<>
    void ReadTask()
    {
        while (!System.IO.File.Exists("G:FILE")) System.Threading.Thread.Sleep(100);
        rv = System.IO.File.ReadAllText("G:FILE");
    }
}
Comment

c sharp async

static public async Task Name () 
{
	Console.Write("hello ");
	await Task.Delay(5000); 
	Console.WriteLine("world");

} 
Comment

PREVIOUS NEXT
Code Example
Csharp :: SQLite Parameters 
Csharp :: unity how to check index of enum 
Csharp :: c# anonymous type as return value 
Csharp :: c# faker 
Csharp :: count number of specific characters in string c# 
Csharp :: create class for database connection in c# 
Csharp :: winforms combobox get selected text 
Csharp :: ef core add OnModelCreating foreign key 
Csharp :: instantiate c# 
Csharp :: c# list to observablecollection 
Csharp :: c# return values 
Csharp :: how to add arrays in c# 
Csharp :: change a positive number to negative or a negative number to positive 
Csharp :: array to object c# 
Csharp :: symfony debug bar 
Csharp :: register all services microsoft .net core dependency injection container 
Csharp :: how to add object in dictionary in c# 
Csharp :: This page contains six pages, created with MigraDoc and scaled down to fit on one page 
Csharp :: How do I call a string inside a different class 
Csharp :: hide component in component menu 
Csharp :: How do I identify the referrer page in ASP.NET? 
Csharp :: c# Class instance 
Csharp :: html inside razor 
Csharp :: pcamera 
Csharp :: c# check value at design time 
Csharp :: asp.net list size 
Csharp :: Query Parent-GrandChild collection 
Csharp :: C# How to implement IEnumerable<T interface 
Csharp :: c# propertyinfo indexof 
Csharp :: c# language 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =