Click here to Skip to main content
15,911,531 members
Home / Discussions / C#
   

C#

 
QuestionCombobox and "autocompletion" Pin
TMattC26-Mar-15 0:18
TMattC26-Mar-15 0:18 
AnswerRe: Combobox and "autocompletion" Pin
TMattC26-Mar-15 0:23
TMattC26-Mar-15 0:23 
AnswerRe: Combobox and "autocompletion" Pin
Simon_Whale26-Mar-15 1:29
Simon_Whale26-Mar-15 1:29 
GeneralRe: Combobox and "autocompletion" Pin
TMattC26-Mar-15 4:38
TMattC26-Mar-15 4:38 
Questionadd PDF file to email body not an attachment Pin
sourabkhajuria25-Mar-15 23:49
sourabkhajuria25-Mar-15 23:49 
AnswerRe: add PDF file to email body not an attachment Pin
OriginalGriff25-Mar-15 23:53
mveOriginalGriff25-Mar-15 23:53 
AnswerRe: add PDF file to email body not an attachment Pin
Dave Kreskowiak26-Mar-15 1:27
mveDave Kreskowiak26-Mar-15 1:27 
QuestionUnderstanding Async / Await Pin
zephaneas25-Mar-15 10:07
zephaneas25-Mar-15 10:07 
I am trying to understand Async, and I have 2 examples, but I don't see the difference:

Example 1
static string sampleFile = @"C:\Projects\Sandbox\Async1\Async1\somefile.txt";

static void Main(string[] args)
{
    Console.WriteLine("Task 1 started");

    Task task = new Task(ReadTheFile);
    task.Start();
    task.Wait();


    Console.WriteLine("The READ task was started");
    Console.ReadLine();
}

static void ReadTheFile()
{
    int count = 0;

    using (StreamReader reader = new StreamReader(sampleFile))
    {
        Console.WriteLine("Reading the file XXXX.");
        string v = reader.ReadToEnd();

        count += v.Length;
    }

    Console.WriteLine("Count: " + count);
}


Example 2
static void Main(string[] args)
{
    Console.WriteLine("Task 1 started");

    Task task = new Task(ProcessDataAsync);
    task.Start();
    task.Wait();


    Console.WriteLine("The READ task was started");
    Console.ReadLine();
}


static async void ProcessDataAsync()
{
    Task<int> task = HandleFileAsync(sampleFile);

    Console.WriteLine("Getting ready to read the file.");

    int x = await task;
    Console.WriteLine("Count: " + x);
}


static async Task<int> HandleFileAsync(string file)
{
    Console.WriteLine("HandleFile enter");
    int count = 0;

    using (StreamReader reader = new StreamReader(file))
    {
        Console.WriteLine("Reading the file.");
        string v = await reader.ReadToEndAsync();

        count += v.Length;
    }

    Console.WriteLine("HandleFile exit");
    return count;
}


They both read in a text file with 1,000,000 lines. The first one actually seems faster.

What is the real difference here?

Thanks
AnswerRe: Understanding Async / Await Pin
F-ES Sitecore25-Mar-15 12:23
professionalF-ES Sitecore25-Mar-15 12:23 
SuggestionRe: Understanding Async / Await Pin
Richard Deeming26-Mar-15 2:57
mveRichard Deeming26-Mar-15 2:57 
QuestionUse EPPlus to Read 1st Column Of Excel Workbook Into Array (.xlsx) Pin
Hellfire123125-Mar-15 4:42
Hellfire123125-Mar-15 4:42 
AnswerRe: Use EPPlus to Read 1st Column Of Excel Workbook Into Array (.xlsx) Pin
OriginalGriff25-Mar-15 5:25
mveOriginalGriff25-Mar-15 5:25 
QuestionEffective way of writing logs in web service in C#.net Pin
manishchourasia200025-Mar-15 0:19
professionalmanishchourasia200025-Mar-15 0:19 
AnswerRe: Effective way of writing logs in web service in C#.net Pin
Elegantly Wasted25-Mar-15 0:51
Elegantly Wasted25-Mar-15 0:51 
AnswerRe: Effective way of writing logs in web service in C#.net Pin
jschell25-Mar-15 11:28
jschell25-Mar-15 11:28 
QuestionCould not cal the repaint on invalidate Pin
Sarita S24-Mar-15 22:54
Sarita S24-Mar-15 22:54 
AnswerRe: Could not cal the repaint on invalidate Pin
OriginalGriff24-Mar-15 23:06
mveOriginalGriff24-Mar-15 23:06 
AnswerRe: Could not cal the repaint on invalidate Pin
OriginalGriff25-Mar-15 2:19
mveOriginalGriff25-Mar-15 2:19 
QuestionScheduler without using timer class in c# Pin
Member 1138787724-Mar-15 19:48
Member 1138787724-Mar-15 19:48 
AnswerRe: Scheduler without using timer class in c# Pin
Pete O'Hanlon24-Mar-15 20:13
mvePete O'Hanlon24-Mar-15 20:13 
QuestionSingleton vs Whatever Else Quandary Pin
Z.C.M.24-Mar-15 16:58
professionalZ.C.M.24-Mar-15 16:58 
AnswerRe: Singleton vs Whatever Else Quandary Pin
Mycroft Holmes24-Mar-15 17:09
professionalMycroft Holmes24-Mar-15 17:09 
AnswerRe: Singleton vs Whatever Else Quandary Pin
Pete O'Hanlon24-Mar-15 19:32
mvePete O'Hanlon24-Mar-15 19:32 
GeneralRe: Singleton vs Whatever Else Quandary Pin
Z.C.M.25-Mar-15 4:00
professionalZ.C.M.25-Mar-15 4:00 
AnswerRe: Singleton vs Whatever Else Quandary Pin
jschell25-Mar-15 11:34
jschell25-Mar-15 11:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.