Click here to Skip to main content
15,891,943 members
Home / Discussions / C#
   

C#

 
AnswerRe: Permutations/Combinations Pin
Luc Pattyn3-Jun-07 2:06
sitebuilderLuc Pattyn3-Jun-07 2:06 
QuestionHow to split work into multiple threads. Pin
kripzz2-Jun-07 19:50
kripzz2-Jun-07 19:50 
AnswerRe: How to split work into multiple threads. Pin
Expert Coming2-Jun-07 20:14
Expert Coming2-Jun-07 20:14 
GeneralRe: How to split work into multiple threads. Pin
kripzz2-Jun-07 20:25
kripzz2-Jun-07 20:25 
GeneralRe: How to split work into multiple threads. Pin
Expert Coming2-Jun-07 20:29
Expert Coming2-Jun-07 20:29 
AnswerRe: How to split work into multiple threads. Pin
Christian Graus2-Jun-07 20:32
protectorChristian Graus2-Jun-07 20:32 
AnswerRe: How to split work into multiple threads. Pin
Luc Pattyn3-Jun-07 2:34
sitebuilderLuc Pattyn3-Jun-07 2:34 
AnswerRe: How to split work into multiple threads. Pin
Robert Rohde3-Jun-07 2:57
Robert Rohde3-Jun-07 2:57 
Hi,

as said before I wouldn't use that much threads. Second I wouldn't assign each thread its tasks from the beginning. It may be that some of your list elements need more time than others (especially true for pings) so each thread should fetch the next element whenever it needs to.
I would start by creating some sort of queue class which holds all your tasks:
public class MyQueue {
   private string[] _elements;
   private int _counter;
   public MyQueue(string[] elements) {
      _elements = elements;
      _counter = 0;
   }
   public string GetNextElement() {
      lock (_elements) {
          if (_counter >= _elements.Length)
             return null;
          string result = _elements[_counter];
          counter++;
          return result;
      }
   }
}

Now define a function:
private void DoStuffInThread(object parameter) {
   MyQueue queue = (MyQueue)parameter;
   string nextElement;
   while ((nextElement = queue.GetNextElement()) != null) {
      //do something with nextElement
   }
}

And let the threading begin:
Thread[] threads = new Thread[numberOfDesiredThreads];
for (int i = 0; i < threads.Length; i++) {
   threads[i] = new Thread(new ParameterizedThreadStart(DoStuffInThread));
   threads[i].Start();
}

You have to add your processing logic, assignment of the results to the listview and also check when all threads are finished. Keep in mind that you should never change control properties directly from a different thread. Instead use Control.Invoke to make it safe.

Robert



GeneralRe: How to split work into multiple threads. Pin
kripzz11-Jun-07 16:39
kripzz11-Jun-07 16:39 
AnswerRe: How to split work into multiple threads. Pin
Hesham Yassin6-Jun-07 8:32
Hesham Yassin6-Jun-07 8:32 
QuestionHmmm why won't this work? Pin
mfkr2-Jun-07 17:58
mfkr2-Jun-07 17:58 
AnswerRe: Hmmm why won't this work? Pin
Christian Graus2-Jun-07 19:07
protectorChristian Graus2-Jun-07 19:07 
GeneralRe: Hmmm why won't this work? Pin
mfkr2-Jun-07 19:17
mfkr2-Jun-07 19:17 
GeneralRe: Hmmm why won't this work? Pin
Christian Graus2-Jun-07 19:22
protectorChristian Graus2-Jun-07 19:22 
AnswerRe: Hmmm why won't this work? Pin
Robert Surtees2-Jun-07 19:24
Robert Surtees2-Jun-07 19:24 
GeneralRe: Hmmm why won't this work? Pin
mfkr2-Jun-07 19:32
mfkr2-Jun-07 19:32 
GeneralRe: Hmmm why won't this work? Pin
Christian Graus2-Jun-07 20:33
protectorChristian Graus2-Jun-07 20:33 
GeneralRe: Hmmm why won't this work? Pin
Dave Kreskowiak3-Jun-07 5:07
mveDave Kreskowiak3-Jun-07 5:07 
QuestionCasting generic parameters to int Pin
Jack Valmadre2-Jun-07 15:19
Jack Valmadre2-Jun-07 15:19 
AnswerRe: Casting generic parameters to int Pin
mikker_1232-Jun-07 16:29
mikker_1232-Jun-07 16:29 
GeneralRe: Casting generic parameters to int Pin
Jack Valmadre2-Jun-07 17:27
Jack Valmadre2-Jun-07 17:27 
GeneralRe: Casting generic parameters to int Pin
Daniel Grunwald3-Jun-07 2:13
Daniel Grunwald3-Jun-07 2:13 
GeneralRe: Casting generic parameters to int Pin
mikker_1233-Jun-07 4:37
mikker_1233-Jun-07 4:37 
GeneralRe: Casting generic parameters to int Pin
Jack Valmadre3-Jun-07 10:34
Jack Valmadre3-Jun-07 10:34 
QuestionGetting to the encapsulated class within a ServiceHost object? Pin
LongRange.Shooter2-Jun-07 11:36
LongRange.Shooter2-Jun-07 11:36 

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.