首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在线程安全列表中使用StringCollectionEditor

在线程安全列表中使用StringCollectionEditor
EN

Stack Overflow用户
提问于 2013-02-02 14:03:24
回答 1查看 350关注 0票数 0

我正在尝试使用System.Windows.Forms.Design.StringCollectionEditor.通过Windows Forms属性网格公开我的List类成员我的问题是关于线程安全的。

代码语言:javascript
运行
复制
[Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]

public List<string> EventLogSources {
        get {
            lock (lockObj) {
                return eventLogSources;
            }
        }  
}

显然,这不是线程安全的,因为多个线程可以获取引用并更新它。那么,使用StringCollectionEditor (或类似的东西)并保持线程安全的最好方法是什么?

EN

回答 1

Stack Overflow用户

发布于 2013-02-27 05:16:44

来自http://mastersact.blogspot.com/2007/06/string-collection-editor-for-property.html的代码看起来可以做到这一点:

代码语言:javascript
运行
复制
public override object EditValue(ITypeDescriptorContext context,
IServiceProvider serviceprovider, object value)
{
if (serviceprovider != null)
{
mapiEditorService = serviceprovider
.GetService(typeof(IWindowsFormsEditorService)) as
IWindowsFormsEditorService;
}

if (mapiEditorService != null)
{
StringCollectionForm form = new StringCollectionForm();

// Retrieve previous values entered in list.
if (value != null)
{
List stringList = (List)value;
form.txtListValues.Text = String.Empty;
foreach (string stringValue in stringList)
{
form.txtListValues.Text += stringValue + "\r\n";
}
}

// Show Dialog.
form.ShowDialog();

if (form.DialogResult == DialogResult.OK)
{
List stringList = new List();

string[] listSeparator = new string[1];
listSeparator[0] = "\r\n";

string[] listValues = form.txtListValues.Text
.Split(listSeparator, StringSplitOptions.RemoveEmptyEntries);

// Add list values in list.
foreach (string stringValue in listValues)
{
stringList.Add(stringValue);
}

value = stringList;
}

return value;
}

return null;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14659161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档