473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Thread updating UI from other Classes...

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


Nov 17 '05 #1
8 6314
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/is...ultithreading/

May the force be with you...

HTH
Cordell Lawrence
Teleios Systems Ltd.

"GTi" <gt*@gti.com> wrote in message news:42********@news.wineasy.se...
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);
}

Nov 17 '05 #2
GTi
"Cordell Lawrence" <cl*******@teleios-systems.com> skrev i melding
news:u8**************@TK2MSFTNGP10.phx.gbl...
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/is...ultithreading/

May the force be with you...

HTH
Cordell Lawrence
Teleios Systems Ltd.

"GTi" <gt*@gti.com> wrote in message news:42********@news.wineasy.se...
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/is...ultithreading/
I did read this article several times and I still not understand it.
Can you help me out here...
Nov 17 '05 #3
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" <gt*@gti.com> wrote in message news:42********@news.wineasy.se...
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);
}

Nov 17 '05 #4
GTi
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> skrev
i melding news:eN****************@TK2MSFTNGP15.phx.gbl...
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" <gt*@gti.com> wrote in message news:42********@news.wineasy.se...
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)


Nov 17 '05 #5

"GTi" wrote:

<snip>
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.
Nov 17 '05 #6

"GTi" wrote:

<snip>
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.
Nov 17 '05 #7
GTi <bl****@blabla.com> wrote:
http://msdn.microsoft.com/msdnmag/is...ultithreading/
I did read this article several times and I still not understand it.
Can you help me out here...


Try http://www.pobox.com/~skeet/csharp/t...winforms.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
GTi <bl****@blabla.com> wrote:
http://msdn.microsoft.com/msdnmag/is...ultithreading/
I did read this article several times and I still not understand it.
Can you help me out here...


Try http://www.pobox.com/~skeet/csharp/t...winforms.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Hal Vaughan | last post by:
First, I am aware of both SwingUtilities.invokeLater(), and of using Thread to create a new thread.  These are part of the problem. I want to have something running in the background, while the...
20
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a...
14
by: Brian Keating EI9FXB | last post by:
I wonder can anyone reccomment a solution to this problem. Let me explain, I've services running on my system, my application receives diagnostic messages from these services, what i want to do...
7
by: Joe | last post by:
I need a control to always run in a separate thread from the application. I'm not too sure where to begin with this since the control could be dropped on the form at design time. One possible...
5
by: Mark R. Dawson | last post by:
Hi all, I may be missing something with how databinding works but I have bound a datasource to a control and everything is great, the control updates to reflect the state of my datasource when I...
3
by: Tlink | last post by:
I have a master control program that needs to run continuously, accepting data and updating a datagrid for the user. As a result I have setup 2 threads, one accepts and processes incoming requests...
14
by: joey.powell | last post by:
I am using VS2005 for a windows forms application. I need to be able to use a worker thread function to offload some processing from the UI thread. The worker thread will need access to a...
5
by: temp2 | last post by:
Hello, I have an app that reads data params from a stream and updates controls accordingly. The stream reader is on a different thread than the main thread that created the controls. I fully...
8
by: Brad Walton | last post by:
Hello. First post, but been doing a bit of reading here. I am working on a project in Java, but decided to switch over to C# after seeing some of the additional features I can get from C#. One of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.