Link to home
Start Free TrialLog in
Avatar of VAL1N
VAL1N

asked on

VB.NET Run existing functions in a background.

Hi, I have some functions that are sending requests to my webserver in order to obtain data,
however when those functions are called, form is freezing up because it's not run via backgroundworker.

Now I need to know what is the easiest way to call them functions without freezing a form.

Function example:

    Public Sub PreSave()
        Try
            Dim response As New String(SendRequest("presavecheck.php?id=" & PCID & "&date=" & TodaysDate))
            If response = "SAVENEWRECORD" Then
                SavetoDB()
            ElseIf response = "UPDATEEXISTINGRECORD" Then
                UpdateDB()
            End If
        Catch
        End Try
    End Sub

Open in new window


Maybe there is a way to create and start a backgroundworker with a functions name to run. For example:

MyBackgroundWorker.RunWorkerAsync(Presave())
so it would do whatever the function has to do and then after that I could run something like:
MyBackgroundWorker.CancelAsync

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of VAL1N
VAL1N

ASKER

Is it okay to define it once:

Private DataProcessingThread As Thread

and then run it multiple times like this:

DataProcessingThread = New Thread(AddressOf PreLoad)
        DataProcessingThread.IsBackground = True
        DataProcessingThread.Start()
        DataProcessingThread = New Thread(AddressOf PreSave)
        DataProcessingThread.IsBackground = True
        DataProcessingThread.Start()
Avatar of VAL1N

ASKER

My main concern is, if I will run it multiple times one after another, will it interrupt the previous operation?