473,395 Members | 1,496 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.

Thread class member access from thread function

Hi,

I just wonder if someone can help me with this:
Consider this simple piece of code:

// ============ START CODE ==============================
internal class EncThread: Thread
{
public string WaveFileName;
}

void EncThreadFunction()
{
<...>
}

void SomeOtherFunction()
{
EncThread et = new EncThread(new ThreadStart(EncThreadFunction));
et.WaveFileName = "Blah.wav";
et.Start();
}
// ============ END CODE ==============================

In "SomeOtherFunction" i start a new thread which executes "EncThreadFunction".
So here's my question - with given structure is there a way to get a value of et.WaveFileName in the thread function "EncThreadFunction"?

I don't see any and think that i need to encapsulate the whole thing into a class, including the function,
but for 10 reasons i'd like to leave it as it is - working function should be in the main class body.

Any suggestions are appreciated!

Thank you
Andrey
Nov 16 '05 #1
8 1866
Andrey,

You can jsut access it using this.WaveFileName in the EncThreadFunction.
Since you are setting the field on that instance, your method should be able
to read/write from it.

I don't know that I agree with extending from thread, as the class has
little to do with the actual thread mechanics, it is just executed from the
Thread. However, that is more of a personal preference, as what you have
will work.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"MuZZy" <le*******@yahoo.com> wrote in message
news:2d********************@rcn.net...
Hi,

I just wonder if someone can help me with this:
Consider this simple piece of code:

// ============ START CODE ==============================
internal class EncThread: Thread
{
public string WaveFileName;
}

void EncThreadFunction()
{
<...>
}

void SomeOtherFunction()
{
EncThread et = new EncThread(new ThreadStart(EncThreadFunction));
et.WaveFileName = "Blah.wav";
et.Start();
}
// ============ END CODE ==============================

In "SomeOtherFunction" i start a new thread which executes
"EncThreadFunction".
So here's my question - with given structure is there a way to get a value
of et.WaveFileName in the thread function "EncThreadFunction"?

I don't see any and think that i need to encapsulate the whole thing into
a class, including the function,
but for 10 reasons i'd like to leave it as it is - working function should
be in the main class body.

Any suggestions are appreciated!

Thank you
Andrey

