{"record":{"id":"6cab7bd615901bc2","repo":"MaterialDesignInXAML/MaterialDesignInXamlToolkit","slug":"snackbarmessagequeue-must-be-created-by-the-same-t","errorCode":null,"errorMessage":"SnackbarMessageQueue must be created by the same thread.","messagePattern":"SnackbarMessageQueue must be created by the same thread\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/MaterialDesignThemes.Wpf/Snackbar.cs","lineNumber":55,"sourceCode":"    }\n\n    public static readonly DependencyProperty MessageQueueProperty = DependencyProperty.Register(\n        nameof(MessageQueue), typeof(SnackbarMessageQueue), typeof(Snackbar), new PropertyMetadata(default(SnackbarMessageQueue), MessageQueuePropertyChangedCallback),\n        MessageQueueValidateValueCallback);\n\n    private static void MessageQueuePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)\n    {\n        var snackbar = (Snackbar)dependencyObject;\n        snackbar._messageQueueRegistrationCleanUp?.Invoke();\n        var messageQueue = dependencyPropertyChangedEventArgs.NewValue as SnackbarMessageQueue;\n        snackbar._messageQueueRegistrationCleanUp = messageQueue?.Pair(snackbar);\n    }\n\n    private static bool MessageQueueValidateValueCallback(object value)\n    {\n        if (value is null || ((SnackbarMessageQueue)value).Dispatcher == Dispatcher.CurrentDispatcher)\n            return true;\n        throw new ArgumentException(\"SnackbarMessageQueue must be created by the same thread.\", nameof(value));\n    }\n\n    public SnackbarMessageQueue? MessageQueue\n    {\n        get => (SnackbarMessageQueue?)GetValue(MessageQueueProperty);\n        set => SetValue(MessageQueueProperty, value);\n    }\n\n    public static readonly DependencyProperty IsActiveProperty = DependencyProperty.Register(\n        nameof(IsActive), typeof(bool), typeof(Snackbar), new PropertyMetadata(default(bool), IsActivePropertyChangedCallback));\n\n    public bool IsActive\n    {\n        get => (bool)GetValue(IsActiveProperty);\n        set => SetValue(IsActiveProperty, value);\n    }\n\n    public event RoutedPropertyChangedEventHandler<bool> IsActiveChanged","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/98edec3a0b96ef272c587b14bcd67ef5f928a7ed/src/MaterialDesignThemes.Wpf/Snackbar.cs#L37-L73","documentation":"The Snackbar.MessageQueue dependency property registers a validate-value callback that ensures the SnackbarMessageQueue shares the Snackbar's Dispatcher. WPF dependency objects are dispatcher-affine; pairing a queue from another thread would cause cross-thread access exceptions later, so the callback throws ArgumentException eagerly at assignment time.","triggerScenarios":"Assigning a SnackbarMessageQueue created on a different thread than the Snackbar control, e.g. `snackbar.MessageQueue = queueFromAnotherThread;` or binding to a queue resolved on a background dispatcher.","commonSituations":"A DI container resolving the queue on a non-UI thread; a Task.Run factory constructing the queue; multi-window setups with separate dispatchers; a ViewModel owned by a worker thread.","solutions":["Construct the SnackbarMessageQueue on the UI (dispatcher) thread, e.g. in the view's constructor.","Pass an explicit Dispatcher into the queue constructor: new SnackbarMessageQueue(duration, Application.Current.Dispatcher).","Bind Snackbar.MessageQueue to a queue owned by the same dispatcher as the control, or create the queue lazily on the UI thread."],"exampleFix":"// before (queue built on a Task thread)\nvar queue = await Task.Run(() => new SnackbarMessageQueue());\nsnackbar.MessageQueue = queue;\n// after\nvar queue = new SnackbarMessageQueue(TimeSpan.FromSeconds(3), Application.Current.Dispatcher);\nsnackbar.MessageQueue = queue;","handlingStrategy":"validation","validationCode":"if (queue.Dispatcher != snackbar.Dispatcher)\n    throw new InvalidOperationException(\"Queue must share the Snackbar's dispatcher.\");\nsnackbar.MessageQueue = queue;","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Construct SnackbarMessageQueue on the UI thread or with an explicit UI Dispatcher.","Do not resolve the queue from a background thread via DI.","Keep multi-window setups on a shared dispatcher, or create one queue per window."],"tags":["wpf","snackbar","threading","dispatcher","argument"],"backgroundTag":null,"analyzedSha":"98edec3a0b96ef272c587b14bcd67ef5f928a7ed","analyzedAt":"2026-08-13T14:31:19.111Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}