무겁거나 장기간 실행되는 동기 작업에 대한 래퍼 역할을하는 C#에서 cmdlet을 작성했습니다. 이 방법 (다른 사람의 코드)은 이벤트 핸들러를 통해이 장기 실행 작업 동안 백분율 진행률을보고합니다.이 코드를 powershell의 표준 WriteProgress
메서드에 연결하여 예쁜 출력 진행률 표시 줄을 얻으려고합니다.보고 이벤트 핸들러에서 Powershell 진행
The WriteObject and WriteError methods cannot be called from outside the overrides of the BeginProcessing, ProcessRecord, and EndProcessing methods, and they can only be called from within the same thread.
것은 내 코드 것 : 발견 할 수
overrride void ProcessRecord()
{
LongRunningOperation op = new LongRunningOperation();
op.ProgressChanged += ProgressUpdate;
op.Execute();
op.ProgressChanged -= ProgressUpdate;
}
void ProgressUpdate(object sender, ProgressChangeEventArgs e)
{
ProgressRecord progress = new ProgressRecord(activityId: 1, activity: "Moving data", statusDescription: "Current operation");
progress.PercentComplete = e.ProgressPercentage;
WriteProgress(progress);
}
누구든지 무엇을 내가 잘못 그러나, 나는 다음과 같은 오류 메시지가 받고 있어요?
업데이트 : 이벤트 핸들러가 ProcessRecord()
과 다른 스레드에서 트리거되는 것처럼 보입니다. 다시 필요한 정보를 ProcessRecord()
과 동일한 스레드로 가져올 수 있습니까?
당신은''op.Execute은()'라는 동일한 스레드에서 제기 op.ProgressChanged' 있는지 확인 있습니까? – PetSerAl
@PetSerAl Whelp, 문제 (a?) 문제 인 것 같습니다. op.Execute()가 새로운 스레드를 회전시킬 것이라고 생각하지 않았습니다. 어떤 생각을 어떻게 해결할 수 있습니까? – Benjin
어떤 PowerShell 및 .NET 버전을 타겟팅합니까? – PetSerAl