0

System.Windows.Forms.Design.StringCollectionEditor を使用して、Windows フォーム プロパティ グリッド経由で List クラス メンバーを公開しようとしています。私の質問はスレッドセーフに関するものです

[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 (または類似のもの) を使用してスレッドセーフにする最良の方法は何ですか?

4

1 に答える 1

0

http://mastersact.blogspot.com/2007/06/string-collection-editor-for-property.htmlのコードは、トリックを実行するように見えます。

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;
}
于 2013-02-26T21:16:43.870 に答える