public async static Task OperateAsyncMethod() { AsyncMethods asyncM1 = new AsyncMethods(); await asyncM1.RunAsyncMethod(); System.Console.WriteLine("In C# 7.1, To use async method"); System.Console.WriteLine($"Main Method execution started at {System.DateTime.Now}"); await Task.Delay(2000); System.Console.WriteLine($"Main Method execution ended at {System.DateTime.Now}"); }
public void ChainMethodsAsynchronously() { int result = 0; AsyncMethods.AddTwoNumbersAsync(5, 10) .SelectMany(aPlusB => AsyncMethods.MultiplyByFiveObservable(aPlusB)) .Subscribe(x => result = x); // This isn't a good idea in general, see chapter 9! Thread.Sleep(1000); Assert.AreEqual(75, result); }
public async Task <IActionResult> GetPageLengthAsync() { var length1 = await AsyncMethods.GetPageLength(); var length2 = await AsyncMethods.GetPageLengthAsync(); ViewBag.Title = "Async Demo"; var message = new[] { $"Page length. First method: {length1}", $"Page length. Second method: {length2}", }; return(View("NextPage", message)); }
private async Task InvokeActions() { try { while (!ActionList.Items.IsEmpty) { if (ActionList.Items[0] is ActionConfig action) { if (double.IsNaN(action.range1)) { await AsyncMethods.DriveAsync( action.v, action.w, TimeSpan.FromSeconds(action.range0), x => _windowContext.Progress = x, e => _windowContext.ErrorInfo = e.Message ).ConfigureAwait(true); } else { await AsyncMethods.DriveAsync( action.v, action.w, action.range0, action.range1, x => _windowContext.Progress = x, e => _windowContext.ErrorInfo = e.Message ).ConfigureAwait(true); } } else if (ActionList.Items[0] is RudderControlConfig rudderControl) { await AsyncMethods.AdjustRudderAsync( rudderControl.value, x => _windowContext.Progress = x, e => _windowContext.ErrorInfo = e.Message ).ConfigureAwait(true); } ActionList.Dispatch(it => { try { it.Items.RemoveAt(0); } catch (ArgumentOutOfRangeException) { } }); } } finally { task = null; } }
public void SetUp() { _methods = new AsyncMethods(); }
public async Task <ViewResult> Index() { long?length = await AsyncMethods.GetPageLength(); return(View(new string[] { $"Length : {length}" })); }
//public IActionResult Index() //{ // // var productsString = Product.GetProducts().Select(product => string.Format($"Name = {product?.Name ?? "No Product"} In stock = {product?.InStock.ToString() ?? "No Product"}")); // // new way to initialise dictionaries // //var products = new Dictionary<string, Product> // //{ // // ["Kayak1"] = new Product { Name = "Kayak", Price = 275m}, // // ["Lifejacket1"] = new Product { Name = "LifeJacket", Price = 48.95m } // //}; // // var totalPrice = new string[] { Product.GetProducts().TotalPrice().ToString() }; // var minimumPrice = 50; // // bool filterByPrice(Product product) { return product.Price > minimumPrice; } // // local function // bool filterByPrice(Product product) => product?.Price > minimumPrice; // // use of null conditional operator and coalesce operator together pretty powerful! // bool filterByName(Product product) => product?.Name?.ToLower().StartsWith("ka") ?? false; // var shoppingCart = new ShoppingCart // { // Products = Product.GetProducts() // }; // // var totalPrice = new string[] { shoppingCart.TotalPrice().ToString() }; // // var filteredProducts = shoppingCart.FilterBy(filterByPrice); // // var filteredProducts = shoppingCart.FilterBy(filterByName); // // return View("Index", new string[] { filteredProducts.TotalPrice().ToString() }); // return View("Index", new string[] { pageLengthTask.ToString() }); // } public async Task <IActionResult> Index() { var pageLength = await AsyncMethods.GetPageLength(); return(View("Index", new string[] { $"pageLength={pageLength}" })); }
//public IActionResult Index() //{ // //lambda func // bool FilterByPriceUsingLambda(Product p) // { // return ((p?.Price ?? 0) >= 20); // } // ShoppingCart cart = new ShoppingCart() // { // Products = Product.GetProducts() // }; // Product[] productArray = // { // new Product {Name = "Kayak", Price = 120M}, // new Product {Name = "Lifejacket", Price = 32.44M}, // new Product { Name = "Toyota Rav4", Price = 900.40M} // }; // decimal totalProductArray = productArray.TotalPrice(); // extension applies to array list // decimal totalCart = cart.TotalPrice(); // decimal lambdaTotal = productArray.FilterByPriceLambda(FilterByPriceUsingLambda).TotalPrice(); //using lambda // decimal yieldedTotalPrice = productArray.FilterByPrice(700).TotalPrice(); // return View("Index", new string[] { $"Total filter: {yieldedTotalPrice:C2}, Lambda total: {lambdaTotal:C2}"}); // //return View("Index", new string[] { $"Cart Total: {totalCart:C2}", $"Array Total: {totalProductArray:C2}"}); ////} public async Task <ViewResult> Index() { long?httpCallLength = await AsyncMethods.GetPageLength(); return(View("Index", new string[] { $"length from http call: {httpCallLength}" })); }
public MainPage() { this.InitializeComponent(); _methods = new AsyncMethods(); _methods.StateEvent += UpdateStateHandler; }
private void ThrottleInnerTest() { AsyncMethods.Throttle(() => _throttleCount--, 10); }
private void DeBounceInnerTest() { AsyncMethods.DeBounce(() => _deBounceCount--, 10, true); }