0% found this document useful (0 votes)
22 views4 pages

C# Threading and Async Examples

The first code snippet demonstrates the use of threading in C# to execute a method that prints a message from a separate thread, followed by a completion message in the main thread. The second code snippet illustrates asynchronous programming in C#, where a method displays numbers with a delay while the main thread continues to run, ultimately waiting for the task to complete before printing a final message. Both examples showcase different concurrency models in C#.

Uploaded by

Sarathi M
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

C# Threading and Async Examples

The first code snippet demonstrates the use of threading in C# to execute a method that prints a message from a separate thread, followed by a completion message in the main thread. The second code snippet illustrates asynchronous programming in C#, where a method displays numbers with a delay while the main thread continues to run, ultimately waiting for the task to complete before printing a final message. Both examples showcase different concurrency models in C#.

Uploaded by

Sarathi M
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1)

Code:

using System;
using [Link];

class Program
{
static void hello()
{
[Link]("Hello from thread!");
}

static void Main()


{

Thread h = new Thread(hello);


[Link]();
[Link]();

[Link]("Work Completed");
}
}
Output:
2)
Code:
using System;
using [Link];

class Program
{
static async Task display()
{
for (int i = 1; i <= 10; i++)
{
[Link](i);
await [Link](1000);
}
}

static async Task Main()


{

Task g = display();

[Link]("Main thread running");

await g;

[Link]("Work Completed");
}

}
Output:

You might also like