2012-08-04 3 views
0

나는 다음 함수를 사용하여 items.itis 함수를 사용하여 잘 작동하지만 실행에 많은 시간이 걸린다. listview에 추가 된 항목의 실행 시간을 줄이는데 도움이된다.listview의 실행 시간을 줄일 수 있습니까?

기능 :

당신은 당신의 UI 당신이 (스트림 및 이미지를 조작, 귀하의 경우) 같은 스레드에서 nonUI 작업을 할 경우 속도가 느린 될 것입니다 것으로 예상 할 필요가
listViewCollection.Clear(); 
listViewCollection.LargeImageList = imgList; 
listViewCollection.LargeImageList.ImageSize = new System.Drawing.Size(100, 100); 
foreach (var dr in Ringscode.Where(S => !S.IsSold)) 
{ 
    listViewCollection.Items.Insert(0, 
        new ListViewItem(dr.CodeNo.ToString(), dr.RingID.ToString())); 
    imgList.Images.Add(dr.RingID.ToString(), binaryToImage(dr.Image)); 
} 

public Image binaryToImage(System.Data.Linq.Binary binary) 
{ 
    byte[] b = binary.ToArray(); 
    MemoryStream ms = new MemoryStream(b); 
    Image img = Image.FromStream(ms); 
    return img; 
} 
+0

시간이 많이 걸리는 방법은 공유하지 않은'binaryToImage'입니다. – yogi

+0

공용 이미지 binaryToImage (System.Data.Linq.Binary 바이너리) { byte [] b = binary.ToArray(); MemoryStream ms = 새 MemoryStream (b); 이미지 img = Image.FromStream (ms); return img; } – Tulsi

+0

@yogi binaryToImage return image – Tulsi

답변

1

. 해결책은이 작업을 다른 스레드로 오프로드하여 사용자에게 UI 스레드를 남기는 것입니다. 작업자 스레드가 완료되면 UI 스레드에 업데이트를 지시하십시오.

두 번째 요점은 ListView 데이터를 일괄 적으로 업데이트 할 때 ListView에 모든 조작을 완료 할 때까지 기다려야한다는 것입니다.

이렇게하면 더 좋습니다. 해설은 인라인입니다.

// Create a method which will be executed in background thread, 
// in order not to block UI 
void StartListViewUpdate() 
{ 
    // First prepare the data you need to display 
    List<ListViewItem> newItems = new List<ListViewItem>(); 
    foreach (var dr in Ringscode.Where(S => !S.IsSold)) 
    { 
     newItems.Insert(0, 
         new ListViewItem(dr.CodeNo.ToString(), dr.RingID.ToString())); 
     imgList.Images.Add(dr.RingID.ToString(), binaryToImage(dr.Image)); 
    } 

    // Tell ListView to execute UpdateListView method on UI thread 
    // and send needed parameters 
    listView.BeginInvoke(new UpdateListDelegate(UpdateListView), newItems); 
} 

// Create delegate definition for methods you need delegates for 
public delegate void UpdateListDelegate(List<ListViewItem> newItems); 
void UpdateListView(List<ListViewItem> newItems) 
{ 
    // Tell ListView not to update until you are finished with updating it's 
    // data source 
    listView.BeginUpdate(); 
    // Replace the data 
    listViewCollection.Clear(); 
    listViewCollection.LargeImageList = imgList; 
    listViewCollection.LargeImageList.ImageSize = new System.Drawing.Size(100, 100); 
    foreach (ListViewItem item in newItems) 
     listViewCollection.Add(item); 
    // Tell ListView it can now update 
    listView.EndUpdate(); 
} 

// Somewhere in your code, spin off StartListViewUpdate on another thread 
... 
     ThreadPool.QueueUserWorkItem(new WaitCallback(StartListViewUpdate)); 
... 

이 인라인을 작성하고 VS에서 테스트하지 않은 경우 몇 가지 사항을 수정해야 할 수도 있습니다.

관련 문제