Skip to content

Commit

Permalink
Awaiting mean not waiting
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonard Sperry committed Dec 18, 2020
1 parent 854de29 commit 1b19683
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
82 changes: 82 additions & 0 deletions Samples/AwaitMeansNoWait/MultiDataCalls.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Samples.AwaitMeansNoWait
{
class MultiDataCallsSync
{
IBookProvider _bookProvider;

object GetPageInfo(int bookId)
{
var book = _bookProvider.GetById(bookId);
var booksByAuthor = _bookProvider.GetByAuthor(book.AuthorId);
var booksInCategory = _bookProvider.GetTop10BooksInCategory(book.PrimaryCategoryId);

return new {
booksByAuthor,
booksInCategory
};
}
object GetPageInfo2(int bookId)
{
var book = _bookProvider.GetById(bookId);
IEnumerable<Book> booksByAuthor = null;
IEnumerable<Book> booksInCategory = null;
Thread authorThread = new Thread(() =>
booksByAuthor = _bookProvider.GetByAuthor(book.AuthorId)
);
authorThread.Start();
Thread catThread = new Thread(() =>
booksInCategory =
_bookProvider.GetTop10BooksInCategory(book.PrimaryCategoryId)
);
catThread.Start();
authorThread.Join();
catThread.Join();

return new
{
booksByAuthor,
booksInCategory
};
}

async Task<object> GetPageInfoAsync(int bookId)
{
var book = await _bookProvider.GetByIdAsync(bookId);
var booksByAuthor = _bookProvider.GetByAuthorAsync(book.AuthorId);
var booksInCategory = _bookProvider
.GetTop10BooksInCategoryAsync(book.PrimaryCategoryId);

return new {
booksByAuthor = await booksByAuthor,
booksInCategory = await booksInCategory
};
}
}

class Book
{
public int AuthorId { get; set; }
public int PrimaryCategoryId { get; set; }
}

class Catagory { }

interface IBookProvider
{
Book GetById(int bookId);
Task<Book> GetByIdAsync(int bookId);

IEnumerable<Book> GetTop10BooksInCategory(int categoryId);
Task<IEnumerable<Book>> GetTop10BooksInCategoryAsync(int categoryId);

IEnumerable<Book> GetByAuthor(int authorId);
Task<IEnumerable<Book>> GetByAuthorAsync(int authorId);
}
}
20 changes: 20 additions & 0 deletions Samples/AwaitMeansNoWait/NoAwait.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Samples.AwaitMeansNoWait
{
class NoAwait
{
async Task<bool> DoWork()
{
return await Task.Run(() => true);
}

Task<bool> DoWorkFixed()
{
return Task.Run(() => true);
}
}
}
39 changes: 39 additions & 0 deletions Samples/brvtybd/ExpandingIdeas.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Samples.brvtybd
{
class ExpandingIdeas
{
void Example(string input)
{
while(!int.TryParse(input, out int choice) || choice < 1)
{
Console.WriteLine("choose a valid option");
input = Console.ReadLine();
}
}

void Example2()
{
var userGroupValidatorFactory = default(object);

}

object AddCategory(bool condition1, bool condition2)
{
object dependency = null;
object returnValue;

return condition1 ? condition2 ? DoTheThing(dependency) :
DoTheThing(default) : default;
}

object DoTheThing(object input)
{
return default;
}

}
}

0 comments on commit 1b19683

Please sign in to comment.