Problem with DisableProcessing()

zequion 441 Reputation points
2024-09-19T01:36:18.99+00:00

I'm trying to prevent a control from being rendered in Wpf because it takes too long to process the items. Since I haven't been able to achieve this, I'm now trying to prevent the screen Form from being rendered.

After doing MyDispatcher.DisableProcessing() of the Screen Process, when adding items to a screen control an error appears in Dispatcher.cs that says: "Error:2146233079 The dispatcher processing has been suspended, but the messages are still being processed."

System.Windows.Threading.Dispatcher MyDispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;
System.IDisposable MyThread_IDisposable = MyDispatcher.DisableProcessing()

I haven't been able to prevent the control from being updated because it's already on the screen and I can't use BeginInit(). It doesn't have the required properties either. E.g. for ListBox:

<ListBox VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling">

Sin título

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,874 questions
{count} votes

Accepted answer
  1. Hongrui Yu-MSFT 1,690 Reputation points Microsoft Vendor
    2024-09-19T07:02:36.3466667+00:00

    Hi,@zequion.

    The UI has been written in XAML but is not needed for the time being. You could set the control as follows.

     

    Visibility="Collapsed": The control will not be rendered.

    IsEnabled="False": The control will not be able to accept user input, but will still be rendered.

    Opacity="0": The control exists but will not be rendered.

    IsHitTestVisible="False": The control will not respond to any mouse events.

     

    When not needed

    
    <Button x:Name="MyButton" Content="Click Me"  HorizontalAlignment="Center" VerticalAlignment="Center" IsEnabled="False" Opacity="0"
    
     IsHitTestVisible="False" Visibility="Collapsed"/>
    
    

    When needed

    
    MyButton.Visibility = Visibility.Visible;
    
    MyButton.IsEnabled = true;
    
    MyButton.IsHitTestVisible = true;
    
    MyButton.Opacity = 1;
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 64,741 Reputation points
    2024-09-19T19:45:35.8866667+00:00

    as adding items to a control generally requires the dispatcher to dispatch the messages, you should not do any UI updates while the .DisableProcessing() is set. if you do not want it to display, set not visible, or remove from the screen.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.