在 Delphi 中,TDataSet 线程安全吗? [英] In Delphi, is TDataSet thread safe?

查看:19
本文介绍了在 Delphi 中,TDataSet 线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在它自己的线程中异步打开一个 TDataSet,以便主 VCL 线程可以继续运行直到完成,然后让主 VCL 线程从该 TDataSet 中读取.我做了一些实验,遇到了一些非常奇怪的情况,所以我想知道以前是否有人这样做过.

I'd like to be able to open a TDataSet asynchronously in its own thread so that the main VCL thread can continue until that's done, and then have the main VCL thread read from that TDataSet afterwards. I've done some experimenting and have gotten into some very weird situations, so I'm wondering if anyone has done this before.

我见过一些示例应用程序,其中 TDataSet 在单独的线程中创建,它被打开,然后从中读取数据,但这一切都是在单独的线程中完成的.我想知道在另一个线程打开数据源之后从主 VCL 线程中读取 TDataSet 是否安全.

I've seen some sample apps where a TDataSet is created in a separate thread, it's opened and then data is read from it, but that's all done in the separate thread. I'm wondering if it's safe to read from the TDataSet from the main VCL thread after the other thread opens the data source.

我正在 Delphi 7 中进行 Win32 编程,使用 DAC for MySQL 作为我的 TDataSet 后代.

I'm doing Win32 programming in Delphi 7, using TmySQLQuery from DAC for MySQL as my TDataSet descendant.

推荐答案

如果您只想在自己的线程中使用数据集,您可以使用 synchronize 与主线程通信以进行任何 VCL/UI 更新,例如任何其他组件.
或者,更好的是,您可以使用自己的消息系统实现主线程和工作线程之间的通信.

Provided you only want to use the dataset in its own thread, you can just use synchronize to communicate with the main thread for any VCL/UI update, like with any other component.
Or, better, you can implement communication between the mainthread and worker threads with your own messaging system.

在此处查看 Hallvard 的线程解决方案:
http://hallvards.blogspot.com/2008/03/tdm6-knitting-your-own-threads.html

check Hallvard's solution for threading here:
http://hallvards.blogspot.com/2008/03/tdm6-knitting-your-own-threads.html

或另一个:
http://dn.codegear.com/article/22411

关于同步及其低效率的一些解释:
http://www.eonclash.com/Tutorials/Multithreading/MartinHarvey1.1/Ch3.html

for some explanation on synchronize and its inefficiencies:
http://www.eonclash.com/Tutorials/Multithreading/MartinHarvey1.1/Ch3.html

这篇关于在 Delphi 中,TDataSet 线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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