如何在该线程中设置线程句柄 [英] How can I set thread handle in that thread

查看:67
本文介绍了如何在该线程中设置线程句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象,它有一个像这样的'Thread'属性



  class 客户端
{
public Thread ClientThread; // 我想要更改
// 某些属性
// 某种方法
}





如何在启动的线程中设置ClientThread

就像我尝试过的那样



我尝试过的事情:



线程tempThread = 线程( new  ThreadStart(< span class =code-keyword> delegate (){
客户端tempClient = null ;
tempClient = processClient(TempTcpClinet) ;
if (tempClient!= null
{
tempClient .ClientThread = tempThread; // 问题在这里......不能使用tempThread
// 这里......应该怎么做我
}
}));
tempThread.Start();



但我无法使用此代码

请告诉我一个解决方案

非常感谢..

解决方案

你可以在我过去的答案中找到一些有用的想法:

http://www.codeproject.com/Answers/536542/anplusamateurplusquestionplusinplussocketplusprogr#answer2 [ ^ ],

http://www.codeproject.com/Answers/165297/Multple-clients-from-same-port-Number#answer1 [ ^ ],

专门针对这个问题断开连接: http ://www.codeproject.com/Answers/848979/How-Do-I-Get-To-Know-If-AA-Tcp-Connection-Is-Over#answer1 [ ^ ]。



正如你所看到的,一个关键的想法是在服务端有两个线程,一个用于监听新连接,另一个用于网络流I / O本身。它解决了所有问题。特别注意我对同步的建议。



我可以看到你处理事情的方式有很多问题。使一些字段可访问(非私人)的整个想法通常被视为滥用。这并非总是如此,但适当的可访问成员是属性,因为它可以帮助封装某些处理,例如封装。但它也不能解决你的问题。关键问题是线程的一般使用。它也与你对​​ Thread.Abort 的评论有关。



许多人说 Thread.Abort 是不安全的,不应该使用。通常,这样的建议来自于缺乏对其作用的理解;这样的人将它与真正不可接受的 TerminateThread 联系起来。这些事情非常不同。但事实是:你只应该使用 Abort 如果你明白它真正理解你做了什么,并且没有多少人理解它。我试着在这里解释一下:

从专用线程创建流程的问题 [ ^ ],

正确关闭dll中的线程 [ ^ ]。



尽管在理解线程流产方面遇到了困难,但我可以建议一种方法来安全地使用它。它可以在应用程序运行时的最后安全地完成。我们的想法是:在运行时的最开始创建所需的所有线程,并始终重用它们。粗略地说,在最后,即使它处于某种未知状态,你也可以中止一个线程,并且最后,你没有任何损失,至少对于某些类别的应用程序,例如你的。我过去的答案应该给你一个想法:

制作代码线程安全 [ ^ ],

在webservice中运行一个永远不会终止的作业/线程/进程(asp.net) [ ^ ],

暂停正在运行的主题 [ ^ ],

线程中的ManualResetEvent和AutoResetEvent [ ^ ]。



(我不是说你需要使用线程限制在您的应用程序中,即事件等待句柄;它与更一般的情况有关。至于你的应用程序,我的观点是避免创建线程但重用相同的固定数量的线程。事情是这样的:一个常见的线程设计错误,几乎是致命的错误,是每个插槽创建额外线程的尝试;正确的设计是使用固定数量的线程,最多一个主线程加上服务端的两个通信线程,并重用所有套接字。)



这个概念与线程包装器的设计完美结合我提出了我的其他答案:

如何将ref参数传递给线程 [ ^ ],

更改线程(生产者)启动后的参数 [ ^ ](这里解释了线程同步的封装),

C#中的多线程 [ ^ ],

列表更新中的类在他们自己的线程中 [ ^ ]。



我还要补充说,服务部分的适当形式是 Windows服务

Windows服务应用程序简介 [ ^ ],

演练:在组件设计器中创建Windows服务应用程序 [ ^ ]。



看起来你好像是尝试使用 TcpClient TcpListener 类在套接字级别开发应用程序。这可能是一个非常合适的方法。我只想提醒你,它可以在不同的级别上完成,从套接字到经典远程处理或WCF(在你的情况下,很可能是自托管)。请参阅:

沟通b / w局域网上的两个Windows应用程序。 [ ^ ],

我如何将byte []发送到其他电脑 [ ^ ]。



< DD> -SA

i have an object that it have a 'Thread' property like this

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:

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..

解决方案

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


这篇关于如何在该线程中设置线程句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