Answer the question
In order to leave comments, you need to log in
Why doesn't Dispatcher.Invoke work for all Controls?
I'm rewriting an application from WinForm to WPF and I've run into strange Invoke behavior.
There is a method that is called via
Task.Factory.StartNew(ThreadFunc, TaskCreationOptions.LongRunning);
MemoryStream ms = null;
while (true)
{
if (_token.IsCancellationRequested) return;
ms?.Dispose();
_client.GetNextMessage(out var msg);
if (msg.eventType == EventType.Data)
{
ms = new MemoryStream(msg.data);
_bitmapImage = new BitmapImage();
_bitmapImage.BeginInit();
_bitmapImage.StreamSource = ms;
_bitmapImage.EndInit();
Dispatcher.Invoke(() =>
{
pictureBox.Source = _bitmapImage;
});
}
else if (msg.eventType == EventType.Disconnected)
{
_cancelTokenSource.Cancel();
Dispatcher.Invoke(() =>
{
Connect.IsEnabled = true;
Disconnect.IsEnabled = false;
pictureBox.Source = null;
});
MessageBox.Show("Трансляция закончилась или вы были отключены!", "Уведомление",
MessageBoxButton.OK, MessageBoxImage.Asterisk);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question