-
December 30th, 2010, 04:24 PM
#1
How to reference an object out of scope of thread
I don't know how to word this except by example. Here's a little psuedo-code, I need help with the part starting with // ********
class MyCollection
{
void Init() {}
void Add(item) {}
bool Check(item) {}
}
void main()
{
MyCollection myCollection;
myCollection.Init();
new Thread(Listen).Start();
}
static void Listen()
{
// wait for request - I have this part already
// **************************
// I NEED HELP WITH THIS PART
// How do I refer back to myCollection
// so I can do something like:
myCollection.Check(item);
// and
myCollection.Add(item);
}
-
January 1st, 2011, 01:15 PM
#2
Re: How to reference an object out of scope of thread
Hello,
Have you looked a ParameterizedThreadStart?
here is an example using your code:
class Program
{
static void Main(string[] args)
{
MyCollection myCollection = new MyCollection();
myCollection.Init();
Thread thread = new Thread(new ParameterizedThreadStart(Listen));
thread.Start(myCollection);
}
static void Listen(object argument)
{
MyCollection myCollection = argument as MyCollection;
myCollection.Check("something...");
}
}
class MyCollection
{
public void Init() { ;}
public void Add(object item) { ;}
public bool Check(object item)
{
System.Windows.Forms.MessageBox.Show("Test");
Thread.Sleep(1000);
return true ;
}
}
Valery.
-
January 4th, 2011, 09:17 AM
#3
Re: How to reference an object out of scope of thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|