threading

N

natebruneau

So I am new to the world of multi-threading. My multithreading
application works, however I want the application to run the commands
post-foreach loop after each thread has completed.

Lets say there are 10 threads. How can I create a wait after the
foreach loop?


foreach (testObject website in websites)
{
Thread th1 = new Thread(this.addToGrid);
th1.Start(website);
}

//Execute this after all threads have been completed
Console.WriteLine("Threads Completed");
 
N

natebruneau

Unforchantely this locks my GUI which isn't what I am thinking I want
for the User.

To give you an idea of what the threads are doing they are doing a get
request and response from a website. (10 websites)

Any other ideas?

Thanks

Nate
 
J

Jon Skeet [C# MVP]

Unforchantely this locks my GUI which isn't what I am thinking I want
for the User.

To give you an idea of what the threads are doing they are doing a get
request and response from a website. (10 websites)

Any other ideas?

Set up a count to determine how many requests are still outstanding.
At the end of each request, decrement the count (in a thread-safe
manner). If your thread happens to be the last one, use Control.Invoke
or Control.BeginInvoke to get back to the UI thread (assuming a
WinForms UI) and then display a message to the user.

Jon
 
C

Cor Ligthert[MVP]

Jon,

I was thinking about the same solution, however on a website, this means
AFAIK that the computer is processing with 10 active users at least 110
running threads at the same time, will this not give a bunch of overhead?

Cor
 
C

Cor Ligthert [MVP]

Peter,

My assumption was that this was on the ServerSide of IIS, otherwise I would
not have given the same solution as Jon did, however, he did already..

foreach (testObject website in websites)

Cor
 
J

Jon Skeet [C# MVP]

My assumption was that this was on the ServerSide of IIS, otherwise I would
not have given the same solution as Jon did, however, he did already..

foreach (testObject website in websites)

The fact that the OP talks about "this locks my GUI" suggests to me
that he's not writing a server side app. Of course we don't know for
sure, but I think it's an odd assumption to make that this *is* on the
server side. Looping through a bunch of websites is reasonably common
on the client side.

Jon
 
C

Cor Ligthert[MVP]

Jon,

I think that a lot of us have done what you write as their first
multithreading project, at least I did.

But forgive me my assumption.
An ASPNet project is so related since version 2005 with the word "WebSite"
that it was my assumption, maybe wrong who cares.

:)

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top