多线程导致读取器冲突错误-请求的解决方案选项 [英] Multithreading causing Reader collision errors - Solution Options Requested

查看:74
本文介绍了多线程导致读取器冲突错误-请求的解决方案选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序正在以每十分之一秒的速率从OPC服务器读取数据,每次读取都在其自己的线程中.然后,它创建一个线程将该信息写入数据库.

总的来说,这似乎工作正常,但是当我停止该过程并尝试在短时间内再次重新启动它时,在尝试写入数据库时​​出现很多错误,这似乎表明SqlReader很忙,而且还没有已关闭.

所以我想知道解决方案的选择是什么?

我曾经想过要创建一个坐着睡觉的线程,直到被唤醒唤醒并向数据库写入信息,然后再进入睡眠状态.这似乎是一个不错的选择,但需要我理解如何在应用程序尝试关闭时实际触发线程唤醒以及如何处理线程关闭(我目前从未这样做). />
我的一部分想知道我是否需要在SQL连接周围放置监视控件,以确保在将变量释放给其他进程之前关闭读取操作……这也许就是问题的根源.

我不确定还有什么其他选择,因此我将不胜感激.

预先感谢.

I have an application that is doing reads from an OPC Server at a rate of one every tenth of a second with each read being in its own thread. It then creates a thread to write that information into the database.

Overall this seems to work fine but when I stop the process and try and restart it again a short time later, I get a lot of errors trying to write to the database which seem to suggest that the SqlReader is busy and hasn''t been closed.

So I''m wondering what the solution options might be?

One thought I had was to create a thread that sat and slept until it was triggered to wake and write information to the database and then go back to sleep. This would seem to be a good option but would require that I understand how to actually trigger threads to wake up as well as handling the shutting down of the thread when the application is trying to close (which I currently have never done).

Part of me is wondering if I need to be putting monitor controls around my SQL connection to ensure that it closes the read before releasing the variable to other processes ... that perhaps that is the source of my problems.

I''m not sure what other options there might be so I would appreciate any thoughts or comments.

Thanks in advance.

推荐答案

每个SqlConnection只能有一个SqlReader,请尝试分别为每个线程创建连接.
You can only have one SqlReader per SqlConnection, try creating connections for each thread separately.


这篇关于多线程导致读取器冲突错误-请求的解决方案选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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