同步对公共数据的访问

发布于 2024-08-13 12:38:57 字数 260 浏览 2 评论 0原文

我有一个 WinForms 应用程序(c#),它有一个后台线程,定期从数据源获取数据,对其进行一些操作,然后将其放入数据表中。应用程序中有 1 个或多个组件定期轮询此通用数据以对其进行处理。我想知道,同步此操作的“正确”方法是什么,例如组件在更新数据时不会查询数据?

在更新数据时锁定某些内容将不起作用,因为更新可能需要几秒钟的时间。所以我想,为什么不对其进行“双缓冲”,例如更新第二个 DataTable 中的数据,然后在完成交换时(仅针对交换操作锁定)。这是否合适,或者有更好的方法吗?

I have a WinForms app (c#) that has a background thread that periodically grabs data from a data source, does some manipulation of it, and puts it in a DataTable. There are 1 or more components in the app that periodically poll this common data to do stuff with it. I'm wondering, what's the "right" way to synchronize this, so e.g. the components don't query the data while it's being updated?

Locking on something while the data is being updated won't work because the updating could take several seconds. So I thought, why not "double-buffer" it, e.g. update the data in a second DataTable, then when finished swap (locking just for the swap operation). Is this appropriate, or is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

_蜘蛛 2024-08-20 12:38:57

这可行。您可以将字段引用标记为 [易失性](http://msdn.microsoft.com/en-us/library/x13ttww7(VS.71).aspx)

如果数据的一致性很重要,但不一定是及时性,那么实际上建议使用这种方法。换句话说,如果您的应用程序不一定总是需要最新和最新的数据;因为当 DataTable 正在侧面更新时,您将获得一些过时的数据。

另外,您需要小心别名(对 DataStore 的其他引用),并确保您不持有对其的任何其他引用。否则,您的应用程序中可能有两个不一致的DataStore

This could work. Instead of locking, you may get away with marking the field reference as [volatile](http://msdn.microsoft.com/en-us/library/x13ttww7(VS.71).aspx).

This approach is actually recommended if consistency of data is important, but not necessarily their timeliness. In other words, if you application doesn't necessarily always need the latest and freshest data; as you will be getting some stale data while DataTable is being updated on the side.

Also, you need to be careful about aliasing (other references to DataStore) and ensure than you don't hold any other reference to it. Otherwise, you may have two inconsistent DataStores in your application.

何以畏孤独 2024-08-20 12:38:57

从理论上讲,这听起来是一个很好的解决方案。但您需要注意一些问题。

  • 仅当多个 WinForms 组件可以具有不同版本的 DataTable 时,这才是一个好的解决方案。在获取新表的组件 1 和组件 2 之间可能会发生交换,导致它们获得不同的版本。这可以通过额外的锁定技术来解决
  • 当你说交换时,你的意思是后台线程发布,可以这么说,一个新的DataTable并开始填充一个完全不同的数据表?您的帖子可能会被解释为总共只有 2 个 DataTable 实例,并且后台线程正在它们之间切换。如果是这样就会导致同步问题。我不认为这就是你的意思,但想检查一下。

In theory this sounds like a great solution. But there are a couple of gotchas that you need to look out for.

  • This is only a good solution if it's OK for the multiple WinForms components can have differing versions of the DataTable. In between component1 and component2 grabbing the new table a swap could occur causing them to get different versions. This can be fixed with additional locking techniques
  • When you say swap do you mean that the background thread publishes, so to speak, a new DataTable and starts populating a completely different one? Your post could be interpretted as there being only 2 total DataTable instances and the background thread is switching between them. If so that will cause synchronization problems. I don't think that's what you mean but wanted to check.
你又不是我 2024-08-20 12:38:57

双缓冲是可行的方法。它消除了重新加载时间过长的问题。当您用新数据替换现有数据时,请确保有一种良好的锁定机制。 :)

组件的数据是只读的吗?如果他们正在更改数据,您需要小心同时写回和读取。

Double buffering is the way to go. It removes the issues of long reload times. Just be sure to have a nice locking mechanism for when you are replacing the existing data with the new one. :)

Is the data read only for the compnents? If they are changing the data you need to be careful about writting back and reading at the same time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文