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

    Problem calling a method of an object maded in a thread.

    Hi guys.

    All in FORM1

    Code:
    public partial class Form1 : Form
        {  
    Thread thread;
     
    public Form1()
            {
    thread = new Thread(run);
                thread.Start();   
            }
    public void run()
            {
              ConectadoThread socketConectado = new ConectadoThread(); 
            }
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
     
            {    
     
                socketConectado.close();
     
            }
     
     }
    Why when i do "socketConectado.close()" it shows me:


    Error 2 The name 'socketConectado' does not exist in the current context C:\Users\Mauri\Desktop\Proyectos\Visual Studio 2008\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Form1.cs
    Thanks =)

    Edit: Solved, need to define the object outside the run method
    Last edited by krosty4782; December 15th, 2010 at 10:48 AM.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Problem calling a method of an object maded in a thread.

    The 'socketConectado' variable is a local variable that only has scope within the Run() method.

    The error you get is because 'socketConectado' isn't defined in the context of the 'Form1_FormClosing' handler.

    The simple solution is to make the 'socketConectado' variable a field of the form class.

  3. #3
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Problem calling a method of an object maded in a thread.

    Quote Originally Posted by krosty4782 View Post
    Edit: Solved, need to define the object outside the run method
    Quote Originally Posted by Arjay View Post
    The simple solution is to make the 'socketConectado' variable a field of the form class.
    I got ya
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Problem calling a method of an object maded in a thread.

    Quote Originally Posted by memeloo View Post
    I got ya
    You might be interested in knowing that I answered before noticing the OP's edit, but thanks for pointing that out anyway.

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