Link to home
Start Free TrialLog in
Avatar of sgaggerj
sgaggerjFlag for United States of America

asked on

C# WPF: Unknown error when making code call from Library.

Hello.

I have a WPF project that I had to split sections into a library so that I could make the same project in C# for mono porting.

I will give the code in the code attach below.
As I am not too sure what the privacy is here. I can only give you problematic code.

Any help appreciated.
Answer that fixes this with the most understanding will be rewarded.

ScreenShots:
Library: http://prntscr.com/3xcb1
WPF App: http://prntscr.com/3xcbm

The WPF Application has Library as the dependancy.
This is the code within the WPF Application.
Before I seperated the code into the library, it used to be a seperate class in this file. http://prntscr.com/3xce6
 
..... other usings .....
using YAEBLibrary;
using System.Windows.Data;

namespace YAEBBinaryMgrWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        #region Variables
        #region public static RoutedCommands
        public static RoutedCommand RunBots = new RoutedCommand();
        public static RoutedCommand RemoveAccounts = new RoutedCommand();
        public static RoutedCommand MaintainBots = new RoutedCommand();
        public static RoutedCommand KillBots = new RoutedCommand();
        public static RoutedCommand FocusBots = new RoutedCommand();
        public static RoutedCommand RestartBots = new RoutedCommand();
        #endregion
        #region public static string StatusMessage
        public string StatusMessage
        {
            get { return MainClass._StatusMessage; }
            set { if (MainClass._StatusMessage != value) { MainClass._StatusMessage = value; NPC("StatusMessage"); } }
        }
        #endregion
        #region public int SelectedAccounts
        private int _SelectedAccounts;
        public int SelectedAccounts
        {
            get { return _SelectedAccounts; }
            set { if (_SelectedAccounts != value) { _SelectedAccounts = value; NPC("SelectedAccounts"); } }
        }
        #endregion
        #region public static int SelectedTheme
        public int _SelectedTheme;
        public int SelectedTheme
        {
            get { return _SelectedTheme; }
            set { if (_SelectedTheme != value) { _SelectedTheme = value; } }
        }
        #endregion
        #region public string NewAccountName
        private string _NewAccountName;
        public string NewAccountName
        {
            get { return _NewAccountName; }
            set { if (_NewAccountName != value) { _NewAccountName = value; NPC("NewAccountName"); NPC("IsValidNewAccountName"); } }
        }
        #endregion
        #region public bool IsValidAccountName
        public bool IsValidNewAccountName
        {
            get
            {
                return NewAccountName != null && NewAccountName.Length > 0 && Accounts.FirstOrDefault(ac => ac.AccountName == NewAccountName) == null;
            }
        }
        #endregion
        #region public string ExecutableName/File
        public string ExecutableName
        {
            get
            {
                return System.IO.Path.GetFileNameWithoutExtension(_ExecutableFile);
            }
        }
        private string _ExecutableFile;
        public string ExecutableFile
        {
            get { return _ExecutableFile; }
            set { if (_ExecutableFile != value) { _ExecutableFile = value; NPC("ExecutableFile"); NPC("ExecutableName"); } }
        }
        #endregion
        #region public string theVersion
        public String theVersion { get { return "rev: " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); } }
        #endregion
        #region public string ConfigLocation
        public object ConfigVersion = 1.4;
        public string _configFile = "config.cfg";
        public string _oldConfigFile = "config.txt";
        public string configLocation
        {
            get
            {
                return File.Exists(_configFile) ? _configFile : File.Exists(_oldConfigFile) ? _oldConfigFile : _configFile;
            }
        }
        #endregion
        #endregion

        private MainClass _mainclass = new MainClass();
        public MainClass MainClass { get { return _mainclass; } }
        public ObservableCollection<AccountInfo> Accounts { get; set; }

        public MainWindow()
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
            LoadConfig();
            FindProcesses();
            StatusMessage = "Ready";
            MainClass._timer = new Timer(OnUpdateProcessStats, null, 2000, 2000);
            InitializeComponent();
            Accounts = new ObservableCollection<AccountInfo>();
        }

Open in new window


WPF XAML design: http://prntscr.com/3xcg1
 
