Tech Off Thread

5 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

WPF Events in usercontrol

Back to Forum: Tech Off
  • User profile image
    sam.pinnacle

    Hi

    I am currently facing memory leak issue. I will exlpain what is the scenario

    I have a window with grid inside it.

    1. I have a login user control with username and password textbox and login button

    2. I have a AddUser user control with certain fields on it.

    In both this usercontrol i have a button control Called Close. 
    On the click on this button i raise an event
    here is a sample code

    Partial Public Class LoginControl

        Public Event EventtOnClose(ByVal sender As Object, ByVal e As RoutedEventArgs)

        Private Sub cmdClose_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmdClose.Click
            RaiseEvent EventtOnClose(Me, New RoutedEventArgs)
        End Sub

    End Class

    Partial Public Class AddUserControl

        Public Event EventtOnClose(ByVal sender As Object, ByVal e As RoutedEventArgs)

        Private Sub cmdClose_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmdClose.Click
            RaiseEvent EventtOnClose(Me, New RoutedEventArgs)
        End Sub

    End Class


    In the window there are two buttons cmdButton1 and cmdButton2
    <Window
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     x:Class="Window1"
     x:Name="Window"
     Title="Window1" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowState="Maximized">

     <Grid x:Name="LayoutRoot">
      <Grid.RowDefinitions>
       <RowDefinition Height="0.106*"/>
       <RowDefinition Height="0.894*"/>
      </Grid.RowDefinitions>
      <Rectangle Grid.Row="0" Grid.RowSpan="2" RadiusX="5" RadiusY="5">
       <Rectangle.Fill>
        <LinearGradientBrush EndPoint="0.497,1.196" StartPoint="0.5,0">
         <GradientStop Color="#FF333333" Offset="0.151"/>
         <GradientStop Color="#FF606060" Offset="1"/>
        </LinearGradientBrush>
       </Rectangle.Fill>
      </Rectangle>
      <Button HorizontalAlignment="Left" Margin="15,0,0,0" x:Name="cmdButton1 " VerticalAlignment="Center" Width="85" Content="Add Login"/>
      <Button HorizontalAlignment="Left" x:Name="cmdButton2" Width="85" Content="Add User" d:LayoutOverrides="Height" Margin="105,0,0,0" VerticalAlignment="Center"/>
      <Grid Grid.Row="1" x:Name="grdContainer"/>
     </Grid>
    </Window>
    On click on cmdButton1,Instance of LoginControl is created and added inside grdContainer
    On click on cmdButton2,Instance of AddUserControl is created and added inside grdContainer

    cmdButton1 Click
    Dim usrLoginControl as New LoginControl
    AddHandler ObjLocHost.EventtOnClose, AddressOf RemoveControl
    grdContainer.Children.Add(usrLoginControl)

    cmdButton2 Click
    Dim usrLoginControl as New LoginControl
    AddHandler ObjLocHost.EventtOnClose, AddressOf RemoveControl
    grdContainer.Children.Add(usrLoginControl)

    Private Sub RemoveControl(ByVal sender As Object, ByVal e As EventArgs)
            Try
                For IntLocCtr As Integer = 0 To GrdContainer.Children.Count - 1
      GrdContainer.Children.Clear()
                Next
            Catch ex As Exception

            End Try
        End Sub

    Both LoginControl and AddUser Control get added inside the grdContainer control and get removed also when the user clicks the close button present inside each of these controls.

    Although these controls are removed from the grid they still stay in memory and not garbage collected. After several weeks of searching i found that this is because of strong reference between controls and the window since
    the event handler is in the window and is not unregistered after removing from grid.
    But my in real application it is not possible to un hook  all the eventhandlers. I tried implementing weak event pattern but that too is too dificult and not possible at this stage.

    If i write the raiseevent in PreviewMouseLeftButtonDown event of buttons instead of Click event then the controls are garbage collected. I am not sure how to work out the scenario. I have installed all the latest service packs also. It is not possible to changes the architecture also. i.e my application consist of different modules in form of usercontrols. so its not possible to change them to wpf pages or use MVVM model architecture.

    requesting someone to help me out.

    THanks in advance

    Regards

    SAM

  • User profile image
    vesuvius

    You will either have to learn to use weak events or use MVVM. People use these design patterns and Classes for a reason.

     

    Your post is basically saying that you ignored the good advice out on the internet, don't want to learn anything complicated, and want to bypass the issues that bad design give you, but reap the benefits that MVVM gives.

     

    I also find it intriguing that your buttons are prefixed cmd but fail to use the commands readily available in WPF.

     

    If I were you, I would fix this application now, rather than try and create fixes that will make you pay an even bigger price by having an application full of very complicated code that is more complaex than MVVM and weak events together just because you wanted garbage collected controls.

     

    Not what you want to hear, I know that, but someone has to say it.

  • User profile image
    sam.pinnacle

    vesuvius said:

    You will either have to learn to use weak events or use MVVM. People use these design patterns and Classes for a reason.

     

    Your post is basically saying that you ignored the good advice out on the internet, don't want to learn anything complicated, and want to bypass the issues that bad design give you, but reap the benefits that MVVM gives.

     

    I also find it intriguing that your buttons are prefixed cmd but fail to use the commands readily available in WPF.

     

    If I were you, I would fix this application now, rather than try and create fixes that will make you pay an even bigger price by having an application full of very complicated code that is more complaex than MVVM and weak events together just because you wanted garbage collected controls.

     

    Not what you want to hear, I know that, but someone has to say it.

    Ok your suggestion taken. As you say i learn and implement weak events(Although i mentioned already that i have done that and its not that i don't understand it) for events that are made by me or defined by me. But can you please explain in the same code instead of raising the event on button click event, raise the event in preview events like PreviewMouseLeftButtonDown or  PreviewMouseLeftButtonUp event and the object (LoginControl or AddUserControl) gets garbage collected. 

     

    Something that should not be the case right. 

     

    Also could you please elaborate "I also find it intriguing that your buttons are prefixed cmd but fail to use the commands readily available in WPF." Acutally i didn't get you.

     

     

    Regards

    SAM

  • User profile image
    Wolfmann

    sam.pinnacle said:
    vesuvius said:
    *snip*

    Ok your suggestion taken. As you say i learn and implement weak events(Although i mentioned already that i have done that and its not that i don't understand it) for events that are made by me or defined by me. But can you please explain in the same code instead of raising the event on button click event, raise the event in preview events like PreviewMouseLeftButtonDown or  PreviewMouseLeftButtonUp event and the object (LoginControl or AddUserControl) gets garbage collected. 

     

    Something that should not be the case right. 

     

    Also could you please elaborate "I also find it intriguing that your buttons are prefixed cmd but fail to use the commands readily available in WPF." Acutally i didn't get you.

     

     

    Regards

    SAM

    Never mind him. Basically, what he is saying in his rude manner, is that he knows about a few things, and that he is going to use that knowledge only to patronize you. Or maybe he just don't know how to express himself politely. He knows the cmd prefix is something you've brought with you from the WinForm days and that you are not familiar with commands in WPF. There is no connection between the two and there is nothing intriguing about it. He has no call to be rude.

     

    Here is a link to a very good and clear article on MVVM and WPF: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090016 Don't miss downloading and studying the sample solution.

     

    Also, try other forums where people treat each other politely. (I guess you're on it already.)

  • User profile image
    spivonious

    Okay, so you have a window with two buttons. Click a button and it adds a user control and assigns the handler. Click a button in the user control and it signals the window to unload the control. Correct?

     

    If the event handler is really what's keeping it from being GCed, then remove the handler before you take it out of the visual tree.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.