-
December 15th, 2010, 09:48 AM
#1
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.
-
December 15th, 2010, 02:31 PM
#2
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.
-
December 15th, 2010, 03:08 PM
#3
Re: Problem calling a method of an object maded in a thread.
Originally Posted by krosty4782
Edit: Solved, need to define the object outside the run method
Originally Posted by Arjay
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
-
December 15th, 2010, 06:41 PM
#4
Re: Problem calling a method of an object maded in a thread.
Originally Posted by memeloo
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|