Sam
Sam

Reputation: 43

VB.net MySQL connection check not working / slow

I have a program that's main function is to pull data from a MySQL database every x amount of time and adjust the GUI accordingly.

I have a two part problem in that the first thing that happens is that my GUI loads incredibly slowly in the background whilst the connection is being tried, you can literally see each label/box/image loading one by one until the MySQL check is complete.

A shoddy fix would be to add a me.Hide() before the function and then make it reappear after the results of the check have been displayed. Can you please take a look at the code? Will adding the MySQL check to a function and then calling it on Form1_Load help?

The second part of this is that my MySQL connection checker doesn't seem to work, now my host is accepting remote MySQL sessions and it does seem to think about connecting for a while... It's a good 6/7 seconds before the connection error message appears.

I pretty much followed guides and wrote the code myself, I understand each part but I think perhaps my ConnectionString is invalid.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        MaximizeBox = False

        Dim conn As MySqlConnection

        conn = New MySqlConnection
        conn.ConnectionString = "Server=50.28.x.x; Port=3306; User id=xxx_admin; password=xxxx; Database=xxx_software; Connect Timeout=60;"

        Try
            conn.Open()

        Catch myerror As MySqlException
            MsgBox("There was an error connecting to the MySQL Database")
            Label42.ForeColor = Color.Red
            Label42.Text = "ERROR"
            Timer1.Stop()
        End Try

        Timer1.Start()
        Label37.Text = Now.ToShortTimeString()

    End Sub

Upvotes: 0

Views: 1752

Answers (1)

igrimpe
igrimpe

Reputation: 1785

.Connect() is a blocking call, therefore you shouldn't put it into a sub that handles the Load event. Put it into Activated for example and check if it has run or not. You might also want to look into async/await which is officially available since VS2012. Though it's still not a good idea to put anything into Load that can cause an exception, because there is a problem with exception handling in this stage that occurs on 64bit machines running code in a 32bit version (to keep it simple).

The basic idea is to keep the UI-Thread (which handles the redrawing of your forms etc) free from "heavy" work, which means that the best results you will get when using some kind of "threading". And async/await is a very easy way to do it, because it keeps the usual program flow.

Public Class Form1

Private InitDone As Boolean = False

Private Async Sub Form1_Activated(sender As Object, e As EventArgs) Handles Me.Activated

    If Not InitDone Then
        InitDone = True
        ' simulate some blocking call
        Await Task.Run(Sub() Threading.Thread.Sleep(10000))
        Me.Text = "Ready to go"
    End If

End Sub

End Class

Async is a decorator which simply "allows" you to use the await keyword in a method. This is needed because await is/was not a reserved word in C# and otherwise it might break old code. The Await means that after the task has been scheduled, the UI-thread will continue and can redraw, process events etc. When the inner methode finished (usually in its own thread), the UI-thread "jumps" to the line directly after Await and executs that code. Nice an easy way to keep your app responsive AND to maintain a (seemingly) linear flow.

There is much more on async/await of course and you might need to invest some time to get used to it, but I would guess that it's worth any minute invested!

Upvotes: 1

Related Questions