如果为vb.net添加表单中的背景图像,则线程运行缓慢 [英] thread running slow if add background image in form for vb.net

查看:97
本文介绍了如果为vb.net添加表单中的背景图像,则线程运行缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

线程运行缓慢,但如果我不使用快速运行的背景图像线程。



我需要使用背景图片在表格上,我该怎么办? ,这很紧急请帮忙。



这里我的代码:



thread running slow if add background image in vb.net ,But if i don't use background image thread running fast.

I necessary to use background image on form , How can i do ? , It's urgent Please help.

Here my code :

Imports System.Data.OleDb
Imports System.Threading
Public Class Lucky
    Dim CN As OleDbConnection
    Dim CM As OleDbCommand
    Dim DA As OleDbDataAdapter
    Dim DT As New DataTable
    Dim t1 As Thread
    Private Sub Lucky_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Row.Hide()
        Dim now As DateTime = DateTime.Now
        'Label2.Text = now.ToString("m")
        'Getdate = September 20
        Dim GetDate As String = now.ToString("m")
        'Split Getdate string to array / date_array(0)=September ; date_array(1)=20
        Dim date_array() As String
        'Split string where Space
        date_array = GetDate.Split(" "c)
        Dim dates, months As String
        months = date_array(0)
        dates = date_array(1)
        txtDate.Text = dates
        MsgBox(months)
       
        Me.CheckForIllegalCrossThreadCalls = False

        CN = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=database\db.accdb;Jet OLEDB:Database Password=12345;")
        CN.Open()
        CM = New OleDbCommand("SELECT * FROM TblPhoneNumber", CN)
        DA = New OleDbDataAdapter(CM)
        DA.Fill(DT)
    End Sub
    Sub PhoneThread()
        'Dim ThreadsArray As List(Of Threading.Thread) = New List(Of Threading.Thread)
        Dim s As Integer
        Dim num1, num2, num3, num4, num5, num6 As Char
        Dim luckyNumArray As String
        'MsgBox(DT.Rows.Count)
        For s = 0 To DT.Rows.Count
            'MsgBox(DT.Rows.Count)
            'MsgBox(s)
            If s = DT.Rows.Count Then
                s = 0
            Else

                'TextBox1.Text = DT.Rows(s)("Luckynum")
                luckyNumArray = DT.Rows(s)("Luckynum")
                num1 = luckyNumArray.Chars(0)
                num2 = luckyNumArray.Chars(1)
                num3 = luckyNumArray.Chars(2)
                num4 = luckyNumArray.Chars(3)
                num5 = luckyNumArray.Chars(4)
                num6 = luckyNumArray.Chars(5)
                If num1 = "1" Then
                    PicBoxNum1.Image = My.Resources._1
                ElseIf num1 = "2" Then
                    PicBoxNum1.Image = My.Resources._2
                ElseIf num1 = "3" Then
                    PicBoxNum1.Image = My.Resources._3
                ElseIf num1 = "4" Then
                    PicBoxNum1.Image = My.Resources._4
                ElseIf num1 = "5" Then
                    PicBoxNum1.Image = My.Resources._5
                ElseIf num1 = "6" Then
                    PicBoxNum1.Image = My.Resources._6
                ElseIf num1 = "7" Then
                    PicBoxNum1.Image = My.Resources._7
                ElseIf num1 = "8" Then
                    PicBoxNum1.Image = My.Resources._8
                ElseIf num1 = "9" Then
                    PicBoxNum1.Image = My.Resources._9
                ElseIf num1 = "0" Then
                    PicBoxNum1.Image = My.Resources._0
                End If

                If num2 = "1" Then
                    PicBoxNum2.Image = My.Resources._1
                ElseIf num2 = "2" Then
                    PicBoxNum2.Image = My.Resources._2
                ElseIf num2 = "3" Then
                    PicBoxNum2.Image = My.Resources._3
                ElseIf num2 = "4" Then
                    PicBoxNum2.Image = My.Resources._4
                ElseIf num2 = "5" Then
                    PicBoxNum2.Image = My.Resources._5
                ElseIf num2 = "6" Then
                    PicBoxNum2.Image = My.Resources._6
                ElseIf num2 = "7" Then
                    PicBoxNum2.Image = My.Resources._7
                ElseIf num2 = "8" Then
                    PicBoxNum2.Image = My.Resources._8
                ElseIf num2 = "9" Then
                    PicBoxNum2.Image = My.Resources._9
                ElseIf num2 = "0" Then
                    PicBoxNum2.Image = My.Resources._0
                End If

                If num3 = "1" Then
                    PicBoxNum3.Image = My.Resources._1
                ElseIf num3 = "2" Then
                    PicBoxNum3.Image = My.Resources._2
                ElseIf num3 = "3" Then
                    PicBoxNum3.Image = My.Resources._3
                ElseIf num3 = "4" Then
                    PicBoxNum3.Image = My.Resources._4
                ElseIf num3 = "5" Then
                    PicBoxNum3.Image = My.Resources._5
                ElseIf num3 = "6" Then
                    PicBoxNum3.Image = My.Resources._6
                ElseIf num3 = "7" Then
                    PicBoxNum3.Image = My.Resources._7
                ElseIf num3 = "8" Then
                    PicBoxNum3.Image = My.Resources._8
                ElseIf num3 = "9" Then
                    PicBoxNum3.Image = My.Resources._9
                ElseIf num3 = "0" Then
                    PicBoxNum3.Image = My.Resources._0
                End If

                If num4 = "1" Then
                    PicBoxNum4.Image = My.Resources._1
                ElseIf num4 = "2" Then
                    PicBoxNum4.Image = My.Resources._2
                ElseIf num4 = "3" Then
                    PicBoxNum4.Image = My.Resources._3
                ElseIf num4 = "4" Then
                    PicBoxNum4.Image = My.Resources._4
                ElseIf num4 = "5" Then
                    PicBoxNum4.Image = My.Resources._5
                ElseIf num4 = "6" Then
                    PicBoxNum4.Image = My.Resources._6
                ElseIf num4 = "7" Then
                    PicBoxNum4.Image = My.Resources._7
                ElseIf num4 = "8" Then
                    PicBoxNum4.Image = My.Resources._8
                ElseIf num4 = "9" Then
                    PicBoxNum4.Image = My.Resources._9
                ElseIf num4 = "0" Then
                    PicBoxNum4.Image = My.Resources._0
                End If

                If num5 = "1" Then
                    PicBoxNum5.Image = My.Resources._1
                ElseIf num5 = "2" Then
                    PicBoxNum5.Image = My.Resources._2
                ElseIf num5 = "3" Then
                    PicBoxNum5.Image = My.Resources._3
                ElseIf num5 = "4" Then
                    PicBoxNum5.Image = My.Resources._4
                ElseIf num5 = "5" Then
                    PicBoxNum5.Image = My.Resources._5
                ElseIf num5 = "6" Then
                    PicBoxNum5.Image = My.Resources._6
                ElseIf num5 = "7" Then
                    PicBoxNum5.Image = My.Resources._7
                ElseIf num5 = "8" Then
                    PicBoxNum5.Image = My.Resources._8
                ElseIf num5 = "9" Then
                    PicBoxNum5.Image = My.Resources._9
                ElseIf num5 = "0" Then
                    PicBoxNum5.Image = My.Resources._0
                End If

                If num6 = "1" Then
                    PicBoxNum6.Image = My.Resources._1
                ElseIf num6 = "2" Then
                    PicBoxNum6.Image = My.Resources._2
                ElseIf num6 = "3" Then
                    PicBoxNum6.Image = My.Resources._3
                ElseIf num6 = "4" Then
                    PicBoxNum6.Image = My.Resources._4
                ElseIf num6 = "5" Then
                    PicBoxNum6.Image = My.Resources._5
                ElseIf num6 = "6" Then
                    PicBoxNum6.Image = My.Resources._6
                ElseIf num6 = "7" Then
                    PicBoxNum6.Image = My.Resources._7
                ElseIf num6 = "8" Then
                    PicBoxNum6.Image = My.Resources._8
                ElseIf num6 = "9" Then
                    PicBoxNum6.Image = My.Resources._9
                ElseIf num6 = "0" Then
                    PicBoxNum6.Image = My.Resources._0
                End If
                'Label1.Text = num1
                'Label2.Text = num2
                'Label3.Text = num3
                'Label4.Text = num4
                'Label5.Text = num5
                'Label6.Text = num6
                Row.Text = s
                '   ThreadsArray.Add(t1)
                Thread.Sleep(19)
                'TextBox1.Text = s.ToString()
                Me.Refresh()
            End If
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        Timer1.Start()

        If Button1.Text = "Start" Then

            If Phonenum.Text = "" Then
                t1 = New Thread(AddressOf Me.PhoneThread)
                t1.Start()
                Button1.Text = "Stop"
            Else
                Phonenum.Show()
                Timer1.Stop()
                Timer2.Stop()
                t1.Resume()
                Button1.Text = "Stop"
            End If

        Else    'Click Stop

            t1.Suspend()
            Button1.Text = "Start"
            Phonenum.Text = DT.Rows(CInt(Row.Text))("Phone")
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Phonenum.Hide()
        Timer2.Enabled = True
        Timer1.Enabled = False
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Phonenum.Show()
        Timer2.Enabled = False
        Timer1.Enabled = True

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        t1.Abort()

        Me.Close()

    End Sub

End Class

推荐答案

首先,如果您正在使用 CheckForIllegalCrossThreadCalls ,那么您正在做的事情非常非常错误。查看该属性的名称并考虑它的含义。



您不会对后台线程或任何其他线程的任何UI控件感到困难UI(启动)线程。这有很好的理由。



不幸的是,解决问题的方法包括抓取您的代码并从头开始重新设计代码。你将计算代码或代码等待后台线程上的I / O操作,而不是用户交互。
First, if you're using CheckForIllegalCrossThreadCalls, you're doing something very VERY wrong. Look at the name of that property and think about what it means.

You do not EVER tough any UI controls from a background thread, or any other thread other than the UI (startup) thread. There are very good reasons for this.

Unfortunately, the solution to your problem involves scraping the code you have and redesigning your code from the ground up. You shove computational code or code that waits on I/O operations off the background threads, NOT user interaction.


Ok。我不知道是否有必要删除代码,但我愿意。但你不会知道你为什么要这样做,所以:



1.)看看使用...结束声明。这将结束并加快您的代码。



2.)您无法从后台线程访问UI控件。相反,我们使用DELEGATES。 这是一个关于如何使用它们的教程。基本上,您需要像函数一样调用委托,然后在该函数中使用control.InvokeRequired()切换到UI线程,然后才能访问UI控件。



