Solved

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

Posted on 2010-08-12
6
509 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now