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 sharp async

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

} 
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

PREVIOUS NEXT
Code Example
Csharp :: autoit console write 
Csharp :: how to insert data into multiple tables using asp.net c# 
Csharp :: base c# 
Csharp :: page parent wpf 
Csharp :: color rgb to float c# 
Csharp :: C# Async Function with await 
Csharp :: Generic Stack in c# 
Csharp :: c# string across multiple lines 
Csharp :: c# generate random string 
Csharp :: unity screen size fix 
Csharp :: elasticsearch nested aggregation in c# 
Csharp :: why to make private fields readonly in c# 
Csharp :: list c# 
Csharp :: delete selected cells in Datagridview 
Csharp :: restrictions 
Csharp :: 2d explosion unity 
Csharp :: combined 2 arrays 
Csharp :: c# get enum name from value 
Csharp :: How to find column name with column index in DataGridView 
Csharp :: DataGridView set column cell Combobox 
Csharp :: system.componentmodel.dataannotations hide field 
Csharp :: nuget package TSETMC guide 
Csharp :: Unity Scene Load by BuildIndex 
Csharp :: html inside razor 
Csharp :: c# enum key value 
Csharp :: mvc remote validation additional table 
Csharp :: C# EDSDK control lens 
Csharp :: != in f# 
Csharp :: how to show enum name list from input in swagger c# 
Csharp :: Return out of a Ienumerator/Courotine in C# 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =