i have a problem with threads, i have this code (example):
private void button_Click(object sender, EventArgs e) {
ShowMessage("Starting Downloads...");
<more code>
StartDownloads();
RunFileDownload();
<more code>
}
private void StartDownloads() {
<more code>
for (int i=0; i<10; i++) {
ShowMessage("Downloading file: " + i);
Download(i);
<more code>
}
<more code>
}
The problem is, when i press the button and the downloading starts, the messages are not displayed... I tried to fix it with threads, like this:
private void button_Click(object sender, EventArgs e) {
ShowMessage("Starting Downloads...");
Thread t = new Thread(new ThreadStart(StartDownloads));
t.Start();
RunFileDownload();
}
But the RunFileDownload(); function starts before the files are downloaded. I try solve this with "Thread.Join();" but again not displayed messages (The main thread is paused).
I thought solve it with a multi-thread and Thread.Join(); but it isn't efficient and i will have problems with others functions in the main thread.
How can i solve this problem? Thanks.
Edit #2:
Considering this code:
private void Download() {
ShowMessage("Starting Downloads...");
Thread t = new Thread(new ThreadStart(StartDownloads));
ShowMessage("Downloads Finished..."); | not run until
RunFileDownload(); | finished
ShowMessage("Files Executed..."); | thread.
}
How can i expect the thread finish before the rest of the code is executed? I try with Thread.Join(); but it freezes the application.