Find a thread referance my name

  • Thread starter Thread starter Joshua Russell
  • Start date Start date
J

Joshua Russell

Hi,
is there a function wherby I can find a find a thread by its name. For
example, I want to get hold of a class referance for a thread named "My
Thread". I woudl like some code that looks like this...

ClassName myClass = Thread.FindByName("My Thread");

Basicaly I want to be able to do things like (presuming that myClass is a
form): myClass.Text = "New Title";

Anyone know how I can do this?

Thanx Josh
 
Joshua,

If you find a thread by name, then a Thread instance is going to be
returned to you (btw, you will have to cycle through all available threads,
and check the thread name as you iterate through it).

Unless you are using thread-local storage, you shouldn't care where you
get the reference from (which thread you are on). You just have to make
sure that the object can handle calls from different threads (or that calls
get marshaled correctly to the correct thread in the case of objects which
have thread-affinity, e.g. the Control class).

Hope this helps.
 
Hi Joshua,

Threads are not guarantee to have name thus, it is not possible to have such
a method.
In addition to what Nicholas said I would add only that WindowsForms
controls are bound to threads for the current implementation of .NET. It may
not be true for the future versions of windows.

Anyways look at System.Diagnostics.Process class.
You may find Process.Threads and Process.MainWindowHandle interesting.

If you explain in more details what you are trying to do we may come up with
some other solution for your poroblem.
 
Basically, I'm writing an MSN like chat client.
On each incoming chat request a new thread is created which in turn connects
to the server specified by the incoming request. If a conversation is open
for a specific user from a previous conversation I do not want another
thread to be started. I want to reuse the current thread. If a new request
comes in specifying a new server I will of course need to tell the thread to
disconnect from the old server and connect to the new server.
On an incoming request I will need some way of checking whether or not a
conversation exists already and if one does, some way of returning an object
reference.

Any ideas?

Thanx Josh

Stoitcho Goutsev (100) said:
Hi Joshua,

Threads are not guarantee to have name thus, it is not possible to have such
a method.
In addition to what Nicholas said I would add only that WindowsForms
controls are bound to threads for the current implementation of .NET. It may
not be true for the future versions of windows.

Anyways look at System.Diagnostics.Process class.
You may find Process.Threads and Process.MainWindowHandle interesting.

If you explain in more details what you are trying to do we may come up with
some other solution for your poroblem.

--
B\rgds
100 [C# MVP]

Joshua Russell said:
Hi,
is there a function wherby I can find a find a thread by its name. For
example, I want to get hold of a class referance for a thread named "My
Thread". I woudl like some code that looks like this...

ClassName myClass = Thread.FindByName("My Thread");

Basicaly I want to be able to do things like (presuming that myClass is a
form): myClass.Text = "New Title";

Anyone know how I can do this?

Thanx Josh
 
Hi Joshua,
Isn't it better if you keep a map between users and threads. Hashtable will
do or you may go for your own collection based on DictionaryBase. You can
use the user ID as a key in the dictionary. When conversaion is started for
the first time you add a new entry in the dictionary.

In this case Process class won't help because ProcessThreads property
returns collection of ProcessThread objects, which are equivalent to the
"physical" window threads. .NET threads (Thread class) at the other hand are
platform independent objects. One "physical" thread may execute more then
one .NET threads. Currently there is one to one mappings but it won't be
like that in future.
--
B\rgds
100 [C# MVP]

Joshua Russell said:
Basically, I'm writing an MSN like chat client.
On each incoming chat request a new thread is created which in turn connects
to the server specified by the incoming request. If a conversation is open
for a specific user from a previous conversation I do not want another
thread to be started. I want to reuse the current thread. If a new request
comes in specifying a new server I will of course need to tell the thread to
disconnect from the old server and connect to the new server.
On an incoming request I will need some way of checking whether or not a
conversation exists already and if one does, some way of returning an object
reference.

Any ideas?

Thanx Josh

Stoitcho Goutsev (100) said:
Hi Joshua,

Threads are not guarantee to have name thus, it is not possible to have such
a method.
In addition to what Nicholas said I would add only that WindowsForms
controls are bound to threads for the current implementation of .NET. It may
not be true for the future versions of windows.

Anyways look at System.Diagnostics.Process class.
You may find Process.Threads and Process.MainWindowHandle interesting.

If you explain in more details what you are trying to do we may come up with
some other solution for your poroblem.

--
B\rgds
100 [C# MVP]

Joshua Russell said:
Hi,
is there a function wherby I can find a find a thread by its name. For
example, I want to get hold of a class referance for a thread named "My
Thread". I woudl like some code that looks like this...

ClassName myClass = Thread.FindByName("My Thread");

Basicaly I want to be able to do things like (presuming that myClass
is
 
Hi,
I don't quite understand what you are suggesting here. Can you please give
me an example of some real world code.

Thanx Josh


Stoitcho Goutsev (100) said:
Hi Joshua,
Isn't it better if you keep a map between users and threads. Hashtable will
do or you may go for your own collection based on DictionaryBase. You can
use the user ID as a key in the dictionary. When conversaion is started for
the first time you add a new entry in the dictionary.

In this case Process class won't help because ProcessThreads property
returns collection of ProcessThread objects, which are equivalent to the
"physical" window threads. .NET threads (Thread class) at the other hand are
platform independent objects. One "physical" thread may execute more then
one .NET threads. Currently there is one to one mappings but it won't be
like that in future.
--
B\rgds
100 [C# MVP]

Joshua Russell said:
Basically, I'm writing an MSN like chat client.
On each incoming chat request a new thread is created which in turn connects
to the server specified by the incoming request. If a conversation is open
for a specific user from a previous conversation I do not want another
thread to be started. I want to reuse the current thread. If a new request
comes in specifying a new server I will of course need to tell the
thread
to
disconnect from the old server and connect to the new server.
On an incoming request I will need some way of checking whether or not a
conversation exists already and if one does, some way of returning an object
reference.

Any ideas?

Thanx Josh

Stoitcho Goutsev (100) said:
Hi Joshua,

Threads are not guarantee to have name thus, it is not possible to
have
such
a method.
In addition to what Nicholas said I would add only that WindowsForms
controls are bound to threads for the current implementation of .NET.
It
may
not be true for the future versions of windows.

Anyways look at System.Diagnostics.Process class.
You may find Process.Threads and Process.MainWindowHandle interesting.

If you explain in more details what you are trying to do we may come
up
with
some other solution for your poroblem.

--
B\rgds
100 [C# MVP]

"Joshua Russell" <joshATojmyster|DOTukDOTTeuDOOTorgNOSPAM.PLZ> wrote in
message Hi,
is there a function wherby I can find a find a thread by its name. For
example, I want to get hold of a class referance for a thread named "My
Thread". I woudl like some code that looks like this...

ClassName myClass = Thread.FindByName("My Thread");

Basicaly I want to be able to do things like (presuming that myClass
is
a
form): myClass.Text = "New Title";

Anyone know how I can do this?

Thanx Josh
 
Back
Top