는 작업 (AsyncCtp with Silverlight 4) 사용 : 새 비동기를 사용할 수 있다면 더 나은 얻을
public void Load(){
this.IsBusy = true;
Task.Factory.StartNew(()=> DoHeavyWork())
.ContinueWith(t => this.IsBusy = false);
}
은/비동기 CTP 또는 VS2012/Silverlight 5
public async void Load(){
try{
this.IsBusy = true;
await Task.Factory.StartNew(()=> DoHeavyWork());
}
finally
{
this.IsBusy = false;
}
}
편집
와 기능을 기다리고 있습니다
백그라운드 작업에서 ObservableCollection을 업데이트한다고 가정합니다. 콜렉션 업데이트를 처리하는 핸들러가 UI 스레드에서 실행되지 않기 때문에 실제로 문제가 발생할 수 있습니다. 따라서 콜렉션 업데이트는 바인딩 시스템만큼 스레드 안전하지 않습니다. 이 작업을 수행하려면 UI 스레드의 ObservableCollection에 항목을 추가해야합니다. 당신이 한 번에 모든 아이템을 취득 할 수있는 경우, 당신은이 작업을 수행 할 수 있습니다 내가 this answer에서 제안처럼
public async void Load(){
try{
this.IsBusy = true;
// Returns the fetched items
var items = await Task.Factory.StartNew(()=> DoHeavyWork());
// This will happen in the UI thread because "await" returns the
// control to the original SynchronizationContext
foreach(var item in items)
this.observableCollection.Add(item);
}
finally
{
this.IsBusy = false;
}
}
당신이 일괄 적으로로드 할 수있는 경우
, 당신은, 현재 Dispatcher를 사용하여 컬렉션에 항목을 추가 할 수 있습니다.
BGW 코드를 게시 할 수 있습니까? –