3.)使用Camel Case和描述性变量,类和函数名称。不要害怕使用longs名称 - 这将使您的代码更具可读性。更容易理解。永远不要使用像s这样的变量名。



4.)最后,有更好的方法来访问My.resources中的图像然后所有那些用于if .. 。评估员。例如,您可以从数据库转换数字的字符串表示形式(即:Convert.ToInt32(luckyNumArray.Chars(0))),然后在将图像添加到ImageList控件(ImageList1)后通过它们的索引号访问图像。图片(index_number))。这将消除大部分混乱。



然后还有更多的东西......我只是在这里提出一些建议。



祝你好运。



- Pete
Ok. I don't know if scrapping the code is NECESSARY, but I would. But you won't unless you know why you should do so, so:

1.) Have a look at the "With... End With" Statement. This will neaten and speed up your code.

2.) You can't access UI controls from a background thread. Instead, we use DELEGATES. Here's a tutorial on how to use them. Basically, you need to call a delegate like a function, and then in that function you switch to the UI thread using control.InvokeRequired() before accessing the UI controls.

3.) Use Camel Case, and descriptive variable, class and function names. Don't be afraid to use longs names - this will make your code much more readable. Much easier to understand. NEVER use variable names like "s".

4.) Last, there are much better ways to access the images in My.resources then all those for if... else evaluators. For instance, you could convert the string representations of the number from the database ( Ie: Convert.ToInt32(luckyNumArray.Chars(0)) ), and then access the images via their index number after adding them to an ImageList control (ImageList1.Images(index_number)). This would eliminate most of that clutter.

And there are more things then this... I'm just making a few suggestions here.

Good luck.

- Pete


这篇关于如果为vb.net添加表单中的背景图像,则线程运行缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