473,569 Members | 2,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Mai nForm.SetStatus BarText(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.StatusBar Panel statusBarPanel1 ;
private System.Windows. Forms.StatusBar Panel statusBarPanel2 ;
private System.Windows. Forms.StatusBar Panel statusBarPanel3 ;

private void MainForm_Load(o bject sender, System.EventArg s e)
{
thread1= new MyThread();
}

public void SetStatusBarTex t(string txt)
{
if (InvokeRequired )
{
BeginInvoke(new StringParameter Delegate(SetVek t1StatusBarText ), new
object[]{txt});
return;
}
this.statusBarP anel1.Text = txt;
}

}

public class MyThread
{
Thread myThread;
public MyThread()
{
ThreadStart threadEntry= new ThreadStart(thi s.beginThread);
myThread= new Thread(threadEn try);
myThread.Start( );
}

public void beginComliThrea d()
{

int a=0;
while(true)
{
Thread.Sleep(50 0);
MainForm.SetSta tusBarText(++a) ;
}


Nov 17 '05 #1
8 6322
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.s e...
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.Mai nForm.SetStatus BarText(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.StatusBar Panel statusBarPanel1 ;
private System.Windows. Forms.StatusBar Panel statusBarPanel2 ;
private System.Windows. Forms.StatusBar Panel statusBarPanel3 ;

private void MainForm_Load(o bject sender, System.EventArg s e)
{
thread1= new MyThread();
}

public void SetStatusBarTex t(string txt)
{
if (InvokeRequired )
{
BeginInvoke(new StringParameter Delegate(SetVek t1StatusBarText ), new
object[]{txt});
return;
}
this.statusBarP anel1.Text = txt;
}

}

public class MyThread
{
Thread myThread;
public MyThread()
{
ThreadStart threadEntry= new ThreadStart(thi s.beginThread);
myThread= new Thread(threadEn try);
myThread.Start( );
}

public void beginComliThrea d()
{

int a=0;
while(true)
{
Thread.Sleep(50 0);
MainForm.SetSta tusBarText(++a) ;
}

Nov 17 '05 #2
GTi
"Cordell Lawrence" <cl*******@tele ios-systems.com> skrev i melding
news:u8******** ******@TK2MSFTN GP10.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.s e...
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.Mai nForm.SetStatus BarText(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.StatusBar Panel statusBarPanel1 ;
private System.Windows. Forms.StatusBar Panel statusBarPanel2 ;
private System.Windows. Forms.StatusBar Panel statusBarPanel3 ;

private void MainForm_Load(o bject sender, System.EventArg s e)
{
thread1= new MyThread();
}

public void SetStatusBarTex t(string txt)
{
if (InvokeRequired )
{
BeginInvoke(new StringParameter Delegate(SetVek t1StatusBarText ), new
object[]{txt});
return;
}
this.statusBarP anel1.Text = txt;
}

}

public class MyThread
{
Thread myThread;
public MyThread()
{
ThreadStart threadEntry= new ThreadStart(thi s.beginThread);
myThread= new Thread(threadEn try);
myThread.Start( );
}

public void beginComliThrea d()
{

int a=0;
while(true)
{
Thread.Sleep(50 0);
MainForm.SetSta tusBarText(++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.s e...
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.Mai nForm.SetStatus BarText(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.StatusBar Panel statusBarPanel1 ;
private System.Windows. Forms.StatusBar Panel statusBarPanel2 ;
private System.Windows. Forms.StatusBar Panel statusBarPanel3 ;

private void MainForm_Load(o bject sender, System.EventArg s e)
{
thread1= new MyThread();
}

public void SetStatusBarTex t(string txt)
{
if (InvokeRequired )
{
BeginInvoke(new StringParameter Delegate(SetVek t1StatusBarText ), new
object[]{txt});
return;
}
this.statusBarP anel1.Text = txt;
}

}

public class MyThread
{
Thread myThread;
public MyThread()
{
ThreadStart threadEntry= new ThreadStart(thi s.beginThread);
myThread= new Thread(threadEn try);
myThread.Start( );
}

public void beginComliThrea d()
{

int a=0;
while(true)
{
Thread.Sleep(50 0);
MainForm.SetSta tusBarText(++a) ;
}

Nov 17 '05 #4
GTi
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > skrev
i melding news:eN******** ********@TK2MSF TNGP15.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.s e...
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.Mai nForm.SetStatus BarText(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.StatusBar Panel statusBarPanel1 ;
private System.Windows. Forms.StatusBar Panel statusBarPanel2 ;
private System.Windows. Forms.StatusBar Panel statusBarPanel3 ;

private void MainForm_Load(o bject sender, System.EventArg s e)
{
thread1= new MyThread();
}

public void SetStatusBarTex t(string txt)
{
if (InvokeRequired )
{
BeginInvoke(new StringParameter Delegate(SetVek t1StatusBarText ), new
object[]{txt});
return;
}
this.statusBarP anel1.Text = txt;
}

}

public class MyThread
{
Thread myThread;
public MyThread()
{
ThreadStart threadEntry= new ThreadStart(thi s.beginThread);
myThread= new Thread(threadEn try);
myThread.Start( );
}

public void beginComliThrea d()
{

int a=0;
while(true)
{
Thread.Sleep(50 0);
MainForm.SetSta tusBarText(++a) ;
}


Ok if I change it to:
public static void SetStatusBarTex t(string txt)
{
if(InvokeRequir ed)
{
BeginInvoke(new StringParameter Delegate(SetSta tusBarText), 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 SetStatusBarTex t(string txt)
{
if(InvokeRequir ed)
{
BeginInvoke(new StringParameter Delegate(SetSta tusBarText), 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 ClassWithStatic Method
{
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:

ClassWithStatic Method.SomeMeth od();

This is a class with an instance level method:

public class ClassWithInstan ceMethod
{
public void SomeMethod()
{
}
}

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

ClassWithInstan ceMethod instance = new ClassWithInstan ceMethod();

Now I can invoke the method through the instance:

instance.SomeMe thod();

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.SetSta tusBarText(++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 SetStatusBarTex t(string txt)
{
if(InvokeRequir ed)
{
BeginInvoke(new StringParameter Delegate(SetSta tusBarText), 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 ClassWithStatic Method
{
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:

ClassWithStatic Method.SomeMeth od();

This is a class with an instance level method:

public class ClassWithInstan ceMethod
{
public void SomeMethod()
{
}
}

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

ClassWithInstan ceMethod instance = new ClassWithInstan ceMethod();

Now I can invoke the method through the instance:

instance.SomeMe thod();

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.SetSta tusBarText(++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.co m>
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.co m>
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
4898
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 GUI is updating.  I've done that before without a problem, however, now, I need to pass variables to the separate Thread or Runnable that I'm...
20
2998
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 delegate inside the UI that I call to update the progress meter. I use the Suspend() and Abort() methods based on button events. I can watch the...
14
4898
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 is create an MDIChild window for each service that is sending messages. Ok so far so good, but I get problems if one service is constantly sending...
7
10429
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 idea I had was to wrap my control in another control which creates the thread and than creates the control. Any suggestions? -Joe
5
12502
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 update the datasource - awesome, but I have an issue with updating from a different thread. Here is my datasource, a person class that raises the...
3
1123
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 and updates the database, whilst the other thread is started once a update has been committed to the database. The problem is when the datagrid is...
14
6858
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 datagridview on the form. I am using the following code to spawn the worker thread... Thread WorkerThread = new Thread(new ThreadStart(WT_MyFunction));...
5
3016
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 realize it's not wise, and in some circumstances, not even possible to update the controls from the stream other than the main stream. I believe there...
8
2967
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 the big changes I want to make is event-driven code (rather than the linear flow I had in Java). I have spent a week or so searching Google, talking...
0
7698
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8122
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.