I'm truly in love in Extension Methods. Awesome!

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Most articles and tutorials that I have seen about BackgroundWorker describing method to update GUI especially ProgressBar or downloading data in background. Today I start to wonder what if I would like to create real huge GUI in background, for example adding 10000 buttons to StackPanel. The solution is really simple. Basically I cannot create new Visual object in BackgroundWorker directly. I have to create an object in Dispather. Invoke thread. The code looks as following.

Procedural code:

    public partial class Window1 : System.Windows.Window

    {

 

        public Window1()

        {

            InitializeComponent();

            for (int i = 1; i < 10000; i++)

            {

                BackgroundWorker bw = new BackgroundWorker();

                bw.DoWork += new DoWorkEventHandler(Dowork);

                bw.RunWorkerAsync(i.ToString());

            }

        }

        Button tempButton = new Button();

        void Dowork(object sender, DoWorkEventArgs e)

        {

                Button b;

                m_Grid.Dispatcher.Invoke((ButtonInvoke)delegate()

                {

                    b = new Button();

                    b.Content = e.Argument;

                    m_Grid.Children.Add(b);

                });

        }

        delegate void ButtonInvoke();

    }

XAML code:

<Window x:Class="threads.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="threads" Height="300" Width="300"

    >

  <ScrollViewer VerticalScrollBarVisibility="Auto">

  <StackPanel x:Name="m_Grid">

  </StackPanel>

  </ScrollViewer>

</Window>

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

In WPF there are some situations when memory leaks. Of course part of this has to be assigned to handlers that were not removed on finalizing. This situation is well known from Winforms and it shouldn't be anything new for .net developers. However Windows Presentation Foundation have separate GUI layer witch push into using binding data. Binding is another situation that can make leaks. To avoid it programmer should use BindingOperations.ClearBinding method. It doesn't matter is binding implemented on GUI layer or in procedural code. Same situation is with handlers of course. Adequate to binding is case of use Command Binding. Mainly CommandBinding is a little bit complicated binding so it is also have to be released. There were some problems with leaking of BitmapImage but after SP1 for .Net 3.5 there is nothing to worry about. After all there is one more thing that I've found and this is registering handler on class layer. EventManager have method to register class handler but there is no method to unregister it. To workaround it programmer have to create a static method that handle an event.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
Main difference is that casting to wrong type throw an exception and an “as” keyword just return null. So if I would like to search specified type in VisualTree I can use “as” keyword and check is object not null or I can use “is” keyword. But when I would like to take not reference object like generics KeyValuePair from object type I will have to use casting to unpack the value type. This is because value types do not inherit from object so they are not objects and cannot use keyword as.
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
To get TemplateChild from DataTemplate use FindName method. In my opinion more adequate name is FindByName.
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
If there is no need to change freezable object anymore, always remember to call freeze method.
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati