Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Generic Error Occurred in GDI+" on Systray icon 1

Status
Not open for further replies.

JFoushee

Programmer
Oct 23, 2000
200
US
Hi. I've got an app that uses a Systray icon.
I have the icon doing an 'animation' every five seconds to indicate program is still running.

Icons are stored in a Forms.Imagelist collection (imglSystrayIcons).

I have a form timer to swap out the icons:
Code:
    Private Sub tmrSystrayIcon_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'as long as program is enabled
        If Me.chkSchedulerEnabled.Checked = True Then
            If Now.Second Mod 5 = 0 Then
                'show washed-out icon
                [COLOR=black yellow]Me.SystrayIcon.Icon = ImageToIcon(Me.imglSystrayIcons.Images(2))[/color]
            Else
                'show solid icon
                Me.SystrayIcon.Icon = ImageToIcon(Me.imglSystrayIcons.Images(1))
            End If
        End If
    End Sub

    Private Function ImageToIcon(ByVal image As System.Drawing.Image) As System.Drawing.Icon
        Try
            Return System.Drawing.Icon.FromHandle(New Bitmap(image).GetHicon)
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Function

The code works well for about the first hour.

Then the debugger stops on the highlighted line with the message "Generic Error Occurred in GDI+" .

Is this not a good technique to do this?
 
Are you running any sort of gargage collection? how much memory is your program using?

you may want to create a global icon variable and just set it to that, you are always generating the same images (either or) so there is no reason to keep retrieving a copy of it, especially not every couple seconds.


If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 
Thanks... I decided to build a form and load the images on it.

Code:
    Private Sub tmrSystrayIcon_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'as long as program is enabled
        If Me.chkSchedulerEnabled.Checked = True Then
            If Now.Second Mod 5 = 0 Then
                Me.SystrayIcon.Icon = ImageToIcon([COLOR=black yellow]frmIcons.imgIconStarted2.Image[/color])
            Else
                Me.SystrayIcon.Icon = ImageToIcon([COLOR=black yellow]frmIcons.imgIconStarted.Image[/color])
            End If
        End If
    End Sub

Seems like with global variables, they have the potential to "fall out" of memory after a long time.

Since this program is somewhat a scheduling system, I should probably have something more... non-memory based(?) by using a form.
 
I second Qik3Coder it tends to be a really bad idea and unnecessary to keep creating it over and over.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
If you create a CONST then it should be fine. What i was talking about is that you are constantly doing the Image to Icon conversion, on the same image If you create an icon const and set it equal to the image to icon value then you can speed up that processing by an insane amount, because then its a pointer change, not a new object.

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 
I wasn't aware I could build constants using functions!

Code:
Module modGlobals
    Class Globals
        Private Shared m_imgPgmIconStarted As Icon = ImageToIcon(frmIcons.imgPgmStarted.Image)
        Private Shared m_imgPgmIconStarted2 As Icon = ImageToIcon(frmIcons.imgPgmStarted2.Image)
        Private Shared m_imgPgmIconStopped As Icon = ImageToIcon(frmIcons.imgPgmStopped.Image)

        Public Shared ReadOnly Property StoppedIcon() As Icon
            Get
                StoppedIcon = m_imgPgmIconStopped
            End Get
        End Property
        Public Shared ReadOnly Property StartedIcon() As Icon
            Get
                StartedIcon = m_imgPgmIconStarted
            End Get
        End Property
        Public Shared ReadOnly Property StartedIcon2() As Icon
            Get
                StartedIcon2 = m_imgPgmIconStarted2
            End Get
        End Property
    End Class
End Module

The code has been aggitated to swap the icons more often.

I'll let you know how it goes.
 
I don't think you can actually set a const with a function.

It may not be a real CONST, but it does the job.


If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 
The fact it lasted through the night is proof enough for me. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top