2011-09-24 4 views
0

이전 Windows Forms 응용 프로그램을 WPF 응용 프로그램으로 변환하려고합니다.스레드 안전 ListView 컨트롤에 하위 항목 추가

다음 코드는 더 이상 C# .NET을 4.0에서 컴파일되지 :

// Thread safe adding of subitem to ListView control 
private delegate void AddSubItemCallback(
    ListView control, 
    int item, 
    string subitemText 
); 

private void AddSubItem(
    ListView control, 
    int item, 
    string subitemText 
) { 
    if (control.InvokeRequired) { 
    var d = new AddSubItemCallback(AddSubItem); 
    control.Invoke(d, new object[] { control, item, subitemText }); 
    } else { 
    control.Items[item].SubItems.Add(subitemText); 
    } 
} 

이 코드를 변환하는 데 도와주세요.

+0

사용 Dispatcher.CheckAccess 및 Dispatcher.Invoke : WPF에서는 Dispatcher.CheckAccess/Dispatcher.Invoke을 사용합니다. –

답변

0

희망 사항으로 following blog post가 도움이 되길 바랍니다. 대신

if (control.Dispatcher.CheckAccess()) 
{ 
    // You are on the GUI thread => you can access and modify GUI controls directly 
    control.Items[item].SubItems.Add(subitemText); 
} 
else 
{ 
    // You are not on the GUI thread => dispatch 
    AddSubItemCallback d = ... 
    control.Dispatcher.Invoke(
     DispatcherPriority.Normal, 
     new AddSubItemCallback(AddSubItem) 
    ); 
} 
+0

코드 : control.Items [item] .SubItems.Add (subitemText); .NET 4.0 WPF에서 더 이상 컴파일되지 않습니다. "SubItems"을 해결할 수 없습니다. – CBrauer