c#.net中的线程问题 [英] threading question in c#.net

查看:83
本文介绍了c#.net中的线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们...



我正在开发聊天应用程序的桌面应用程序...它的数据库驱动的应用程序...我正在使用计时器来获取新消息,新请求..


但是问题是它在用户界面上给出的响应速度很慢..就像我在执行添加联系人,删除联系人...之类的操作...



我使用哪个概念?

在后台运行的进程不会影响用户界面...

给我建议..


我可以为此使用异步线程吗?
异步线程有四种方法... so

请给我建议我使用哪种异步线程方法..?

hi frnds...



i m developing chat application its desktop application...its database driven application... i m using timer for geting new message,new request ..


but problem is that it give slow response on user interface..like i m doing operation like add contact,delete contact...



which concept i use?

process running on background not affect on user interface...

give me suggestion ..


can i use asynchronous threading for that ....?

and asynchronous threading has four method ...so

pls give me suggestion which method i use for asynchronous threading.. ?

推荐答案

有很多东西,很难一起回答.
另外,我不知道您的设置.您想要什么,Web服务? Web应用程序? P2P聊天?

只是一些想法.第一个原则:不要为每个客户端创建线程!所有客户端应由一个线程服务.线程数应该是固定的或可配置的,每个服务器的生命周期创建一次:类似这样的线程:主线程,一个用于数据库的线程,一个或两个用于侦听新用户和身份验证的线程,一个用于用户交互的公共线程.线程可以通过阻塞消息队列进行通信(我有一个通用队列实现,在Framework v.4.0中可以使用更快的实现).

您可以在我过去的答案中找到一些想法.抱歉,它们是为回答不同的问题而写的,有些项目可能无关紧要,但您也未提供所需的确切信息.请参阅:
来自同一端口号的多个客户端 [如何将ref参数传递给线程 [ ^ ]
其他相关参考资料的集合:
如何获取keydown事件在vb.net中的不同线程上操作 [用于线程通信和线程间调用的简单阻塞队列 [ ^ ].

这一切都需要一定的研究.详细信息高度取决于您的要求和决定.希望它能对您有所帮助.

祝你好运,
—SA
There is a lot to it, hard to answer all together.
Also, I don''t know your setting. What do you want, a Web Service? Web application? P2P chat?

Just a few ideas. First principal: don''t create a thread per client! All clients should be served by one thread. The number of threads should be fixed or configurable, created once per server''s lifetime: something like this: main thread, one thread for database, one or two threads for listening for new users and authentication, one common thread for user interactions. The threads can communicate through the blocking message queue (I have a generic queue implementation, a faster implementation is available in the Framework v. 4.0).

You can find some idea in my past Answers. Sorry, they are written in response to different Question ans some items can be irrelevant, but you also did not provide exact information on what you want. Please see:
Multple clients from same port Number[^]
Thread wrapper, very useful to avoid the problem of passing parameters:
How to pass ref parameter to the thread[^]
Collection of other related references:
How to get a keydown event to operate on a different thread in vb.net[^]
My article on blocking message queue, full code with usage samples:
Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

It all needs certain study. Further details highly depend on you requirements and decisions. Hope it can help you.

Good luck,
—SA


正如SA所指出的,其中还有更多内容.但是问题是,所有数据库调用都是在UI线程中进行的,然后阻止了您的编程.如果您使用的是.NET 4,请查看 System.Threading.Tasks [^ ]

可以启动一个新任务,该任务从数据库中获取新行,然后在UI中显示.
As SA already pointed out, there is more in it. But the problem is, that all database call is made in the UI thread with then blocks you program. If you are using .NET 4, take at look at System.Threading.Tasks[^]

There it is possible to start a new Task that fetch new rows from the database and then display then in the UI.


这篇关于c#.net中的线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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