錯誤:Object reference not set to an instance of an object.
爲什麼SynchronizationContext.Current爲空?
下面的算法工作。 我試了一下,然後我把Winform
項目移到了另一個目錄,SynchronizationContext.Current
是null
。 爲什麼?
SynchronizationContext uiCtx = SynchronizationContext.Current;
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int[] makeSelfMoves = new int[4];
lock (replay)
{
// count should be more than 2
foreach (KeyValuePair<int, int[]> item in replay)
{
makeSelfMoves = replay[item.Key];
codeFile.ExecuteAll(makeSelfMoves[0],
makeSelfMoves[1], makeSelfMoves[2], makeSelfMoves[3]);
// i get the error here. uictx is null
uiCtx.Post(o =>
{
PrintPieces(codeFile.PieceState());
}, null);
System.Threading.Thread.Sleep(1000);
}
}
}
然後,你在運行這段代碼的背景是什麼?控制檯應用程序? –
winform應用程序 –
什麼是創建此對象的實例以及何時? (畢竟,你似乎正在捕獲同步上下文*) –