Code Monkey home page Code Monkey logo

005_asyncawait's Introduction

005_AsyncAwait

Asynchronous Programming with C#

This repository contains a simple example of asynchronous operation in C# using async/await.

Code Example

namespace _005_AsyncAwait
{
    internal class MyClass
    {
        int Operation()
        {
            Console.WriteLine("Operation in Thread Id = {0}", Environment.CurrentManagedThreadId);

            return 2 + 2;
        }

        public async void OperationAsync()
        {
            Task<int> task = Task<int>.Factory.StartNew(Operation);
            Console.WriteLine("\nResult {0}", await task);
        }
    }
}

using _005_AsyncAwait;

MyClass myClass = new();

myClass.OperationAsync();

Console.WriteLine("General Thread finish the work ThreadId = {0}", Environment.CurrentManagedThreadId);
Console.ReadLine();

In the provided code, MyClass contains the Operation method that performs some operation and returns a result. OperationAsync uses async/await to execute Operation asynchronously.

To initiate an asynchronous operation, Task<int>.Factory.StartNew is used to run Operation on a separate thread and return the result as a Task<int> object.

Description

  • Operation: A method that performs an operation (in this case, adding 2 and 2).
  • OperationAsync: An asynchronous method that asynchronously runs Operation and prints the result.

005_asyncawait's People

Contributors

nerses2001 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.