它是安全的,允许两个线程在同时编辑同一个对象的不同属性?它是、线程、属性、对象

2023-09-04 10:33:01 作者:刀锋战神

我写的,通过分析和从文件中提取信息并存储从一个对象实例的每个文件的信息编目应用程序。除了从文件中提取的数据中的对象也具有附加的元数据属性(作者,标签,注释,等等。),后来获取存储在一个单独的XML文件。

I am writing a cataloging application that parses through and extracts information from files and stores the information from each file in an object instance. In addition to the data extracted from the file the objects also have additional meta data properties (author, tags, notes, etc..) which later get stored in a separate XML file.

从提取的文件中的数据是一个耗时的过程,所以我把它在一个单独的线程中运行。从文件中提取的属性将只来自文件,因而有[只读]属性prevent的用户编辑它们。仅填充由用户在另一方面的元数据属性,因此不仅读取。我允许用户观看/通过的PropertyGrid编辑这些对象

Extracting the data from the files is a time consuming process so I have it running on a separate thread. The properties extracted from the files will only ever come from the files, and thus have [ReadOnly] attributes to prevent the user from editing them. The meta data properties on the other hand are only populated by the user and thus are not read only. I'm allowing the user to view/edit these object through a PropertyGrid.

因此​​,如果提取处理一个线程上填充的对象的文件属性运行,是有在让用户编辑的元数据属性的同时任何危险?我试图决定我是否应该使用一个模式界面,prevents做任何事情,直到解压缩完成/取消的用户,还是非模式界面,允许他们继续提取运行时的工作。

So if the extraction process is running on one thread populating the file properties of an object, is there any danger in letting the user edit the meta data properties at the same time? I'm trying to decided if I should use a modal interface that prevents the user from doing anything until the extraction is complete/canceled, or a non-modal interface that allows them to continue working while the extraction is running.

推荐答案

要具体到你的问题:没有没有任何问题。

To be specific to your question: No there is no problem.

你应该注意的是,不能从UI线程读取你的属性写的后台线程他们正在写一会儿。如果不能保证这一点,你必须使用锁,马歇尔写入到UI线程。 (使用 control.Invoke()的BackgroundWorker 或者,确保写为指针的原子写入对象不是由后台线程编辑,而从UI线程可见我不认为标准的容器,如名单,其中,T> 是线程安全的。

What you should watch out for is that your properties written by the background thread are not read from the UI thread while they are being written. If you can't guarantee this, you must either use locks, marshall the write to the UI thread. (using control.Invoke() or BackgroundWorker or, make sure the write is an atomic write of pointer to an object that is not edited by the background thread while visible from the UI thread. I would not assume standard containers like List<T> are thread safe.

[措辞改变]

 
精彩推荐