CLR20R3错误 - 文本到图像到图标渲染问题(包括线程) - VB.net

时间:2011-11-04 00:55:16

标签: vb.net multithreading background-process

我有一个计时器,每隔半秒就会打勾。它调用后台进程,将文本呈现为图标并将其用于通知图标。

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

现在由于某种原因,在应用程序处于活动状态几分钟后,我得到一个不呈现任何文本或按钮的JIT弹出窗口。单击它会导致JIT调试器崩溃,我得到Windows“{App}已停止响应”。错误代码CLR20R3。我不确定这是什么,但如果需要,我可以提供任何其他细节。 我已将所有内容嵌套在Try / Catch语句中,所有语句都包含写入崩溃日志的代码,但没有任何内容被写入,我仍然收到该错误消息。

任何人都可以给我任何指示吗?

1 个答案:

答案 0 :(得分:1)

我无法从您的代码段中确切地说出来,但我怀疑问题是您正在尝试从后台线程更新用户界面线程上的内容。这总会导致你遇到的崩溃。

如果更新用户界面,则必须对UI线程上运行的对象使用Invoke。以下是一些可以帮助您开始的链接:

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

Updating the UI from a thread - The simplest way