Click here to Skip to main content
15,889,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i want write program for sum array with system.threading.thread

there is a pic File show how i want to do it :
pic Array Sum


i write it but give me error :
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RezaTreads
{
    public partial class frmTread : Form
    {
        public frmTread()
        {
            InitializeComponent();
        }
        int[] A ={1,2,3,4,5,6,7,8};
        int id = -1;
        
        Task ts;
        System.Threading.Thread d;
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                //ts = new Task(()=>doaction(ts,A));
                for (int i = 1; i < 3; i++)
                {
                    id++;
                    for (int j =0; j < 4/i; j++)
                    {
                        d = new System.Threading.Thread(() => doaction(j, A,id));
                        
                        d.Start();
                        if (d.ThreadState == System.Threading.ThreadState.Stopped)
                            d.Suspend();
                    }
                    for (int k = 0; k < 8; k++)
                    {
                        textBox2.Text += A[k].ToString();
                    }
                }
                textBox2.Text += "*result: ";
                for (int k = 0; k < 10; k++)
                {
                    textBox2.Text += A[k].ToString();
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        }
// erro is here ====================================================================
        private static void doaction(int j,int[] A,int index)
        {
            
            try
            {
                int id = getid(j,index);
                
                if ((2*(id+1)) % 2 == 0)
                {

                    A[((2 * (id + 1)) - 1)] = A[(2 * (id + 1)) - 1] + A[(2 * (id + 1))- 2-index];
                    
                        
                }
               
            }
            catch (Exception)
            {

                throw;
            }
           
        }
//==================================================================
        private static int getid(int j,int index)
        {
            int id=0;
            switch (index)
            {
                case 0: id=j; break;
                case 1: switch (j)
                    {
                        case 0: id = 1; break;
                        case 1: id = 3; break;
                    }break;
                case 2: id = 3; break;
                    
                    
           
            }
            return id;
        }

      
    }
}

error is :
{"Index was outside the bounds of the array."}
Posted
Updated 21-Nov-13 3:56am
v5
Comments
OriginalGriff 21-Nov-13 2:25am    
First off, I'm not going to a random site to download an unknown file to read your homework instructions. If we need the information, copy and paste it into the question.
Secondly, don't show your whole code - just the relevant fragment. It makes it easier for us to work out what part of the code you are talkign about.
Thirdly, if you get an error, ***then tell us what it is***
"i write it but give me error :"
Tells us nothing:
Is it a run time error? If so, what's the message? Where it is? When? do you have to do anything specific to cause it?
Is it a compiler error? If so, what's the message? Where?
Is it a logic error? If so, what is it doing that you didn't expect, or not doing that you did?

Use the "Improve question" widget to edit your question and provide better information.
Help us to help you!
Er Daljeet Singh 21-Nov-13 2:47am    
what you want to do exactly.
you want to get the sum of all the element in array using threading or you have some different scenario.
NorouziFar 21-Nov-13 10:00am    
tank you

i update my post and upload a pic for information

if you simply want to the sum of array using thread then use this.

C#
int[] A = new int[] { 1, 2, 3, 4, 5 };
           int a = 0;
           Thread t = new Thread(() =>
           {
               a = A.Sum();
           });
           t.Start();
           t.Join();
           MessageBox.Show(a.ToString());
 
Share this answer
 
Comments
NorouziFar 21-Nov-13 3:47am    
yes is True ;But I want to show the sum of the action array in the array.
See my post in word file placed
Er Daljeet Singh 21-Nov-13 4:22am    
try to put content of you doc file here in codeproject
NorouziFar 21-Nov-13 10:00am    
tank you

i update my post and upload a pic for information
Looks like a "normal" index misscalculation, most likely on line

C#
A[((2 * (id + 1)) - 1)] = A[(2 * (id + 1)) - 1] + A[(2 * (id + 1))- 2-index];


But:
* Don't you know what this (very common) error message means?
* Can't you use debugger or tracing to work out the error?

Btw. This is what happens if you(or me):
don't use "good" names for your variables and methods.
do all your calculations inline (hard to debug)
don't use build in "goodies" like LINQ to make your life easier.

I think you may find the bug for yourself, but maybe consider another aproach:
First get the subset from the array (value list) you want to sum, then calculate sum with built-in function.
Avoid index calculation and "crazy" indexing math. For shure you know what you have written there, but this can become a maintenance nightmare...

just my 2c..
 
Share this answer
 
v2
Comments
NorouziFar 21-Nov-13 16:02pm    
i try to solve error but i cant ,I'm so sad because I can not solve this problem


Do you have another way to solve this problem using thread? ?
johannesnestler 22-Nov-13 1:32am    
Hi NorouziFar,

I don't think your problem is related to threads. The error Messages (index out of bounds) tells you that you want to access an element (index) which is either negative or not higher than Array.Length - 1. Normally I'd read through it and maybe spot it quickly, but in this case I don't feel the Need to figure out what you meant with k, A and doaction. This gives me no context, of course I see what you do but I think you should be able to debug it for yourself. If thread is keeping you from easy Debugging, try it without threading and have a look where you miss calculating your index (you should quickly get the line where the error occurs, - then break up your "big line" to smaller ones - make an indexCalculated variable and have a look with the Debugger which value it has... Sorry if I bother you with pretty basic stuff, but this kind of error is normally found quickly with Debugging...
NorouziFar 23-Nov-13 3:15am    
Tank you for your help i change my code :
for (int j =0; j < 4/i; j++)
{

d = new System.Threading.Thread(() => doaction(j, A,id));
d.Start();
d.Join();


}

and it work properly

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900