GUI Applications - Streamlined! As?

2

Hello, everyone!

My mission is to give agility and fluency to a software ... leave it without those constant fights in each For ...

For this I created a problem and would like to know if anyone can solve it. The solution will apply to a large part of my system

The function below is responsible for performing several complex calculations, and when called, locks the form completely. The challenge is to run this in the background and still get the answer: (true / false)

Public Function MinhaFuncao() As Boolean

    Try
        'Realiza os cálculos'

        Return True
    Catch ex As Exception

        Return False
    End Try

End Function

I've tried using Threads ... It looks something like this:

Dim minhaThread As Threading.Thread
minhaThread = New Threading.Thread(AddressOf MinhaFuncao)
minhaThread.IsBackground = True
minhaThread.Start()

In fact it worked! But no response came back to me ...

Well, thank you in advance!

    
asked by anonymous 26.05.2015 / 21:12

1 answer

1

Using threads directly is discouraged.

Ideally, you should use a high-level abstraction, such as Task " or Task<T> . These encapsulate logic that must be executed asynchronously, and propagate the return values.

In addition, they also propagate exceptions - if any exceptions occur in the background thread, they will be re-launched to the main thread with the use of await , Wait , or Result .

I'm not familiar with VB.NET syntax, but here's how to do it in C # using Task.Run (which defends the job for a thread in the pool thread):

bool result = await Task.Run(MinhaFuncao);

(Note: if this is a personal project, I advise you to use C # rather than VB.NET. It is much easier to find resources online and help materials for C #)

After a little research, I think the syntax in vb.net is:

Await Task.Run(AddressOf MinhaFuncao);
    
26.05.2015 / 23:36