Solved

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

Posted on 2010-08-12
6
536 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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