Błąd: Object reference not set to an instance of an object.
Dlaczego SynchronizationContext.Current null?
Poniższy algorytm działa. Próbowałem, a następnie usunąłem projekt Winform
do innego katalogu i SynchronizationContext.Current
jest null
. Dlaczego?
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);
}
}
}
Jakim kontekstem * * prowadzisz ten kod? Aplikacja konsolowa? –
winform application –
Co tworzy instancję tego obiektu i kiedy? (Wydaje się, że przechwytujesz kontekst synchronizacji * na konstrukcji *.) –