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:
Quote:
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
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.
Re: Problem calling a method of an object maded in a thread.
Quote:
Originally Posted by
krosty4782
Edit: Solved, need to define the object outside the run method
Quote:
Originally Posted by
Arjay
The simple solution is to make the 'socketConectado' variable a field of the form class.
I got ya :D
Re: Problem calling a method of an object maded in a thread.
Quote:
Originally Posted by
memeloo
I got ya :D
You might be interested in knowing that I answered before noticing the OP's edit, but thanks for pointing that out anyway. :thumb: