• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

Wher to put the global data?

I want a 2nd thread to create an array of size x and start filling it with data. But I want the array to be available to the rest of the program and stay around after my 2nd thread exits. And I want the array to fill as the data is generated. How would I go about doing that?

Here's a sample program outlining what I want to do:
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;

namespace AnonMeth
{
  public class Form1 : Form
  {
    // This thread is used to demonstrate thread-safe way
    // to call a Windows Forms control.
    private Thread demoThread = null;
    private TextBox textBox1;
    private Button setTextSafeBtn;

    public Form1()
    {
      InitializeComponent();
    }

    // This event handler creates a thread that calls a
    // Windows Forms control in a thread-safe way.
    private void setTextSafeBtn_Click(
        object sender,
        EventArgs e)
    {
      // Create a background thread and start it.
      this.demoThread = new Thread(new ThreadStart(this.ThreadProcSafe));
      this.demoThread.Start();

      // Continue in the main thread.  Set a textbox value
      // that will be overwritten by demoThread.
      textBox1.Text = "Written by the main thread.";
    }

    // If the calling thread is different from the thread that
    // created the TextBox control, this method passes in the
    // the SetText method to the SetTextCallback delegate and
    // passes in the delegate to the Invoke method.
    private void ThreadProcSafe()
    {
      // Wait two seconds to simulate some background work
      // being done.
      Thread.Sleep(2000);

      MethodInvoker mi = delegate()
      {
        string text = "Written by the background thread.";
        // Check if this method is running on a different thread
        // than the thread that created the control.
        this.textBox1.Text = text;
      };
      updateUI(mi);
      /*
       HERE I'LL GENERATE DATA WHICH I WANT AVAILABLE
       FOR THE REST OF THE PROGRAM. 
       CREATE AN ARRAY OF SIZE x 
       BUT I CAN'T CREATE THE ARRAY HERE, IT WOULDN'T STAY AROUND
       AFTER THIS THREAD EXITS.
       HOW DO I CREATE THE DATA ARRAY AND ADD DATA TO IT?
       */
    }

    private void updateUI(MethodInvoker mi)
    {
      if (this.InvokeRequired)
      {
        this.BeginInvoke(mi);
      }
      else
      {
        mi();
      }
    }

    #region Windows Form Designer generated code
    private void InitializeComponent()
    {
      this.textBox1 = new System.Windows.Forms.TextBox();
      this.setTextSafeBtn = new System.Windows.Forms.Button();
      this.SuspendLayout();
      //
      // textBox1
      //
      this.textBox1.Location = new System.Drawing.Point(12, 12);
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(360, 20);
      this.textBox1.TabIndex = 0;
      //
      // setTextSafeBtn
      //
      this.setTextSafeBtn.Location = new System.Drawing.Point(96, 55);
      this.setTextSafeBtn.Name = "setTextSafeBtn";
      this.setTextSafeBtn.TabIndex = 2;
      this.setTextSafeBtn.Text = "Safe Call";
      this.setTextSafeBtn.Click += new System.EventHandler(this.setTextSafeBtn_Click);
      //
      // Form1
      //
      this.ClientSize = new System.Drawing.Size(388, 96);
      this.Controls.Add(this.setTextSafeBtn);
      this.Controls.Add(this.textBox1);
      this.Name = "Form1";
      this.Text = "Form1";
      this.ResumeLayout(false);
      this.PerformLayout();
    }
    #endregion

    [STAThread]
    static void Main()
    {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }
  }
}

Open in new window


(Follow-up to question http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_27030667.html)
0
deleyd
Asked:
deleyd
5 Solutions
 
dj_alikCommented:
You can use static Array member.
0
 
saraganiCommented:
You will need to define the array in the scope of your class.
You will also need to lock the array from access by 2 different threads on the same time.

do this by defining in the class scope an object objLock = new object()

and then

lock (objLock)
{
  // Your code here
}
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here's an example of a static member in a class as dj_alik suggests:

    public class Data
    {
        public static List<int> numbers = new List<int>();
    }

Now, from anywhere, you can access the List like this:

        private void button1_Click(object sender, EventArgs e)
        {
            Data.numbers.Add(411);
        }
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
deleydAuthor Commented:
Are static arrays defined at compile time? Because I need to dynamically allocate the array after I determine the size of the array I need:
      int x = 100;
      string[,] mystrarray = new string[x,9];

Open in new window

The only thing I can think of is use delegates or Anonymous Methods, send x back to the primary thread so primary thread creates,... no wait, that's for which thread does the work, I want to send x back to primary class so array is created in primary class so it's still around for rest of program.

I'm old school. I'm probably not thinking about this correctly in new OOP terms.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can set the size of the static array at run-time from anywhere:

    public class Data
    {
        public static string[,] mystrarray;
    }

Then, from somewhere:

            int x = 100;
            Data.mystrarray = new string[x, 9];
0
 
saraganiCommented:
Static property means that all instances of the same class have that specific property located at the same memory address. All of them are pointing to the same object
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now