Solved

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

Posted on 2010-08-12
6
499 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 16

Assisted Solution

by:Vikram Singh Saini
Vikram Singh Saini earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Please cancel request to close.
I must have hit wrong button.

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

13 Experts available now in Live!

Get 1:1 Help Now