Question

I have a list view with an itemsource of a list. When I try to execute the following code, I get an InvocationException was unhandled by the calling class. If more code is required, I can add what is requested.

My goal is to be able to update the progress bar which is in the list view. The worker1.RunWorkerAsync() is called from a button click event.

XAML:

<Window x:Class="CS3000Config2.Dialogs.Clone"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Clone Scanner Configuration" Height="768" Width="1024" FontSize="16" WindowStyle="ToolWindow" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" Loaded="Window_Loaded">
<Window.Background>
    <LinearGradientBrush EndPoint=".5,1" StartPoint=".5,0">
        <GradientStop Color="#b8e1fc" Offset="0" />
        <GradientStop Color="#a9d2f3" Offset="0.10" />
        <GradientStop Color="#90bae4" Offset="0.25" />
        <GradientStop Color="#90bcea" Offset="0.37" />
        <GradientStop Color="#90bff0" Offset=".50" />
        <GradientStop Color="#6ba8e5" Offset=".51" />
        <GradientStop Color="#a2daf5" Offset=".83" />
        <GradientStop Color="#bdf3fd" Offset="1" />
    </LinearGradientBrush>
</Window.Background>

<DockPanel>
    <Grid>
        <DockPanel Height="650" Margin="50,40,50,0" Width="800" VerticalAlignment="Top" Background="White">
            <Grid>
                <ToolBar Height="46" Margin="0,6,-11,0" Name="toolBar1" VerticalAlignment="Top" ToolBarTray.IsLocked="True" BorderBrush="Black" BorderThickness=".5" Loaded="toolBar1_Loaded">
                    <Button ToolBar.OverflowMode="AsNeeded" Click="Button_Click">
                        <StackPanel  Orientation="Horizontal" VerticalAlignment="Center" >
                            <StackPanel.Resources>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="Margin" Value="10,0,0,0"/>
                                </Style>
                            </StackPanel.Resources>
                            <Image Source="/CS3000Config2;component/Resources/dsave.png" />
                            <TextBlock VerticalAlignment="Center" FontSize="16" Margin="5,0,0,0">Load To Selected Scanners</TextBlock>
                        </StackPanel>
                    </Button>
                    <Separator />
                    <Button ToolBar.OverflowMode="Never">
                        <StackPanel  Orientation="Horizontal" VerticalAlignment="Center" >
                            <StackPanel.Resources>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="Margin" Value="10,0,0,0"/>
                                </Style>
                            </StackPanel.Resources>
                            <Image Source="/CS3000Config2;component/Resources/document-save-all2.png" />
                            <TextBlock VerticalAlignment="Center" FontSize="16" Margin="5,0,0,0"> Load To All Scanners</TextBlock>
                        </StackPanel>
                    </Button>
                    <Separator />
                    <Button ToolBar.OverflowMode="Never" Click="Button_Click_1">
                        <StackPanel  Orientation="Horizontal" VerticalAlignment="Center" >
                            <StackPanel.Resources>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="Margin" Value="10,0,0,0"/>
                                </Style>
                            </StackPanel.Resources>
                            <Image Source="/CS3000Config2;component/Resources/refresh.gif" />
                            <TextBlock VerticalAlignment="Center" FontSize="16" Margin="5,0,0,0"> Refresh Scanner List</TextBlock>
                        </StackPanel>
                    </Button>
                </ToolBar>
                <ScrollViewer Margin="6.5,58,6,6" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" >
                    <Grid>
                        <ListView  Margin="0,5" Name="listView1" SelectionMode="Multiple" HorizontalContentAlignment="Center">
                            <ListView.View >
                                <GridView >
                                    <GridViewColumn Width="50" Header="">
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <Image Source="/CS3000Config2;component/Resources/cs3000-small.png" />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>
                                    <GridViewColumn Width="100" Header="Drive Letter" >
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Margin="5,0,0,0" HorizontalAlignment="Center" Text="{Binding Path=Drive}" TextAlignment="Center" />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>
                                    <GridViewColumn Width="200" Header="Model" >
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Margin="5,0,0,0" HorizontalAlignment="Center" Text="{Binding Path=Model}" TextAlignment="Center" />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>

                                    <GridViewColumn Width="200" Header="Serial Number" >
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Margin="5,0,0,0" HorizontalAlignment="Center" Text="{Binding Path=Serial}" TextAlignment="Center" />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>

                                    <GridViewColumn Width="225" Header="" >
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <ProgressBar  BorderBrush="Black" BorderThickness=".5" Width="200" Height="20" HorizontalAlignment="Center" Visibility="{Binding Path=isVisible}" Minimum="0" Value="{Binding Path=progress}" Maximum="{Binding Path=maxProgress}"/>
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>
                                </GridView>
                            </ListView.View>

                        </ListView>
                    </Grid>
                </ScrollViewer>
            </Grid>
        </DockPanel>

    </Grid>
</DockPanel>

C# Code:

private void worker1_DoWork(object sender, DoWorkEventArgs e)
    {
        Config tmpConfig = config;
        Config tmpConfig2 = new Config();
        Dispatcher.Invoke((Action)(() =>
            {

                foreach (Scanner s in listView1.SelectedItems)
                {
                    Int32 index = listView1.SelectedIndex;
                    tmpConfig2.LoadConfig(s.Drive + "\\Parameters\\Config.ini");
                    tmpConfig2.LoadSysInfo(s.Drive + "\\Parameters\\SYSINFO.TXT");
                    if (tmpConfig2.Model == "CS3070")
                    {
                        tmpConfig.BtName = tmpConfig2.BtName;
                        tmpConfig.BtPIN = tmpConfig2.BtPIN;
                    }
                    tmpConfig.WriteConfig(s.Drive + "\\Parameters\\Config.ini", tmpConfig);
                    String[] lines = File.ReadAllLines(s.Drive + "\\Parameters\\Config.ini");
                    s.maxProgress = lines.Length;
                    s.isVisible = Visibility.Visible;

                        for (int i = 0; i != lines.Length; i++)
                        {
                            items[index] = new Scanner() { Drive = s.Drive, Model = s.Model, isVisible = Visibility.Visible, progress = i + 1, maxProgress = lines.Length, Serial = s.Serial };
                            System.Threading.Thread.Sleep(10);

                            listView1.ItemsSource = null;
                            System.Threading.Thread.Sleep(10);
                            listView1.ItemsSource = items;
                        }


                }
            }));
    }
Was it helpful?

Solution

You can certainly do that, and MVVM is an unrelated topic here. Generally when you receive an invocation exception, the originating exception (and correlating call stack) was wrapped, and you should look to the inner exception.

In the event you (or someone else) is not familiar...

  • On the exception popup, at the bottom under the 'Actions' header, click the link "Veiw Detail..."

  • That will popup the 'View Detail' Dialog.

  • Next, open the details of the exception by (clicking the small triangle on the left or double click).
  • Select the 'InnerException' node, and open its details (the same way).

  • Continue to drill-down InnerExceptions until you find the originating exception.

The issue may be as simple as 'Object reference not set to an instance of an object'. However your basic strategy is fine and will work. Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top