Click here to Skip to main content
15,887,822 members
Home / Discussions / C#
   

C#

 
AnswerRe: Writing source code in seperate files Pin
BobJanova28-Jan-12 6:32
BobJanova28-Jan-12 6:32 
QuestionSimple MultiThread question Pin
PozzaVecia27-Jan-12 20:51
PozzaVecia27-Jan-12 20:51 
AnswerRe: Simple MultiThread question Pin
OriginalGriff27-Jan-12 21:14
mveOriginalGriff27-Jan-12 21:14 
GeneralRe: Simple MultiThread question Pin
PozzaVecia28-Jan-12 1:21
PozzaVecia28-Jan-12 1:21 
GeneralRe: Simple MultiThread question Pin
OriginalGriff28-Jan-12 2:41
mveOriginalGriff28-Jan-12 2:41 
AnswerRe: Simple MultiThread question Pin
Luc Pattyn27-Jan-12 22:22
sitebuilderLuc Pattyn27-Jan-12 22:22 
GeneralRe: Simple MultiThread question Pin
PozzaVecia28-Jan-12 1:56
PozzaVecia28-Jan-12 1:56 
AnswerRe: Simple MultiThread question Pin
Luc Pattyn28-Jan-12 2:29
sitebuilderLuc Pattyn28-Jan-12 2:29 
There are dozens of ways to get a total result from the work of several threads. I'll list just a few:

1.
have a single variable "double total" accessible by all thread methods involved; now let each threaded method add its result to it. This is a situation where you need a lock (you shouldn't allow multiple threads to read and write the total value at exactly the same time).

2.
have a single queue accessible by all threads; each threads pushes its result to the queue; when all is done (after the join point), the main thread pops everything from the queue and calculates the sum. As the typical queue isn't thread-safe, here too you'll need a lock.

2bis.
like 2, but using some synchronization means to have yet another thread calculate total. See e.g. ManualResetEvent type.

3.
much simpler: have a little job class, which is intended to hold all the input parameters and the results of each thread. Instantiate one instance for each job/thread, have the thread get its values from there, and store its results in there as well. Also maintain a collection of these jobs, so the totalizer can enumerate the jobs and accumulate their results.

4.
often my preference: like #3, however put the actual job code also inside the job class, which means all threading code is encapsulated there too. Here is an example:

class Job {
    private int parm1;
    private int parm2;
    public int Result;
    private Thread thread;

    public Job(int parm1, int parm2) {
        this.parm1=parm1;
        this.parm2=parm2;
    }

    public void Run() {
        thread=new Thread(new ThreadStart(runner));
        thread.Start();
    }

    public void Join() {
        thread.Join();
    }

    private void runner() {
        Result=parm1+parm2;
    }
}


class Test {
    public static Main() {
        List<Job> jobs=new List<Job>();
        for(int i=0; i<10; i++) {
            Job job=new Job(3*i, i+12);
            jobs.Add(job);
            job.Run();
        }
        foreach(Job job in jobs) job.Join();
        int total=0;
        foreach(Job job in jobs) total+=job.Result;
    }
}


I suggest you read up on all the classes/types I mentioned, and better yet, read about threading in your C# book.

Smile | :)
Luc Pattyn [My Articles] Nil Volentibus Arduum

Fed up by FireFox memory leaks I switched to Opera and now CP doesn't perform its paste magic, so links will not be offered. Sorry.

GeneralRe: Simple MultiThread question Pin
harold aptroot28-Jan-12 2:24
harold aptroot28-Jan-12 2:24 
AnswerRe: Simple MultiThread question Pin
Luc Pattyn28-Jan-12 2:50
sitebuilderLuc Pattyn28-Jan-12 2:50 
AnswerRe: Simple MultiThread question Pin
BobJanova28-Jan-12 6:45
BobJanova28-Jan-12 6:45 
GeneralRe: Simple MultiThread question Pin
PozzaVecia28-Jan-12 9:50
PozzaVecia28-Jan-12 9:50 
QuestionConverting MySqlDateTime Pin
Ian_urquhart27-Jan-12 4:50
Ian_urquhart27-Jan-12 4:50 
AnswerRe: Converting MySqlDateTime Pin
Bernhard Hiller27-Jan-12 5:01
Bernhard Hiller27-Jan-12 5:01 
GeneralRe: Converting MySqlDateTime Pin
Ian_urquhart27-Jan-12 5:27
Ian_urquhart27-Jan-12 5:27 
AnswerRe: Converting MySqlDateTime Pin
PIEBALDconsult27-Jan-12 5:08
mvePIEBALDconsult27-Jan-12 5:08 
GeneralRe: Converting MySqlDateTime Pin
Ian_urquhart27-Jan-12 5:31
Ian_urquhart27-Jan-12 5:31 
GeneralRe: Converting MySqlDateTime Pin
Richard Andrew x6427-Jan-12 16:43
professionalRichard Andrew x6427-Jan-12 16:43 
GeneralRe: Converting MySqlDateTime Pin
PIEBALDconsult28-Jan-12 4:03
mvePIEBALDconsult28-Jan-12 4:03 
GeneralRe: Converting MySqlDateTime Pin
jschell28-Jan-12 4:39
jschell28-Jan-12 4:39 
GeneralRe: Converting MySqlDateTime Pin
PIEBALDconsult28-Jan-12 12:05
mvePIEBALDconsult28-Jan-12 12:05 
GeneralRe: Converting MySqlDateTime Pin
jschell28-Jan-12 13:54
jschell28-Jan-12 13:54 
QuestionSocket server handling MySQL queries from clients and return the response to them Pin
Islorvat27-Jan-12 4:48
Islorvat27-Jan-12 4:48 
AnswerRe: Socket server handling MySQL queries from clients and return the response to them Pin
Richard Andrew x6427-Jan-12 7:30
professionalRichard Andrew x6427-Jan-12 7:30 
AnswerRe: Socket server handling MySQL queries from clients and return the response to them Pin
SledgeHammer0127-Jan-12 13:25
SledgeHammer0127-Jan-12 13:25 

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.