This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using System.Collections.Generic; | |
public class WhenAllExample | |
{ | |
public static async Task Main(string[] args) | |
{ | |
List<Task<string>> downloadTasks = new List<Task<string>> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
public class AsyncAwaitWithTPLExample | |
{ | |
public static async Task Main(string[] args) | |
{ | |
Console.WriteLine("Main thread ID: " + Environment.CurrentManagedThreadId); | |
// Using async/await with TPL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
public class ParallelForEachExample | |
{ | |
public static void Main(string[] args) | |
{ | |
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; | |
double sumSquares = 0.0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
public class ParallelForExample | |
{ | |
public static void Main(string[] args) | |
{ | |
int sum = 0; | |
Parallel.For(1, 101, i => |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
public class TaskContinuationExample | |
{ | |
public static void Main(string[] args) | |
{ | |
Task<int> originalTask = Task.Run(() => | |
{ | |
// Simulate some time-consuming computation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
public class TPLBeginnerExample | |
{ | |
public static void Main(string[] args) | |
{ | |
// Create and run a task | |
Task task = Task.Run(() => | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Singleton | |
{ | |
private static Singleton instance; | |
private Singleton() { } | |
public static Singleton Instance | |
{ | |
get | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class Program | |
{ | |
public static async Task Main(string[] args) | |
{ | |
LinkedInProfile linkedInProfile = new LinkedInProfile(); | |
try | |
{ | |
await linkedInProfile.GetProfileWithoutAsync(); | |
} | |
catch (Exception e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LinkedInProfile | |
{ | |
private readonly HttpClient _httpClient = new HttpClient(); | |
private readonly string _linkedInProfile = "https://www.linkedin.com/in/rahulsahay19/"; | |
public async Task<string> GetProfileUsingAsync() | |
{ | |
return await _httpClient.GetStringAsync(_linkedInProfile); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy() | |
{ | |
return HttpPolicyExtensions.HandleTransientHttpError() | |
.WaitAndRetryAsync(5, | |
retryAttempt => TimeSpan.FromMilliseconds(Math.Pow(2, retryAttempt) * 1000), | |
(_, waitingTime) => | |
{ | |
Console.WriteLine("Retrying....."); | |
}); | |
} |
NewerOlder