Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.50/5 (4 votes)
See more:
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
Posted
Updated 23-Sep-13 7:18am
v2
Comments
[no name] 23-Sep-13 3:37am    
Use of background image or not is totally depends on your project requirement.
Mehdi Gholam 23-Sep-13 4:20am    
Background images and threads have nothing to do with each other unless you are doing something in your thread code, so post what you are doing for meaningful help.
pdoxtader 23-Sep-13 9:57am    
You're going to have to post what your background thread is doing, and the code you're using to update the background image for anyone to have enough information to answer you.
yoctokarthi 27-Jun-15 6:14am    
I Have Same Problem...how to fix this??

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.
 
Share this answer
 
Comments
msymarina 23-Sep-13 13:25pm    
If remove background image from form , the thread is running normally. just have the problem when add background image form.
i'm poor with Vb.net . Can you explain me with code ?
Dave Kreskowiak 23-Sep-13 18:58pm    
I don't have time to rewrite your code for you.
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
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900