473,727 Members | 2,034 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 6331
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
4914
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 using.  I'm using something like this: //Other code setting things up and updating GUI //Variables...
20
3028
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 progress meter increase just fine when the thread is running. When I select Start, I enable the Cancel...
14
4912
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 messages to it's window because, this window is very busy updating it's view, hence because it's...
7
10437
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
12525
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 PropertyChanged event: class Person : INotifyPropertyChanged {
3
1128
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 accessed after the first time it is unable to access the datagrid (Cannot clear this list)? I...
14
6888
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)); WorkerThread.IsBackground = true; WorkerThread.Start(); The problem I am having is...I cannot seem...
5
3024
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 are two recommended ways to handle this situation. 1. Use Invoke() to "call" an updater method...
8
2983
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 to a couple of programming friends, and just chewing on it in my brain. I think I have an ok handle...
0
8752
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9406
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9185
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9121
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8106
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6703
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4521
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4786
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3229
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.