Hello, Newsgroupians:
I'm a little confused when it comes to threads in C#. Here's what I'm
trying to do...
I have a List<containing a set of MyPoint(s)...
public struct MyPoint
{
int x;
int y;
int val;
}
class MyClass
{
List<MyPointm_list = new List<MyPoint>();
...
}
Now, in my main thread I'm going to populate the list. Assume the
following...
class MyClass
{
public MyClass()
{
for (int i = 0; i < 10; i++)
{
MyPoint pt = new MyPoint;
pt.x = <Some X>
pt.y = <Some Y>
// pt.val is changed in the thread
m_list.Add(pt);
}
}
...
}
Now, in the secondary thread, I am going to be changing .val
For simplicity, assume I do the following...
class MyClass
{
void SomeFunc()
{
Thread th = new Thread(new ThreadStart(this.ThreadFunc));
th.Start();
while (th.IsAlive) // Wait until the secondary thread is done
{
Thread.Sleep(1000);
}
for (int i = 0; i < m_list.Count; i++)
{
Console.WriteLine(m_list[i].val);
}
}
public void ThreadFunc()
{
Random r = new Random();
for (int i = 0; i < this.m_list.Count; i++)
{
MyPoint pt = this.m_list[i];
pt.val = r.Next();
}
...
}
}
Yet, even after waiting for the secondary thread to end, the .val for each
MyPoint in the List<is always zero. I don't understand why it isn't
changing. I've tried setting some variables to static; this didn't work.
Overall, what do I need to do to make it so the secondary thread can write
to the values in the List<and have the main thread read these values once
the thread has ended? Thank you.
Trecius