CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2010
    Posts
    3

    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);
    }

  2. #2
    Join Date
    Jan 2011
    Location
    Worthing, UK
    Posts
    3

    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.

  3. #3
    Join Date
    Aug 2010
    Posts
    3

    Re: How to reference an object out of scope of thread

    thanks. that helps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured