Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-08-12
6
Medium Priority
?
547 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 1200 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 16

Assisted Solution

by:Vikram Singh Saini
Vikram Singh Saini earned 800 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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

705 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