简体   繁体   English

如何在 C# 中创建高效的后台任务?

[英]How to create an efficient background task in C#?

I am fairly new to asynchronous programming so I need help.我对异步编程相当陌生,所以我需要帮助。

What I need to do is, create a windows service that constantly checks the database for menu updates (insert/updates), tables updates (insert/updates), menu category updates (insert/updates) and so on and if any change is detected the service will then need to POST those said changes to separate APIs one by one.我需要做的是,创建一个 Windows 服务,不断检查数据库的菜单更新(插入/更新)、表更新(插入/更新)、菜单类别更新(插入/更新)等等,如果检测到任何更改然后,该服务将需要将这些更改一一发布到单独的 API。 Keeping in mind that the service will be used for just this purpose and the database that I need to check for updates is SQL Server.请记住,该服务将仅用于此目的,而我需要检查更新的数据库是 SQL Server。

So, how do I approach this scenario efficiently ?那么,我该如何有效地处理这种情况呢? Do I create new Tasks (System.Threading.Tasks) or create new Threads (System.Threading.Thread) for each pieces like UpdateMenu that checks the menu updates and upload to api, UpdateTable, UpdateDishes and so on and how do I go about the Posting to the API part I mean do I create a new Task for each and every API call?我是创建新任务 (System.Threading.Tasks) 还是为每个部分创建新线程 (System.Threading.Thread),例如检查菜单更新并上传到 api、UpdateTable、UpdateDishes 等的 UpdateMenu,以及我该怎么做发布到 API 部分我的意思是我是否为每个 API 调用创建一个新任务? I want the application to be as efficient as possible and pick the changes and post them to API as soon as possible.我希望应用程序尽可能高效并选择更改并尽快将它们发布到 API。

Thanks in advance.提前致谢。

It seems that you are worried about the overhead of the mechanism that you are going to use, in order to fetch data from the database and post these data to APIs.您似乎担心要使用的机制的开销,以便从数据库中获取数据并将这些数据发布到 API。 You are thinking that maybe Thread s are fast and Task s are slower, or vice versa.您在想,也许Thread快而Task慢,反之亦然。 In fact choosing between these two mechanisms is likely to have no measurable impact to your service's demand for CPU, memory or other system resources.事实上,在这两种机制之间进行选择可能不会对您的服务对 CPU、内存或其他系统资源的需求产生可衡量的影响。

What is likely to be impactful, is the pattern of communication of your service with the database and the APIs.可能会产生影响的是您的服务与数据库和 API 的通信模式。 For example if your threads/tasks are not coordinated with each other, and query the database all at the same time, the database might be slow to respond, and might consume larger amounts of memory while preparing the response.例如,如果您的线程/任务没有相互协调,并且同时查询数据库,则数据库可能响应缓慢,并且在准备响应时可能会消耗大量内存。 That's not because your threads/tasks are slow.那不是因为您的线程/任务很慢。 It's because your service is querying the database with a pattern that makes it harder for the database to respond.这是因为您的服务正在使用使数据库更难响应的模式查询数据库。 The same might be true for the pattern of communication with the APIs.与 API 的通信模式可能也是如此。 If your workers are not coordinated, the network connectivity might become a bottleneck, or the remote machines that host the APIs might suffer.如果您的工作人员不协调,网络连接可能会成为瓶颈,或者托管 API 的远程计算机可能会受到影响。

So my advice is to focus on the usability factor of the mechanisms, and not on their supposed difference in performance.因此,我的建议是关注机制的可用性因素,而不是它们假定的性能差异。 If you are comfortable and familiar with threads, and know nothing about tasks, use threads.如果您对线程很熟悉,对任务一无所知,请使用线程。 If you are familiar with both threads and tasks, use tasks because they are generally easier to use.如果您熟悉线程和任务,请使用任务,因为它们通常更易于使用。 You'd better invest your time to optimize the communication pattern between your service and its dependencies, than for doing benchmarks trying to find the best between mechanisms that for all intents and purposes are equally efficient.您最好花时间优化服务及其依赖项之间的通信模式,而不是进行基准测试,试图在所有意图和目的都同样有效的机制之间找到最好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM