Solved

C# separate array data to process another thread. Within same class? or not?

Posted on 2010-08-12
6
526 Views
Last Modified: 2012-05-10
Hi,
Stuck on how to do the following.

I'm collecting data and have it in array and can copy to another array within same class
BUT if I want to have separate thread to process on the copy of the array how do design that?

Array.Copy( x,x,x,x,x) doesn't see the array outside the class.
And if the array copy is in the class with original array, I cannot figure out how to have a separate thread to proccess while data is still streaming to original array.
Thanks.
I've looked at Monitor and locks etc.
0
Comment
Question by:SSschultz
  • 3
  • 2
6 Comments
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33423828
private void Form1_Load(object sender, EventArgs e)
        {
         
            progressBar1.Maximum = 100;
            progressBar1.Minimum = 0;
            progressBar1.Step = 1;

            toolStripStatusLabel1.Text = "Connecting to the file";
           
            System.Threading.Thread.Sleep(1000);
            textBox1.Text = "Deleting Old Data from temp file.";

           // here try to instantiate a new array where you are going to copy let it be temp array.
           
            System.Threading.Thread.Sleep(1000);
                       
           // copy the values into the temp array.
            System.Threading.Thread.Sleep(20000);
                       
           
            if (progressBar1.Value == progressBar1.Maximum)
            {
                progressBar1.Value = progressBar1.Minimum;
            }
            for (int i = progressBar1.Minimum; i <= progressBar1.Maximum; i++)
            {
                progressBar1.PerformStep();
            }
           
          // insert the values from temp array to where you want to store.

Note: because as you have created a temp array you should not wait for each and every array to get copied. Here you can just copy the first one into temp them 2nd thread will copy that from temp to mail. In the mean time a new array will be loaded on to temp. Something like this. Hope it helps.

Good Luck.
0
 

Author Comment

by:SSschultz
ID: 33423940
Thanks.  I'm not sure that's what I'm looking for.
I was able to but thread to sleep before you answered and did find that the stream collected the data while thread was sleeping.

But, I want to make a quick copy of array (or temp as you listed) to another class or within the class but so the original thread collecting data is running most of the time and quick copy to another array and then process off the data in the buffer/array.

You mentioned it but I'm a newbie and need actual code example of how to copy array to another thread/class for processing.
I know how to copy an array within the class. Reason I'm asking outside the orignal class is I don't know how else to have 2 threads running at same time execpt for the quick copy to second array and class?
Thanks
0
 
LVL 18

Accepted Solution

by:
Anil Golamari earned 300 total points
ID: 33424378
So you want a code which will use multi threading to copy the contents from one arrary to other in the same class?
If you just want to copy an array from one to other then you can simply use
C#:
ArrayList s = new ArrayList();
ArrayList p = new ArrayList();
          int i = 20;
 Array.Copy(s, p, 20);

vb:
Dim s As Array
Dim d As Array
Dim length As Integer
Array.Copy(s, d, length)

http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/0e7e3c2a-7c15-4442-b73a-6e6eb1e5a883

The above link shows you how to access arrary from other classes.

Good Luck.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 16

Assisted Solution

by:Vikram Singh Saini
Vikram Singh Saini earned 200 total points
ID: 33428146
Hi,

Please confirm that your requirements are as follows:

You have two array named A1 and A2. You want to get your A1 fill with some stream of data, and along you need to Copy those values from A1 to A2. Copying those values should be done in separate thread (be it any reason).

And if above are not your requirements let us know again with clear description.

However check the following code, might it help you to gain insight about your requirements.

There are two buttons and two richtextbox on the form.

Regards,
V.S.Saini

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Threading;

namespace Threads_Array
{
    public partial class Form1 : Form
    {
        static int[] items;
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(StartT));
            thread.Start();
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {           
            CopyData();
        }

        public void StartT()
        {
            items = new int[1000];
            for (int i = 0; i < 1000; i++)
            {
                items[i] = i;
                richTextBox1.Text += items[i].ToString() + "| ";
            }
        }

        // This thread procedure performs the task.
        public  void CopyData()
        {
            int[] Numbers = new int[1000];
            Array.Copy(items, Numbers, 1000);
            for (int i = 0; i < 1000; i++)
            {
                this.richTextBox2.Text += items[i].ToString() + "/";
            }
        }

    }
}

Open in new window

0
 

Author Comment

by:SSschultz
ID: 33431742
Thanks for everyones help.
Last night I was able to get arrays copied as wanted, and today have the threads processing correctly.

Specific question was how to do this outside to different classes. Lucky85's link was sort of helpfully,
but the answer I solved last night was to use public static byte to use across classes.
0
 

Author Comment

by:SSschultz
ID: 33433060
Please cancel request to close.
I must have hit wrong button.

Attempted to award solution points experts who commented.
thanks.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Question! 4 39
Sent email using Shared Mailbox from outlook in C# 5 50
FTP file download using c# 3 25
Call Controller Action Method from ASPX 2 22
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question