Thread updating UI from other Classes...

G

GTi

I have a UI Class with StatusBarPanels
Then I have another Class that is a thread.
I want this class thread to update the StatusBarPanels in the UI class.

But I get the error:
error CS0120: An object reference is required for the nonstatic field,
method, or property myNameSpace.MainForm.SetStatusBarText(string)'

Please help a old C# newbie...


Short version:
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.StatusBarPanel statusBarPanel1;
private System.Windows.Forms.StatusBarPanel statusBarPanel2;
private System.Windows.Forms.StatusBarPanel statusBarPanel3;

private void MainForm_Load(object sender, System.EventArgs e)
{
thread1= new MyThread();
}

public void SetStatusBarText(string txt)
{
if (InvokeRequired)
{
BeginInvoke(new StringParameterDelegate(SetVekt1StatusBarText), new
object[]{txt});
return;
}
this.statusBarPanel1.Text = txt;
}

}



public class MyThread
{
Thread myThread;
public MyThread()
{
ThreadStart threadEntry= new ThreadStart(this.beginThread);
myThread= new Thread(threadEntry);
myThread.Start();
}

public void beginComliThread()
{

int a=0;
while(true)
{
Thread.Sleep(500);
MainForm.SetStatusBarText(++a);
}
 
G

GTi

Cordell Lawrence said:
This article covers you concern and then some. For you problem
specifically
check out the section on "Threads and Controls".

http://msdn.microsoft.com/msdnmag/issues/03/02/Multithreading/

May the force be with you...

HTH
Cordell Lawrence
Teleios Systems Ltd.

GTi said:
I have a UI Class with StatusBarPanels
Then I have another Class that is a thread.
I want this class thread to update the StatusBarPanels in the UI class.

But I get the error:
error CS0120: An object reference is required for the nonstatic field,
method, or property myNameSpace.MainForm.SetStatusBarText(string)'

Please help a old C# newbie...


Short version:
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.StatusBarPanel statusBarPanel1;
private System.Windows.Forms.StatusBarPanel statusBarPanel2;
private System.Windows.Forms.StatusBarPanel statusBarPanel3;

private void MainForm_Load(object sender, System.EventArgs e)
{
thread1= new MyThread();
}

public void SetStatusBarText(string txt)
{
if (InvokeRequired)
{
BeginInvoke(new StringParameterDelegate(SetVekt1StatusBarText), new
object[]{txt});
return;
}
this.statusBarPanel1.Text = txt;
}

}



public class MyThread
{
Thread myThread;
public MyThread()
{
ThreadStart threadEntry= new ThreadStart(this.beginThread);
myThread= new Thread(threadEntry);
myThread.Start();
}

public void beginComliThread()
{

int a=0;
while(true)
{
Thread.Sleep(500);
MainForm.SetStatusBarText(++a);
}


http://msdn.microsoft.com/msdnmag/issues/03/02/Multithreading/
I did read this article several times and I still not understand it.
Can you help me out here...
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You have to make the instance of MainForm accesible from MyThread, you can
either pass the reference of it to the MyThread instance or make it
accesible globaly ( static )

cheers,
 
G

GTi

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

You have to make the instance of MainForm accesible from MyThread, you
can either pass the reference of it to the MyThread instance or make it
accesible globaly ( static )

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



GTi said:
I have a UI Class with StatusBarPanels
Then I have another Class that is a thread.
I want this class thread to update the StatusBarPanels in the UI class.

But I get the error:
error CS0120: An object reference is required for the nonstatic field,
method, or property myNameSpace.MainForm.SetStatusBarText(string)'

Please help a old C# newbie...


Short version:
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.StatusBarPanel statusBarPanel1;
private System.Windows.Forms.StatusBarPanel statusBarPanel2;
private System.Windows.Forms.StatusBarPanel statusBarPanel3;

private void MainForm_Load(object sender, System.EventArgs e)
{
thread1= new MyThread();
}

public void SetStatusBarText(string txt)
{
if (InvokeRequired)
{
BeginInvoke(new StringParameterDelegate(SetVekt1StatusBarText), new
object[]{txt});
return;
}
this.statusBarPanel1.Text = txt;
}

}



public class MyThread
{
Thread myThread;
public MyThread()
{
ThreadStart threadEntry= new ThreadStart(this.beginThread);
myThread= new Thread(threadEntry);
myThread.Start();
}

public void beginComliThread()
{

int a=0;
while(true)
{
Thread.Sleep(500);
MainForm.SetStatusBarText(++a);
}

Ok if I change it to:
public static void SetStatusBarText(string txt)
{
if(InvokeRequired)
{
BeginInvoke(new StringParameterDelegate(SetStatusBarText), new
object[]{txt});
return;
}
statusBarPanel1.Text = txt;
}

I got this error:
error CS0026: Keyword this is not valid in a static property, static method,
or static field initializer

I want to figure this out, but need some help....
(newbee)
 
W

Wavemaker

:

Ok if I change it to:
public static void SetStatusBarText(string txt)
{
if(InvokeRequired)
{
BeginInvoke(new StringParameterDelegate(SetStatusBarText), new
object[]{txt});
return;
}

statusBarPanel1.Text = txt;
}

I got this error:
error CS0026: Keyword this is not valid in a static property, static
method, or static field initializer

A couple of problems. InvokeRequired is not a static property, but
you're trying to access it from a static method. Same goes for the
statusBarPanel1 object.

The key to understanding the problem here is to understand the
difference between class methods/properties/etc. and instance
methods/properties/etc. If I make a method static, it exists at the
class level. If it's not static, it exists at the instance level. To
give an example, say I have a class with the following declaration:

public class ClassWithStaticMethod
{
public static void SomeMethod()
{
}
}

This method can be accessible at the class level, so all I have to do to
invoke it is use the class name:

ClassWithStaticMethod.SomeMethod();

This is a class with an instance level method:

public class ClassWithInstanceMethod
{
public void SomeMethod()
{
}
}

In order to invoke this method, I need an instance of the class (i.e. an
object):

ClassWithInstanceMethod instance = new ClassWithInstanceMethod();

Now I can invoke the method through the instance:

instance.SomeMethod();

So it's important to grasp the different between static and not static
methods/properties/etc.

The problem with your code as it was initially is that in the thread
method, you were trying to access an instance level method through the
class name. Since the method is not static, you got an error. Then you
made the method a static method as above, but then inside the static
method you're trying to access instance level properties and variables.

So something has to give. You could pass an instance of the form to the
thread class and have the thread call the method in the form. You would
not do it by using the class name, but through the form object itself:

someForm.SetStatusBarText(++a);

This isn't necessarily good design, but I would go ahead and play with
this until you understand static vs. non-static methods/properties/etc.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top