Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i have an object that it have a 'Thread' property like this

C#
class Client
{
   public Thread ClientThread; // it is that i want to change
   //some property
   //some method 
}


how can i set the ClientThread in a started thread
like what i have tried

What I have tried:

C#
Thread tempThread = new Thread(new ThreadStart(delegate () {
    Client tempClient = null;
    tempClient = processClient(TempTcpClinet);
    if (tempClient != null)
    {
        tempClient.ClientThread = tempThread; //problem is here... cannot use tempThread
                                              //here... what should i do
    }
}));
tempThread.Start();

but i can not use this code
please tell me a soloution
Thanks alot..
Posted
Updated 21-May-16 5:12am
v3
Comments
Sergey Alexandrovich Kryukov 21-May-16 9:44am    
You cannot do it because the Thread member is private, but don't rush to change it. It all looks wrong. You certainly need to review the whole design. If you need help, you have to explain your ultimate goals.
—SA
Sergey Alexandrovich Kryukov 21-May-16 10:36am    
Very bad step.
—SA
Abtin Bina 21-May-16 10:35am    
i want to have access to this thread by object of this class

Sergey Alexandrovich Kryukov 21-May-16 10:37am    
Bad idea. What is the goal of it?
Look, it won't go anywhere unless you explain your ultimate goals. Try not to mix the goal with your ideas on how you want to achieve what you want.
—SA
Abtin Bina 21-May-16 10:38am    
i use some private methods in Client class that the methods should have access to this thread

1 solution

You can find some useful ideas in my past answers:
http://www.codeproject.com/Answers/536542/anplusamateurplusquestionplusinplussocketplusprogr#answer2[^],
http://www.codeproject.com/Answers/165297/Multple-clients-from-same-port-Number#answer1[^],
specifically on the problem of disconnection: http://www.codeproject.com/Answers/848979/How-Do-I-Get-To-Know-If-A-A-Tcp-Connection-Is-Over#answer1[^].

As you can see, one key idea is to have two threads on the service side, one for listening for new connection, another to network stream I/O itself. It solves all the problems. Pay special attention for my recommendation on thready synchronization.

I can see many concerns in the way you approach things. The whole idea to make some field accessible (non-private) is usually considered as abuse. This is not always so, but the adequate accessible member is a property, because it can help to encapsulate some processing, such as encapsulation. But it won't solve your problem, too. The key problem is the general use of threads. It is also related to your comment on Thread.Abort.

Many say that Thread.Abort is unsafe and should not be used. Usually, such advice comes from lack of understanding of what it does; such people associate it with really unacceptable TerminateThread. These things are very different. But the truth is: you only should use Abort if you understand what it really understand what you do, and not many understand it. I tried to explain things here:
Problem with creating Process from dedicated thread[^],
Close correcly the thread inside a dll[^].

Despite the difficulties related to understanding of thread abortion, I can suggest one way to use it really safely. It can be really safely done at the very end of your application runtime. The idea is: you create all the threads you need in the very beginning of your runtime and reuse them all the time. Roughly speaking, at the very end, you can abort a thread even if it is in some unknown state, and, at the very end, you have nothing to lose, at least for some class of applications, such as yours. My past answers should give you the idea:
Making Code Thread Safe[^],
Running exactly one job/thread/process in webservice that will never get terminated (asp.net)[^],
pause running thread[^],
ManualResetEvent and AutoResetEvent in Thread[^].

(I'm not trying to say that you need to use thread throttling in your application, that is, event wait handles; it is rather related to more general case. As to your application, my point is to avoid creation of threads but reusing the same fixed number of threads. Here is the thing: one common thread design mistake, nearly fatal one, is the attempt to create additional thread per socket; right design is using fixed number of threads, at most, one main thread plus two communication threads on the service side, and reuse the for all sockets.)

This concept couples well with the design of the thread wrapper I put forward on my other answers:
How to pass ref parameter to the thread[^],
Change parameters of thread (producer) after it is started[^] (encapsulation of thread synchronization is explained here),
MultiThreading in C#[^],
Classes in list updating in their own thread[^].

I would also add that an adequate form for the service part would be Windows Service:
Introduction to Windows Service Applications[^],
Walkthrough: Creating a Windows Service Application in the Component Designer[^].

It looks like you are trying to develop your application at the level of sockets, using TcpClient and TcpListener classes. It can be quite an adequate approach. I only want to remind you that it can be done on different levels, from sockets to "classical" remoting or WCF (in your case, most likely, self-hosted). Please see:
Communication b/w two Windows applications on LAN.[^],
how i can send byte[] to other pc[^].

—SA
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900