Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
3
Reply
Threads.join() doesn't work like expected
Jeroen Kolk
Nov 5 2017 8:00 AM
188
Reply
Hi, I am totally new to C# and doing a retraining.
Today I walked into a little problem I can't solve yet..
I did a tutorial from Pragmtech on youtube:
https://www.youtube.com/watch?v=SgHYVPKJRX8&list=PLAC325451207E3105&index=102
The problem is that when running the code it doesn't show the text "processing file. Please wait".
I hope someone can help me out. Thx!
private
int
CountCharacters()
{
int
count = 0;
using
(StreamReader reader =
new
StreamReader(@
"C:\Users\TTTG11133.NOA\Documents\JeroenK\Test\AsyncAwaitPragmTech101\data.txt"
))
{
string content = reader.ReadToEnd();
count = content.Length;
Thread.Sleep(5000);
}
return
count;
}
//Async await with blocking (using task)
private
async
void
btnProcess1_Click(object sender, RoutedEventArgs e)
{
Task<
int
> task =
new
Task<
int
>(CountCharacters);
task.Start();
lblCount.Text =
"processing file. Please wait..."
;
int
count = await task;
lblCount.Text = count.ToString() +
"characters in file"
;
myTextBox.Text =
"jeroen"
;
}
private
void
btnProces2_Click(object sender, RoutedEventArgs e)
{
int
count = 0;
Thread
thread
=
new
Thread(() => { count = CountCharacters(); });
thread
.Start();
myTextBlock2.Text =
"processing file. Please wait..."
;
thread
.Join();
// The next string should appear after 5 sec.
myTextBlock2.Text = count.ToString();
}
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
3
)
Next Recommended Forum
Problem commit to GIT repos
how to edit the tags of mp3 file?