Nov 16 '05 #2
Nicholas Paldino [.NET/C# MVP] wrote:
Andrey,

You can jsut access it using this.WaveFileName in the EncThreadFunction.
Since you are setting the field on that instance, your method should be able
to read/write from it.

I don't know that I agree with extending from thread, as the class has
little to do with the actual thread mechanics, it is just executed from the
Thread. However, that is more of a personal preference, as what you have
will work.

Hope this helps.


Well, how could it work if WaveFileName is the EncThread class field, but EncThreadFunction method is main class' member,
so "this" inside "EncThreadFunction" would point to main class, not to EncThread, which is internal class for the main class...

Or am i missing something?

Thank you,
Andrey
Nov 16 '05 #3
Andrey,

The way you had it laid out, it looked like the EncThreadFunction would
be on the EncThread class.

I would do it this way:

public class EncFunction
{
public string WaveFileName;

public function EncThreadFunction()
{
// Do something.
}
}

Then, just create an instance of this class, set the wave file name, and
then pass the EncThreadFunction of that instance to the thread to have it
run.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"MuZZy" <le*******@yahoo.com> wrote in message
news:a4********************@rcn.net...
Nicholas Paldino [.NET/C# MVP] wrote:
Andrey,

You can jsut access it using this.WaveFileName in the
EncThreadFunction. Since you are setting the field on that instance, your
method should be able to read/write from it.

I don't know that I agree with extending from thread, as the class
has little to do with the actual thread mechanics, it is just executed
from the Thread. However, that is more of a personal preference, as what
you have will work.

Hope this helps.


Well, how could it work if WaveFileName is the EncThread class field, but
EncThreadFunction method is main class' member,
so "this" inside "EncThreadFunction" would point to main class, not to
EncThread, which is internal class for the main class...

Or am i missing something?

Thank you,
Andrey

Nov 16 '05 #4
Nicholas Paldino [.NET/C# MVP] wrote:
Andrey,

The way you had it laid out, it looked like the EncThreadFunction would
be on the EncThread class.

I would do it this way:

public class EncFunction
{
public string WaveFileName;

public function EncThreadFunction()
{
// Do something.
}
}

Then, just create an instance of this class, set the wave file name, and
then pass the EncThreadFunction of that instance to the thread to have it
run.


Got ya!
Will give it a try now

Thank you,
Andrey
Nov 16 '05 #5
MuZZy <le*******@yahoo.com> wrote:
I just wonder if someone can help me with this:
Consider this simple piece of code:

// ============ START CODE ==============================
internal class EncThread: Thread
{
public string WaveFileName;
}
First problem: the Thread class is sealed. What does your actual code
look like?
Second problem: public member variables are not a good idea.
void EncThreadFunction()
{
<...>
}

void SomeOtherFunction()
{
EncThread et = new EncThread(new ThreadStart(EncThreadFunction));
et.WaveFileName = "Blah.wav";
et.Start();
}
// ============ END CODE ==============================

In "SomeOtherFunction" i start a new thread which executes "EncThreadFunction".
So here's my question - with given structure is there a way to get a
value of et.WaveFileName in the thread function "EncThreadFunction"?


Not that I can see - but that's partly because your class is supposedly
deriving from Thread, which it can't actually do to start with...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Jon Skeet [C# MVP] wrote:
MuZZy <le*******@yahoo.com> wrote:
I just wonder if someone can help me with this:
Consider this simple piece of code:

// ============ START CODE ==============================
internal class EncThread: Thread
{
public string WaveFileName;
}

First problem: the Thread class is sealed. What does your actual code
look like?


Sealed? Then it's weird, because following didn't give any compile error

internal class EncThread: Thread
{
<...>
}

I already omitted this though by creating a class for the thread function as Nicholas advised

Thank you
Andrey
Second problem: public member variables are not a good idea.

void EncThreadFunction()
{
<...>
}

void SomeOtherFunction()
{
EncThread et = new EncThread(new ThreadStart(EncThreadFunction));
et.WaveFileName = "Blah.wav";
et.Start();
}
// ============ END CODE ==============================

In "SomeOtherFunction" i start a new thread which executes "EncThreadFunction".
So here's my question - with given structure is there a way to get a
value of et.WaveFileName in the thread function "EncThreadFunction"?

Not that I can see - but that's partly because your class is supposedly
deriving from Thread, which it can't actually do to start with...

Nov 16 '05 #7
MuZZy <le*******@yahoo.com> wrote:
First problem: the Thread class is sealed. What does your actual code
look like?


Sealed? Then it's weird, because following didn't give any compile error

internal class EncThread: Thread
{
<...>
}


That's very strange, given that the code below produces the error
below. Are you sure that the Thread class you're using above is
System.Threading.Thread?

using System.Threading;

internal class EncThread : Thread
{
}

Test.cs(3,16): error CS0509: 'EncThread' : cannot inherit from sealed
class
'System.Threading.Thread'
c:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\mscor lib.dll: (Location of
symbol related to previous error)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
Jon Skeet [C# MVP] wrote:
MuZZy <le*******@yahoo.com> wrote:
First problem: the Thread class is sealed. What does your actual code
look like?


Sealed? Then it's weird, because following didn't give any compile error

internal class EncThread: Thread
{
<...>
}

That's very strange, given that the code below produces the error
below. Are you sure that the Thread class you're using above is
System.Threading.Thread?

using System.Threading;

internal class EncThread : Thread
{
}

Test.cs(3,16): error CS0509: 'EncThread' : cannot inherit from sealed
class
'System.Threading.Thread'
c:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\mscor lib.dll: (Location of
symbol related to previous error)

Yeah, i've just tried and i got an error too.. Maybe i've messed something before...
Anyway, now i use a different approach

Thank you,
Andrey
Nov 16 '05 #9

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

Similar topics

9
by: baumann | last post by:
hi all, to implement a singleton class, one has define a static function in the singleton class, class A{ public: static A* getInstance() { static A a; return &a;
5
by: Bill Davidson | last post by:
Hello All: I've got a question about synchronization requiremements in a C# worker thread procedure that, among other things, sinks events from outside sources. I realize the worker thread will...
12
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
23
by: Boltar | last post by:
Hi I'm writing a threading class using posix threads on unix with each thread being run by an object instance. One thing I'm not sure about is , if I do the following: myclass::~myclass() {...
3
by: NaeiKinDus | last post by:
Hello, i'm trying to program a thread that would be locked (by a mutex) and that would only be unlocked once that a function (generating data) is done. The purpose is to generate data, and unlock...
4
by: Vajkay.Rene | last post by:
Hi Last week I got faced to a curious problem when try to access the member pointers of my class in the worker thread. In main thread everything is ok, but in worker thread when I try to access...
6
by: Olumide | last post by:
Hi - I've got a class that contains static member functions alone, all of whose arguments are passed by reference as shown below: class MySpiffyClass{ // no constructor, destructor or...
3
by: aine_canby | last post by:
I have a class which has a member which takes a long time to populate. Some of this member is serialised with an xml file, and some is established through a set of commands issued on the system....
18
by: J.K. Baltzersen | last post by:
To whomever it may concern: I am using MS Visual C++ 6.0. I have a process A which instantiates an object C. At a later point the process A creates the thread B. The thread B has access...
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
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
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
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...
0
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...

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.