MultiThreading\Design Question
-
Hi! This is what i have so far... foreach (string result in Ldap.Query(filtertext)) { ThreadStart starter = delegate { NetInfo.Resolve(result, out ipResult); }; Thread t = new Thread(starter); t.Start(); while (t.ThreadState.ToString() == "Running") { Thread.Sleep(50); } } The problem in my mind is that i'm making the UI thread sleep... this ties up the UI and seeing as how i started trying to integrate multi-threading to stop something like this and increase the speed at which i can ping other machine's and return there address assuming they are up. What should i do because i need alot of the values off the UI and i'd still like to not tie up the UI i've been reading a bit about using a threadpool i'm not sure if that would be faster or not... The other problem that i'm running into is that i have to pull a bunch of values off the form and then obviously i need to put other values back on the form in a listview. I was thinking that i could start a new thread that starts all the threads that pulls all the values back and then stores them into something like a arraylist and pushes the values back to the form... is this a good idea? or should i just forget about multi threading? I hope this isn't to muddled up and is clear enough for people to understand if you need further clarification lemme know... :) I know the human being and fish can coexist peacefully. -George Dubya Bush Don't believe me? http://politicalhumor.about.com/library/blbushism-fish.htm Yes thats right he actually said that...
-
Hi! This is what i have so far... foreach (string result in Ldap.Query(filtertext)) { ThreadStart starter = delegate { NetInfo.Resolve(result, out ipResult); }; Thread t = new Thread(starter); t.Start(); while (t.ThreadState.ToString() == "Running") { Thread.Sleep(50); } } The problem in my mind is that i'm making the UI thread sleep... this ties up the UI and seeing as how i started trying to integrate multi-threading to stop something like this and increase the speed at which i can ping other machine's and return there address assuming they are up. What should i do because i need alot of the values off the UI and i'd still like to not tie up the UI i've been reading a bit about using a threadpool i'm not sure if that would be faster or not... The other problem that i'm running into is that i have to pull a bunch of values off the form and then obviously i need to put other values back on the form in a listview. I was thinking that i could start a new thread that starts all the threads that pulls all the values back and then stores them into something like a arraylist and pushes the values back to the form... is this a good idea? or should i just forget about multi threading? I hope this isn't to muddled up and is clear enough for people to understand if you need further clarification lemme know... :) I know the human being and fish can coexist peacefully. -George Dubya Bush Don't believe me? http://politicalhumor.about.com/library/blbushism-fish.htm Yes thats right he actually said that...
I agree that "Thread.Sleep(50);" is sleeping the UI thread. Bad thing to do, generally speaking. You might want to change the
while(t.ThreadState.ToString() == "Running"){}
intowhile(t.ThreadState == ThreadState.Running){}
Strings are slower, and string compare should be done usingString.Compare(string a, string b)
, nota == b
, which does not compare the string content. There are a number of mechanisms for sharing data between the UI and other threads. A simple one is to have the threadInvoke
a call on the form. This will allow the other thread to pass data back to the form with out UI sync problems. You also need to pass the initial data in to the thread you are starting. Generally, I create a class that has the threaded method, and holds the data (or references) that are needed. The UI thread creates objects of that class, hands them the data, and tells them to go start themselves. That way you just start them,and don't have to wait for any type of sync events before you start the next. LMK if that helps.Learn to write self marginalizing code! Call 1-888-BAD-CODE ------------------ Silver member by constant and unflinching longevity.
-
Hi! This is what i have so far... foreach (string result in Ldap.Query(filtertext)) { ThreadStart starter = delegate { NetInfo.Resolve(result, out ipResult); }; Thread t = new Thread(starter); t.Start(); while (t.ThreadState.ToString() == "Running") { Thread.Sleep(50); } } The problem in my mind is that i'm making the UI thread sleep... this ties up the UI and seeing as how i started trying to integrate multi-threading to stop something like this and increase the speed at which i can ping other machine's and return there address assuming they are up. What should i do because i need alot of the values off the UI and i'd still like to not tie up the UI i've been reading a bit about using a threadpool i'm not sure if that would be faster or not... The other problem that i'm running into is that i have to pull a bunch of values off the form and then obviously i need to put other values back on the form in a listview. I was thinking that i could start a new thread that starts all the threads that pulls all the values back and then stores them into something like a arraylist and pushes the values back to the form... is this a good idea? or should i just forget about multi threading? I hope this isn't to muddled up and is clear enough for people to understand if you need further clarification lemme know... :) I know the human being and fish can coexist peacefully. -George Dubya Bush Don't believe me? http://politicalhumor.about.com/library/blbushism-fish.htm Yes thats right he actually said that...