0

I've got a timer which ticks every half a second. It calls a background process which renders text as icons and uses them for notification icons.

Public Shared DISCUS(9) As NotifyIcon
        Private Sub BackgroundProcess2_DoWork() Handles BackgroundProcess2.DoWork
           Try
              For counter As Integer = 0 To My.Computer.FileSystem.Drives.Count - 1
                    Dim FontColor As Color
                    If Math.Round((this / that) * 100, decimals:=0) < 25 Then
                        FontColor = Settings1.Default.CriticalColor
                    ElseIf Math.Round((this / that) * 100, decimals:=0) >= 25 And Math.Round((actualsize / ofsize) * 100, decimals:=0) < 50 Then
                        FontColor = Settings1.Default.WarningColor
                    Else
                        FontColor = Settings1.Default.NominalColor
                    End If
                    Dim BackColor As Color = Color.Transparent
                    Dim FontName As String = Settings1.Default.InterfaceFont.Name
                    Dim FontSize As Integer = 9
                    Dim iHeight As Integer = 16
                    Dim iWidth As Integer = 16
                    Dim objBitmap As New Bitmap(iWidth, iHeight)
                    Dim objGraphics As Graphics = Graphics.FromImage(objBitmap)
                    Dim objFont As New Font(FontName, FontSize)
                    Dim objPoint As New PointF(1, 0)
                    Dim objBrushForeColor As New SolidBrush(FontColor)
                    Dim objBrushBackColor As New SolidBrush(BackColor)
                    objGraphics.FillRectangle(objBrushBackColor, 0, 0, Width, Height)
                    objGraphics.DrawString(Text, objFont, objBrushForeColor, objPoint)
                    Dim img As Bitmap = objBitmap
                    Dim hIcon As IntPtr = img.GetHicon()
                    Dim icn As Icon = Icon.FromHandle(hIcon)
                    DISCUS(counter).Text = My.Computer.FileSystem.GetDriveInfo(My.Computer.FileSystem.Drives.Item(counter).ToString).VolumeLabel & vbCrLf & "Free space:" & vbCrLf & actualsize & stringvalue1 & " of " & ofsize & stringvalue2 & " (" & (Math.Round((this / that), decimals:=2) * 100) & "%)"
                    DISCUS(counter).BalloonTipText = Text
                    DISCUS(counter).Icon = icn
                    icn.Dispose()
                    DISCUS(counter).ContextMenuStrip = ContextMenuStrip1
                Next
            Catch ex as exception
                    My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.SpecialDirectories.Desktop & "\DT crash log.txt", "Log: " & My.Computer.Clock.LocalTime.Date.ToString & vbCrLf & o.ToString & vbCrLf & vbCrLf, True)
            End Try
        End Sub

Now for some reason, after a few minutes of the application being active, I get a JIT pop-up that doesn't render any text or buttons. Clicking on it causes the JIT debugger to crash, and I get the Windows "{App} has stopped responding." with error code CLR20R3. I'm not sure what this is all about, but I can provide any other details if needed. I've got everything nested in Try/Catch statements, all with code to write to a crash log but nothing gets written and I still get that error message.

Can anyone give me any pointers?

Yiu Korochko
  • 69
  • 1
  • 11

1 Answers1

1

I can't tell with certainty from your code snippet, but I suspect the issue is that you are attempting to update something on the user interface thread from the background thread. This will invariably lead to crashes like you are experiencing.

If you update the user interface, you must use Invoke on an object that is running on the UI thread. Here are a couple of links that may get you started on this:

How can I update my user interface from a thread that did not create it?

Updating the UI from a thread - The simplest way

competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • The second thread looks really really useful, I'm attempting to implement Invoke now... – Yiu Korochko Nov 04 '11 at 02:51
  • The second thread led me to the answer. I'm currently running my app to see if it crashes. If it doesn't I'll get to designing the icon and I'll put up a download link for this rather nifty utility! – Yiu Korochko Nov 04 '11 at 03:22
  • Ran into a GDI+ issue... http://www.mediafire.com/?pzn25f2tad1yl22 It doesn't occur immediately, but when it does even if I click "Continue" it just continuously happens... – Yiu Korochko Nov 04 '11 at 03:54
  • Could this be the issue you are having with GDI+: http://stackoverflow.com/questions/1772083/when-drawing-an-image-system-runtime-interopservices-externalexception-a-gener – competent_tech Nov 04 '11 at 16:31
  • nope, must be something else up. I'm creating the image programmatically, should I maybe render it to a file and then load it? – Yiu Korochko Nov 07 '11 at 02:06