473,395 Members | 1,393 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,395 software developers and data experts.

Threads - what's going wrong

Hi

This is probably simple, but can anyone explain why this won't work:

public void SomeMethod()
{
//Do stuff
}

ThreadStart myThreadDelegate = new ThreadStart(SomeMethod);
Thread myThread = new Thread(myThreadDelegate);

It gives me a "A field initializer cannot reference the nonstatic
field, method, or property..." error.

If I make the SomeMethod static, then it works properly. But if the
method is static then it can't access other non-static methods from
the class, so that's no good.

I realise that I'm not getting what's happening here because I know
virtually nothing about threads!

Thanks.

Jerry
Nov 15 '05 #1
2 1236
"Jerry S" <je***@philosophers.co.uk> wrote in message
news:mf********************************@4ax.com...
This is probably simple, but can anyone explain why this won't work:
ThreadStart myThreadDelegate = new ThreadStart(SomeMethod);
Thread myThread = new Thread(myThreadDelegate);

It gives me a "A field initializer cannot reference the nonstatic
field, method, or property..." error.


Where did you place that "ThreadStart myThreadDelegate = ..." code? I think
you've placed it in a static method and then the exception makes sense.
Remember that static methods cannot access instance methods without an
object reference.

Here's what you can do:

public class MyClass {
public void SomeMethod() {
//Do stuff
}
public void Start() {
ThreadStart myThreadDelegate = new ThreadStart(SomeMethod);
Thread myThread = new Thread(myThreadDelegate);
}
public static void Main() {
MyClass mc = new MyClass();
mc.Start();
}
}

Regards,
Pieter Philippaerts
Managed SSL/TLS: http://www.mentalis.org/go.php?sl
Nov 15 '05 #2
Thanks!

That's sorted it.

Regards,

Jerry

On Sat, 25 Oct 2003 19:07:29 +0200, "Pieter Philippaerts"
<Pi****@nospam.mentalis.org> wrote:

Where did you place that "ThreadStart myThreadDelegate = ..." code? I think
you've placed it in a static method and then the exception makes sense.
Remember that static methods cannot access instance methods without an
object reference.

Here's what you can do:

public class MyClass {
public void SomeMethod() {
//Do stuff
}
public void Start() {
ThreadStart myThreadDelegate = new ThreadStart(SomeMethod);
Thread myThread = new Thread(myThreadDelegate);
}
public static void Main() {
MyClass mc = new MyClass();
mc.Start();
}
}

Regards,
Pieter Philippaerts
Managed SSL/TLS: http://www.mentalis.org/go.php?sl


Nov 15 '05 #3

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

Similar topics

8
by: andrewpalumbo | last post by:
I'm trying to write some code which will split up a vector into two halves and run a method on the objects in the vector using two seperate threads. I was hoping to see a near linear speedup on an...
8
by: Alban Hertroys | last post by:
Hello, I'm using psycopg to insert records in a number of threads. After the threads finish, another thread runs to collect the inserted data. Now, my problem is that psycopg let's my threads...
1
by: Leonard J. Reder | last post by:
Hello Mark, I took your three day course here at JPL and recall that you said something was wrong with the implementation of threads within Python but I cannot recall what. So what is wrong...
28
by: Dennis Owens | last post by:
I am trying to run a thread off of a form, and every once in a while the thread will raise an event for the form to read. When the form gets the event, the form will place the event into a dataset...
0
by: Philip Greer | last post by:
I am getting the following issue with a first threads program I've written: root@aixsys1:/usr/local/bin # ./threadtest.pl Thread 1 with 0 and 10 to do something... Can't call method "tid"...
22
by: Jeff Louie | last post by:
Well I wonder if my old brain can handle threading. Dose this code look reasonable. Regards, Jeff using System; using System.Diagnostics; using System.IO; using System.Threading;
2
by: Brett | last post by:
Let's say some one makes the argument that instead of multi threading an application, they say it's better just to make multiple applications. The app does the same thing for different modules. ...
6
by: RahimAsif | last post by:
Hi guys, I would like some advice on thread programming using C#. I am writing an application that communicates with a panel over ethernet, collects data and writes it to a file. The way the...
5
by: PH | last post by:
Hi guys; I got a single processor computer, running an application that launches 2 threads. Each of these threads listens for incoming connections in a specific port, so there is a Loop ....
167
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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...
0
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...

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.