异步方法调用的顺序
我正在开发一个类库,它根据 Web 配置文件中定义的策略在多种类型的数据源(文件、xml、数据库)中记录 Web 应用程序的审核详细信息。
我的审核日志方法具有与此类似的签名: 公共静态无效LogInfo(用户用户,模块模块,列表lst);
Web 应用程序使用此方法来记录重要的详细信息,例如警告、错误甚至异常详细信息。
由于在单个工作流程中,对这些方法的调用超过 700 次,因此我想到将它们设为异步。我使用了 ThreadPool 类中名为 QueueUserWorkItem 的简单方法,
ThreadPool.QueueUserWorkItem(o => LogInfo(User user, Module module, List<Object> lst) );
但这并不能确保工作项的排队顺序到它。尽管我的所有信息都已记录,但整个订购都被搞乱了。在我的文本文件中,我的日志未按调用顺序排列。
有没有办法可以控制使用 QueueUserWorkItem 调用的线程的顺序?
I am working on a class library that logs audit details of a web application in several types of datasources(file, xml, database) based on policies defined in the web configuration file.
My Audit log method has a signature similar to this:
public static void LogInfo(User user, Module module, List lst);
Web application uses this method to log important pieces of details like warnings, error and even exception details.
Since in a single workflow, there are more than 700+ calls to these methods , I thought of making them asynchronous. I used simple method from ThreadPool class called QueueUserWorkItem
ThreadPool.QueueUserWorkItem(o => LogInfo(User user, Module module, List<Object> lst) );
but this does not ensure the order in which work item was queued to it. Even though all my information was logged but entire ordering was messed up. In my text file my logs were not in the order in which they were called.
Is there a way I can control the ordering of the threads being called using QueueUserWorkItem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您在使用 QueueUserWorkItem 时无法指定顺序。
要并行运行日志记录(在某些后台线程上),您可以使用 ConcurrentQueue。这是一个线程安全的集合,可以从多个线程访问。您可以创建一个工作项(或线程)来从集合中读取元素并将它们写入文件。您的主应用程序会将项目添加到集合中。事实上,您从单个线程向集合添加项目应该保证它们将以正确的顺序读取。
为了简单起见,您可以将
Action
值存储在队列中:后台任务只需从队列中获取
Action
并运行它们:如果您需要停止后台处理器(将数据写入日志),您可以创建一个
CancellationTokenSource
并在取消令牌(由主线程)时结束while
循环。使用IsCancellationRequested
属性 (参见 MSDN)I don't think you can specify ordering when using
QueueUserWorkItem
.To run the logging in parallel (on some background thread), you could use
ConcurrentQueue<T>
. This is a thread-safe collection that can be accessed from multiple threads. You could create one work item (or a thread) that reads elements from the collection and writes them to a file. Your main application would add items to the collection. The fact that you're adding items to the collection from a single thread should guarantee that they will be read in the right order.To keep things simple, you can store
Action
values in the queue:The background task can just take
Action
s from the queue and run them:If you need to stop the background processor (that writes data to the log), you can create a
CancellationTokenSource
and also end thewhile
loop when the token is being cancelled (by the main thread). This cha be checked usingIsCancellationRequested
property (see MSDN)解决此问题的一种方法是将数据放入队列中,然后从该队列中选择一个任务并按顺序写入它们。如果您使用的是.net 4.0,您可以使用 ConcurrentQueue,它是线程安全,否则一个带有适当锁的简单队列也可以工作。
然后,使用队列的线程可以定期检查队列内的任何元素,并且可以记录其中的每一个元素。这样,冗长的操作(日志记录)可以在它自己的线程中进行,而在主线程中您只需添加即可。
One way of solving this would be to put your data in a queue, and then having a single task picking from that queue and writing them in order. If you are using .net 4.0 You could use ConcurrentQueue, which is thread safe, otherwise a simple Queue with proper locks would work as well.
The thread consuming the queue could then periodically check for any element inside the queue, and for each one of them it could log. This way the lengthy operation (logging) could be in its own thread, whereas in the main thread you do simply adds.