Thread class member access from thread function

  • Thread starter Thread starter MuZZy
  • Start date Start date
M

MuZZy

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
 
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 said:
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
 
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 said:
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
 
MuZZy said:
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 said:
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
 
MuZZy said:
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\mscorlib.dll: (Location of
symbol related to previous error)
 
Jon said:
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\mscorlib.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
 
Back
Top