나는 윈폼 응용 프로그램에서 다음 문제를 추적하기 위해 노력했습니다 null의 경우 주 스레드 (나는 현재 동기화 컨텍스트가 System.Windows.Forms.WindowsFormsSynchronizationContext
일 것으로 기대한다). 나는 계속에서 BackgroundWorker
를 사용하려고 시도하고있는 BackgroundWorker가 해당 이벤트 RunWorkerCompleted
및 ProgressChanged
의 현재에서 SynchronizationContext를 사용하기 때문에SynchronizationContext.Current 메인 UI에 계속에서 스레드
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
TaskScheduler ts = TaskScheduler.FromCurrentSynchronizationContext(); // Get the UI task scheduler
// This line is required to see the issue (Removing this causes the problem to go away), since it changes the codeflow in
// \SymbolCache\src\source\.NET\4\DEVDIV_TFS\Dev10\Releases\RTMRel\ndp\clr\src\BCL\System\Threading\ExecutionContext.cs\1305376\ExecutionContext.cs
// at line 435
System.Diagnostics.Trace.CorrelationManager.StartLogicalOperation("LogicalOperation");
var task = Task.Factory.StartNew(() => { });
var cont = task.ContinueWith(MyContinueWith, CancellationToken.None, TaskContinuationOptions.None, ts);
System.Diagnostics.Trace.CorrelationManager.StopLogicalOperation();
}
void MyContinueWith(Task t)
{
if (SynchronizationContext.Current == null) // The current SynchronizationContext shouldn't be null here, but it is.
MessageBox.Show("SynchronizationContext.Current is null");
}
}
}
이 나를 위해 문제입니다 :
다음은 문제를 보여주는 윈폼 코드입니다. BackgroundWorker를 시작하면 현재 SynchronizationContext가 null이므로 이벤트는 의도 한대로 주 UI 스레드에서 실행되지 않습니다.
내 질문 :
는 Microsoft의 코드에서 버그, 아니면 어디 선가 실수를했다?
추가 정보 :
- 내가 닷넷 4.0을 사용하고 내가 어떤에서 모두 디버그/릴리스에서이 작업을 재현 할 수
- (아직 .NET 4.5 RC에서이 시도하지 않은) x86/x64/모든 CPU (x64 시스템).
- 일관되게 재생합니다 (누군가를 재생성 할 수 없다면 관심이 있습니다).
- 가 나는 경우 BackgroundWorker를 사용하는 기존 코드가 - 그래서 쉽게 내가
MyContinueWith
의 코드가 메인 UI 스레드에서 실행되고 있는지 확인했습니다 BackgroundWorker에 - 사용하지 않는까지 변경할 수 없습니다.
StartLogicalOperation
호출이 문제의 원인이되는 이유를 정확히 알지 못합니다. 그게 내 응용 프로그램에서 좁혔습니다.
글쎄, 당신은 그 링크 진단을 가지고. 이 Winforms 응용 프로그램에 WPF 또는 WCF 코드를 혼합하고 있습니까? –
아니요 - Winforms 전용입니다. –