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.
VAL1NAsked:
Who is Participating?
 
CodeCruiserConnect With a Mentor Commented:
You can use a thread instead and give it the name of the function to execute. It will execute the function and then just die.

http://support.microsoft.com/kb/315577

Alternatively, you can call this function from backgroundworker's DoWork event handler.
0
 
VAL1NAuthor Commented:
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()
0
 
VAL1NAuthor Commented:
My main concern is, if I will run it multiple times one after another, will it interrupt the previous operation?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.