<Window x:Class="YAEBBinaryMgrWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:xtk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
        xmlns:local="clr-namespace:YAEBBinaryMgrWPF"
        xmlns:library="clr-namespace:YAEBLibrary;assembly=YAEBLibrary"
        Title="YAEB Binary Manager" Height="467" Width="429"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Loaded="Window_Loaded" Icon="/YAEBBinaryMgrWPF;component/Images/favicon.ico"
        MinHeight="250" MinWidth="298" Background="{DynamicResource WindowBackgroundBrush}"
        WindowStyle="ThreeDBorderWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
    <Window.Resources>
        <DataTemplate x:Key="AccountInfoTemplate">
            <Grid VerticalAlignment="Center" ShowGridLines="False">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="25"/>
                    <ColumnDefinition Width="55"/>
                    <ColumnDefinition Width="55"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <TextBlock TextDecorations="underline" Foreground="{DynamicResource TextBrush}" Text="{Binding AccountName}" ToolTip="{Binding AccountName}" DataContext="{Binding}" />
                <TextBlock Name="pct" Grid.Column="2" Foreground="{DynamicResource TextBrush}" Text="{Binding ProcessPct, StringFormat={}{0:F2}%}" HorizontalAlignment="Center" />
                <TextBlock Name="mem" Grid.Column="3" Foreground="{DynamicResource TextBrush}" Text="{Binding MemoryUsage, StringFormat={}{0:F2}M}" HorizontalAlignment="Center" />
                <Viewbox Name="mainticon" Grid.Column="4" Width="18" Height="18" HorizontalAlignment="Center">
                    <Grid Width="20">
                        <Image Source="/YAEBBinaryMgr;component/Images/maint.png" Width="20" Height="20" />
                    </Grid>
                </Viewbox>
                <Viewbox Name="runicon" Grid.Column="5" Width="18" Height="18" HorizontalAlignment="Center">
                    <Grid>
                        <Ellipse Width="20" Height="20" Stroke="#FF00A712" >
                            <Ellipse.Fill>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="#FF2B7945" Offset="0"/>
                                    <GradientStop Color="#FFB4E4C4" Offset="1"/>
                                </LinearGradientBrush>
                            </Ellipse.Fill>
                        </Ellipse>
                        <Path Data="M1.3515625,3.2804165 L7.1328125,9.0616665 L1.3515625,14.842916 z" Fill="#FF014E09" RenderTransformOrigin="0.5,0.5"  HorizontalAlignment="Center" Margin="2,1,0,0">
                            <Path.RenderTransform>
                                <TransformGroup>
                                    <ScaleTransform/>
                                    <SkewTransform/>
                                    <RotateTransform/>
                                    <TranslateTransform/>
                                </TransformGroup>
                            </Path.RenderTransform>
                        </Path>
                    </Grid>
                </Viewbox>
            </Grid>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Process}" Value="{x:Null}">
                    <Setter TargetName="runicon" Property="Visibility" Value="Hidden" />
                    <Setter TargetName="pct" Property="Visibility" Value="Hidden" />
                    <Setter TargetName="mem" Property="Visibility" Value="Hidden" />
                </DataTrigger>
                <DataTrigger Binding="{Binding AutoLoad}" Value="False">
                    <Setter TargetName="mainticon" Property="Visibility" Value="Hidden" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </Window.Resources>

Open in new window

<ListBox Grid.Row="3" Margin="8,8,8,29" Name="listBox1" TabIndex="3" IsTabStop="True" SelectionMode="Extended" ItemsSource="{Binding Path=Accounts}"
                     ItemTemplate="{StaticResource AccountInfoTemplate}" Background="Transparent" HorizontalContentAlignment="Stretch">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    </Style>
                </ListBox.ItemContainerStyle>
            </ListBox>

Open in new window


If I remove the ItemSource="{Binding Path=Accounts}" then it will run but obviously will not show the binding from the ObservableCollection.
Else it will give "object reference not set to an instance of an object" error

Library: AccountInfo.cs:
 
using System;
using System.Diagnostics;

