Click here to Skip to main content
15,898,938 members
Home / Discussions / C#
   

C#

 
AnswerRe: Question about Chart in C# Pin
Luc Pattyn29-Jan-12 8:35
sitebuilderLuc Pattyn29-Jan-12 8:35 
GeneralRe: Question about Chart in C# Pin
Luc Pattyn28-Jan-12 22:12
sitebuilderLuc Pattyn28-Jan-12 22:12 
GeneralRe: Question about Chart in C# Pin
pongpanut28-Jan-12 22:23
pongpanut28-Jan-12 22:23 
QuestionWriting source code in seperate files Pin
Fred 3427-Jan-12 22:27
Fred 3427-Jan-12 22:27 
AnswerRe: Writing source code in seperate files Pin
Richard MacCutchan27-Jan-12 22:37
mveRichard MacCutchan27-Jan-12 22:37 
GeneralRe: Writing source code in seperate files Pin
Eddy Vluggen27-Jan-12 23:44
professionalEddy Vluggen27-Jan-12 23:44 
GeneralRe: Writing source code in seperate files Pin
Richard MacCutchan28-Jan-12 0:19
mveRichard MacCutchan28-Jan-12 0:19 
GeneralRe: Writing source code in seperate files Pin
Eddy Vluggen28-Jan-12 5:19
professionalEddy Vluggen28-Jan-12 5:19 
GeneralRe: Writing source code in seperate files Pin
Richard MacCutchan28-Jan-12 5:26
mveRichard MacCutchan28-Jan-12 5:26 
GeneralRe: Writing source code in seperate files Pin
AmitGajjar28-Jan-12 21:35
professionalAmitGajjar28-Jan-12 21:35 
GeneralRe: Writing source code in seperate files Pin
Richard MacCutchan28-Jan-12 22:06
mveRichard MacCutchan28-Jan-12 22:06 
GeneralRe: Writing source code in seperate files Pin
Luc Pattyn28-Jan-12 22:16
sitebuilderLuc Pattyn28-Jan-12 22:16 
AnswerRe: Writing source code in seperate files Pin
Dean Oliver27-Jan-12 22:38
Dean Oliver27-Jan-12 22:38 
AnswerRe: Writing source code in seperate files Pin
jschell28-Jan-12 4:35
jschell28-Jan-12 4:35 
GeneralRe: Writing source code in seperate files Pin
PIEBALDconsult28-Jan-12 13:34
mvePIEBALDconsult28-Jan-12 13:34 
AnswerRe: Writing source code in seperate files PinPopular
Eddy Vluggen28-Jan-12 5:29
professionalEddy Vluggen28-Jan-12 5:29 
GeneralRe: Writing source code in seperate files Pin
PIEBALDconsult28-Jan-12 13:37
mvePIEBALDconsult28-Jan-12 13:37 
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.

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.