性能提高在Windows服务中同步执行threading.timer [英] increased performance & execute synchronously threading.timer in windows service

查看:63
本文介绍了性能提高在Windows服务中同步执行threading.timer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Windows服务中使用了system.threading.timer的数量
每个线程每隔1秒分配一次执行,但是输出在4秒(有时甚至3秒)之后发出.
为什么不是每隔一秒钟或相同的时间都没有为所有线程提供输出?
能否请您告诉我如何提高此性能.
谢谢

I have used the number of system.threading.timer in windows service
each thread assign the execute at every 1 second but the output come after the 4 second or sometimes 3 seconds .
why all threads are not given the output at every one seconds or same timing?
can you please tell me how to increased this performance.
thanks

推荐答案

取决于您的线程在做什么:从您的描述来看并不明显.

您不能每隔1秒分配一次执行",线程会一直运行直到由于某种原因而结束或挂起(例如您所说的"Sleep(1000)"),但这并不能保证线程将在以下时间再次开始执行:精确到一秒-这取决于发生的其他事情,拥有多少个内核,创建多少个线程以及各种线程优先级是什么.

您实际上想做什么?



在线程函数中执行,从数组中找出选定的值,然后写入数据表,然后将此表写入MYSQL表
但是当我将只检查一个线程时,在1秒内将1000行写入MYSQL表没有问题,我在250 milisecods上也测试了这1个线程,效果很好
那为什么12个线程不起作用?"


这不是一个简单的问题,要取决于很多因素.
一个将1000条记录写入MySQL的线程的工作速度可能好于10个线程每条写入100条记录的工作速度:这取决于许多因素,例如MySql服务器上可用的连接数,网络带宽,MySql服务器可用的内存例如,核心数.请记住,每个线程都需要在您的计算机上处​​理时间(即要在其上运行的免费内核)和在数据库服务器上的处理时间.它不像更多线程=更快的处理"那样简单.
即使在琐碎的示例中,更多线程也可能意味着处理速度变慢,因为处理线程本身会涉及处理开销.

考虑一下您要从中实现的目标:我建议,如果可能的话,一个线程应该处理所有数据库更新,纯粹是为了最大程度地减少所需的连接数.
Depends on what your threads are doing: From your description it isn''t obvious.

You can''t "assign an execute" every 1 second, a thread runs until it is finished or suspended for some reason - like you said "Sleep(1000)" - but that will not guarantee that the thread will start executing again in precisely one second - it depends on whet else is going on, how many cores you have, how many threads you create, what the various thread priorities are.

What are you actually trying to do?



"in thread function execute find out the selected value from the array then write into the datatable then this table write to the MYSQL table
but when i will be checking the only one thread there is no issue to writing the 1000 row into the MYSQL table at 1 second i have tested this 1 thread also at 250 milisecods it works fine
then why 12 threads are not working"


That is not a simple question to answer, it depends on so many factors.
A single thread writing 1000 records into MySQL may well work quicker than ten threads writing 100 records each: It depends on so many factors, such as the number of connections available at the MySql server, the network bandwidth, the memory available to the MySql server instance, the number of cores. Remember, each thread needs processing time on your machine (i.e. a free core to run on) and processing time on the database server. It isn''t as simple as "More threads = faster processing".
Even in trivial examples, more threads can mean slower processing, as there is a processing overhead involved in handling the threads themselves.

Think about what you are trying to achieve from this: I would suggest that a single thread should handle all database updates if possible, purely to minimise the number of connections required.


尝试给出的链接下方:

逐步创建C#服务:第一课 [
Try the link given below:

Creating a C# Service Step-by-Step: Lesson I[^]


这篇关于性能提高在Windows服务中同步执行threading.timer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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