Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

C# Async Function without await

// Sample 2: Async Task, without await for result
// Search for C# Async Function simple for a simple version

// Starts the Task, that wait for G:FILE, then read all content and ends.
Task<string> readTask = Read();
Console.WriteLine("Before file exists...");

// Do Something other, and checks if task completed
bool doMore = true;
while (doMore)
{
    Console.Write("Do more? (Y/n): ");
    string? answer = Console.ReadLine();
    doMore = answer?.ToUpper() != "N";
    if (readTask.IsCompleted)
    {
        string res = readTask.Result;
        Console.Write("The ReadTask was ready, Result: " + res);
        readTask.Dispose();
    }
}


// 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 :: c# split string 
Csharp :: create stripe subscription pay_immediately 
Csharp :: hide external app from taskbar 
Csharp :: elasticsearch nested aggregation in c# 
Csharp :: docker-compose cassandra db 
Csharp :: c# comments 
Csharp :: c# 2d arrays 
Csharp :: escape in c# 
Csharp :: c# sc create service 
Csharp :: c# code examples 
Csharp :: c# nunit test case 
Csharp :: math.pow in C# using loop 
Csharp :: static property in c# 
Csharp :: c# async task constructor 
Csharp :: f# get last element of list 
Csharp :: jtoken toobject is not exact double 
Csharp :: what does focusbale do in listview WPF 
Csharp :: list equals in order c# 
Csharp :: Uninstall-SPSolution: This solution contains resources scoped for a Web application and must be retracted from one or more Web applications. 
Csharp :: list findall c# 
Csharp :: constant interpolated string 
Csharp :: how to hide tree level button when no record found for devexpress child grid view in Winform c# 
Csharp :: how to get angular on visual studio mac 
Csharp :: Unity how get Attributes of a gameObject 
Csharp :: .net entities query multiple join condition type inference 
Csharp :: replace filename extension c# 
Csharp :: class merging 
Csharp :: telerik raddatepicker default date today wpf 
Csharp :: last word of string to uppercase c# 
Csharp :: c# regex double of some letters only 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =