반응형
Dispatcher.BeginInvoke : 람다를 System.Delegate로 변환 할 수 없습니다.
전화하려고 해요 System.Windows.Threading.Dispatcher.BeginInvoke
. 방법의 서명은 다음과 가변합니다.
BeginInvoke(Delegate method, params object[] args)
대리인을 생성하는 대신 Lambda를 전달합니다.
_dispatcher.BeginInvoke((sender) => { DoSomething(); }, new object[] { this } );
그것은 나에게 컴파일러 오류를 준다.
람다를 System.Delegate로 변환 할 수 없습니다.
대리자 의은 개체를 반환합니다. 내 람다는 이것과 일치하지만 작동하지 않습니다. 내가 무엇을 놓치고 있습니까?
이 메소드 는 System.Delegate 를 사용하여 선언 된 특정 유형의 대리 제공해야합니다. 이는 다음과 같이 새 DelegateType을 통해 지정된 델리게이트를 생성하거나 캐스트를 통해 수행 할 수 있습니다.
_dispatcher.BeginInvoke(
new Action<MyClass>((sender) => { DoSomething(); }),
new object[] { this }
);
또한 SLaks가 지적했듯이 Dispatcher.BeginInvoke 는 매개 변수 배열을 사용하여 다음과 같이 사용할 수 있습니다.
_dispatcher.BeginInvoke(
new Action<MyClass>((sender) => { DoSomething(); }),
this
);
또는 DoSomething 이이 개체 자체에 대한 메서드 인 경우 :
_dispatcher.BeginInvoke(new Action(this.DoSomething));
더 짧게 :
_dispatcher.BeginInvoke((Action)(() => DoSomething()));
인라인 Lambda 사용 ...
Dispatcher.BeginInvoke((Action)(()=>{
//Write Code Here
}));
프로젝트에서 System.Windows.Presentation.dll을 참조하고 추가 using System.Windows.Threading
하면 람다 구문을 사용할 수있는 확장 메서드에 액세스 할 수 있습니다.
using System.Windows.Threading;
...
Dispatcher.BeginInvoke(() =>
{
});
이를 위해 확장 메서드를 만듭니다. 예
public static void BeginInvoke(this Control control, Action action)
=> control.BeginInvoke(action);
이제 형식 내에서 호출 할 수 있습니다 this.BeginInvoke(() => { ... })
. ..
반응형
'ProgramingTip' 카테고리의 다른 글
TeamCity로 빌드 한 후 배포하는 방법은 무엇입니까? (0) | 2020.10.27 |
---|---|
일반 메서드에서 프로토콜 기본 구현 호출 (0) | 2020.10.27 |
T-SQL에 노드를 연결하는 존재 함수가 있습니까? (0) | 2020.10.26 |
"JPEG 파일이 아닙니다 : 0x89 0x50으로 시작"오류가 발생하는 이유 (0) | 2020.10.26 |
Golang 어디에서 문자를 인덱싱하는 방법은 무엇입니까? (0) | 2020.10.26 |