LINQ가 실제로이 기능을 갖고 있지는 않지만 프레임 워크 자체는 ... 30 개의 라인으로 나만의 비동기 쿼리 실행 프로그램을 쉽게 롤백 할 수 있습니다 ... 사실,)
편집 :이 글을 쓰면서 내가 구현하지 않은 이유를 발견했습니다. 익명 형식은 로컬 범위이므로 익명 형식을 처리 할 수 없습니다. 따라서 콜백 함수를 정의 할 방법이 없습니다. 이것은 많은 linq에서 sql에 이르기까지 select 절에서 생성됩니다. 아래 제안 중 하나라도 같은 운명을 겪습니다. 그래서 나는이 것이 가장 사용하기 쉽다고 생각합니다!
편집 : 유일한 해결책은 익명 형식을 사용하지 않는 것입니다. 콜백은 IEnumerable (형식 args 없음)을 사용하는 것으로 선언 할 수 있으며 리플렉션을 사용하여 필드 (ICK !!)에 액세스 할 수 있습니다. 또 다른 방법은 콜백을 "동적 인"것으로 선언하는 것입니다 ... 오 ... 기다려 ... 아직 안 끝났어. :) 이것은 동적 인 방법을 사용할 수있는 또 다른 예입니다. 일부는 학대라고 부를 수도 있습니다. 당신의 유틸리티 라이브러리에
던져이 :
public static class AsynchronousQueryExecutor
{
public static void Call<T>(IEnumerable<T> query, Action<IEnumerable<T>> callback, Action<Exception> errorCallback)
{
Func<IEnumerable<T>, IEnumerable<T>> func =
new Func<IEnumerable<T>, IEnumerable<T>>(InnerEnumerate<T>);
IEnumerable<T> result = null;
IAsyncResult ar = func.BeginInvoke(
query,
new AsyncCallback(delegate(IAsyncResult arr)
{
try
{
result = ((Func<IEnumerable<T>, IEnumerable<T>>)((AsyncResult)arr).AsyncDelegate).EndInvoke(arr);
}
catch (Exception ex)
{
if (errorCallback != null)
{
errorCallback(ex);
}
return;
}
//errors from inside here are the callbacks problem
//I think it would be confusing to report them
callback(result);
}),
null);
}
private static IEnumerable<T> InnerEnumerate<T>(IEnumerable<T> query)
{
foreach (var item in query) //the method hangs here while the query executes
{
yield return item;
}
}
}
그리고 당신이처럼 사용할 수 있습니다 매우 편리합니다, 지금 내 블로그에 올려 갈
class Program
{
public static void Main(string[] args)
{
//this could be your linq query
var qry = TestSlowLoadingEnumerable();
//We begin the call and give it our callback delegate
//and a delegate to an error handler
AsynchronousQueryExecutor.Call(qry, HandleResults, HandleError);
Console.WriteLine("Call began on seperate thread, execution continued");
Console.ReadLine();
}
public static void HandleResults(IEnumerable<int> results)
{
//the results are available in here
foreach (var item in results)
{
Console.WriteLine(item);
}
}
public static void HandleError(Exception ex)
{
Console.WriteLine("error");
}
//just a sample lazy loading enumerable
public static IEnumerable<int> TestSlowLoadingEnumerable()
{
Thread.Sleep(5000);
foreach (var i in new int[] { 1, 2, 3, 4, 5, 6 })
{
yield return i;
}
}
}
.
당신을위한 확인 작업 밖으로 아래의 대답을 했습니까? – TheSoftwareJedi
http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx에서 Reactive Extensions for .NET을 확인하십시오.이 기능은 비동기 Linq 쿼리 용으로 설계되었습니다. –
실제로 Linq *와 SQL * 질의는 작성자가 묻는 것입니다. 여기 –