Unhandled exception when adding tiles programmatically

3 posts, 0 answers
  1. Revital
    Revital avatar
    5 posts
    Member since:
    Jun 2013

    Posted 09 Aug 2015 Link to this post

    Hi,

    I'm relatively new at programming, so please bare with me.

    What I have:

    On load fetching image files from folder and creating tiles with those images.

    Also, I have an event handler when a file added to folder, to add a new tile.

    On load everything works fine and tiles are added. On event, the same sub that generates the tile fails in the "End Sub" with 

    "An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll" and I'm unable to understand what I'm doing wrong.

    My code: 

    Imports System.IO
    Imports System.Diagnostics
    Imports Telerik.WinControls
    Imports Telerik.WinControls.UI
    Imports System.Threading
    Imports System.ComponentModel


    Public Class GridWindow
        Public watchfolder As FileSystemWatcher
        Dim itemCol As New List(Of String)
        Dim fileCount As Int32

        Public Sub fillGrid()
            Try

                Dim watchfdr As New FileSystemWatcher
                watchfdr.Path = My.Settings.sourcefldr
                Dim txtFiles = Directory.GetFiles(watchfdr.Path, "*.*", SearchOption.TopDirectoryOnly).[Select](Function(nm) Path.GetFileName(nm))
                If txtFiles.Count > 0 Then
                    For Each filenm As String In txtFiles
                        itemCol.Add(filenm)
                    Next
                    fileCount = itemCol.Count()

                End If
                If itemCol.Count > 0 Then
                    For Each filenm In itemCol
                        AddNewImageTile(filenm)
                    Next
                End If
                watchfdr.NotifyFilter = IO.NotifyFilters.DirectoryName
                watchfdr.NotifyFilter = watchfdr.NotifyFilter Or _
                                           IO.NotifyFilters.FileName
                watchfdr.NotifyFilter = watchfdr.NotifyFilter Or _
                                           IO.NotifyFilters.Attributes

                AddHandler watchfdr.Created, AddressOf fileAdded
                watchfdr.EnableRaisingEvents = True



            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

        End Sub

     
        Private Sub fileAdded(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)

            Try
                If e.ChangeType = IO.WatcherChangeTypes.Created Then
                    Dim shortPath As String = e.FullPath.Substring(e.FullPath.LastIndexOf("\") + 1)
                    AddNewImageTile(shortPath)
                    'itemCol.Add(e.FullPath.ToString())
                   
                End If
            Catch ex As Exception
                MessageBox.Show("Exception in fileAdded: " & ex.Message)

            End Try

        End Sub


        Private Sub AddNewImageTile(ImagePath)
            Try
                Dim img As Bitmap = CType(Image.FromFile(My.Settings.sourcefldr.ToString & "\" & ImagePath, True), Bitmap)
                Dim newTile As New RadTileElement()
                newTile.AutoSize = True
                newTile.AutoSizeMode = Telerik.WinControls.RadAutoSizeMode.Auto
                newTile.GradientStyle = Telerik.WinControls.GradientStyles.Linear
                newTile.Image = img
                newTile.Padding = New System.Windows.Forms.Padding(1)
                newTile.Size = New System.Drawing.Size(214, 276)        
                newTile.Row = RadPanorama1.Items.Count Mod Me.RadPanorama1.RowsCount
                newTile.Column = RadPanorama1.Items.Count / Me.RadPanorama1.RowsCount
                newTile.Visibility = ElementVisibility.Visible
                Me.RadPanorama1.Items.Add(newTile)

            Catch ex As Exception
                MessageBox.Show("Exception in AddNewImage: " & ex.Message)
            End Try

        End Sub



        Public Sub New()

            ' This call is required by the designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.
            fillGrid()

        End Sub



    End Class

  2. Revital
    Revital avatar
    5 posts
    Member since:
    Jun 2013

    Posted 10 Aug 2015 in reply to Revital Link to this post

    Solved using the following article:

    http://www.blackwasp.co.uk/FileSystemWatcher.aspx

    The problem was in a buffer size. When the handler attempted to treat files too quickly, the buffer reached its limit. 

     ​

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 10 Aug 2015 Link to this post

    Hello Revital,

    Thank you for writing.

    Although, I was unable to replicate the issue locally, I am glad that the problem you were facing is now resolved.

    Should you have further questions I would be glad to help.
     
    Regards,
    Dess
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top