namespace YAEBLibrary
{
    public class AccountInfo : Notifier
    {
        #region public string AccountName
        private string _AccountName;
        public string AccountName
        {
            [DebuggerStepThrough]
            get { return _AccountName; }
            set { if (_AccountName != value) { _AccountName = value; NPC("AccountName"); } }
        }
        #endregion
        #region public Process Process
        private Process _Process;
        public Process Process
        {
            [DebuggerStepThrough]
            get { return _Process; }
            set { if (_Process != value) { _Process = value; NPC("Process"); } }
        }
        #endregion
        #region public AutoLoad AutoLoad
        private bool _AutoLoad;
        public bool AutoLoad
        {
            [DebuggerStepThrough]
            get { return _AutoLoad; }
            set { if (_AutoLoad != value) { _AutoLoad = value; NPC("AutoLoad"); } }
        }
        #endregion
        #region public double MemoryUsage
        public double MemoryUsage { [DebuggerStepThrough] get { return (Process == null || Process.HasExited) ? 0.0 : Process.WorkingSet64 / (1024.0 * 1024.0); } }
        private double _lastProcessorTotal = 0.0;
        private DateTime _lastDateTime = DateTime.Now;
        private Double _ProcessPct;
        public Double ProcessPct { [DebuggerStepThrough] get { return _ProcessPct; } }
        public Double ProcessPctCalc
        {
            [DebuggerStepThrough]
            get
            {
                if (Process == null || Process.HasExited)
                {
                    return 0.0;
                }

                DateTime now = DateTime.Now;
                double span = (now - _lastDateTime).TotalSeconds;
                double retval = (Process.TotalProcessorTime.TotalSeconds - _lastProcessorTotal) / span * 100;
                _lastDateTime = now;
                _lastProcessorTotal = Process.TotalProcessorTime.TotalSeconds;
                _ProcessPct = retval;
                return retval;
            }
        }

        [DebuggerStepThrough]
        public void Refresh()
        {
            if (Process != null)
            {
                Process.Refresh();
            }
            _ProcessPct = ProcessPctCalc;

            NPC("MemoryUsage");
            NPC("ProcessPct");
        }

        [DebuggerStepThrough]
        public void Reset()
        {
            _lastProcessorTotal = 0.0;
            _lastDateTime = DateTime.Now;
        }
        #endregion
    }
}

Open in new window


I think that covers everything.
ASKER CERTIFIED SOLUTION
Avatar of Snarf0001
Snarf0001
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sgaggerj

ASKER

Thanks. That was not the issue.

I have done what you asked anyway.. I have narrowed it down to this:
 
#region Update Total Usage
        public double TotalMemoryUsage
        {
            //[DebuggerStepThrough]
            get
            {
                if (Accounts == null)
                    return 0.0;
                else
                    //return 0.0;
                    return Accounts.Sum(ac => ac.MemoryUsage);
            }
        }

        public double TotalCPUUsage
        {
            //[DebuggerStepThrough]
            get
            {
                if (Accounts == null)
                    return 0.0;
                else
                    //return 0.0;
                    return Accounts.Sum(ac => ac.ProcessPct);
            }
        }
        #endregion

Open in new window


Which is called every 2 seconds for updating this area:


hmm... looks like I have fixed it.... Let me role back some changes and see what I changed...

I double checked and made sure all resources were working, re-added as one was having issues.
Added using System.Drawing for bitmap on images.
Changed AssemblyName.

I also made changes to my dll embeder... even though I would love to get it to embed my library class:
 
[DebuggerStepThrough]
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            AppDomain.CurrentDomain.AssemblyResolve += (senders, args) =>
            {
                String resourceName = "YAEBBinaryMgrWPF.dlls." + new AssemblyName(args.Name).Name + ".dll";
                String assemblyName = Assembly.GetExecutingAssembly().FullName;
                String[] assemly = Assembly.GetExecutingAssembly().GetManifestResourceNames();
                Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
                using (stream)
                {
                    Byte[] assemblyData = new Byte[stream.Length];
                    stream.Read(assemblyData, 0, assemblyData.Length);
                    return Assembly.Load(assemblyData);
                }
            };
        }

Open in new window


ItemTemplate icon was changed to the new path. (From YAEBBinaryMgr to YAEBBinaryMgrWPF)

Added:
 
<StartupObject>YAEBBinaryMgrWPF.App</StartupObject>

Open in new window


Thats all I done.
Strange how it would give the error one minute and gone the next with only those changes.
Perhaps I just needed to make sure that all the foundations were correct.

Thank you for your help. As this is resolved and I used your change I will accept that solution ;)
Main thing is. It worked :P