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

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

问题描述

我想能够在自己的线程中异步打开TDataSet,以便主VCL线程可以继续,直到完成,然后从该TDataSet中读取主VCL线程。我已经做了一些实验,并已经陷入了一些非常奇怪的情况,所以我想知道有没有人这样做。

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 的TmySQLQuery作为我的TDataSet后代。

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

推荐答案

如果您只想在自己的线程中使用数据集,则可以使用同步与主线程进行通信,以进行任何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天全站免登陆