GUI Applications - Speeding up! How?

Asked

Viewed 153 times

2

Hello, everybody!

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

For this I created a problem and would like to know if someone can solve. The solution will be applied to much 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 it in the background and still receive 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... something like this:

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

In fact it worked! But I did not return any reply...

Well, anyway, thank you in advance!

1 answer

1


Use of threads directly is advised against.

The ideal is to 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 an exception occurs in the background thread, it will be re-launched in the main thread with the use of await, Wait, or Result.

I’m not familiar with the syntax of VB.NET, but here’s how to do it in C# using Task.Run (deferring work to a thread from the thread pool):

bool result = await Task.Run(MinhaFuncao);

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


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

Await Task.Run(AddressOf MinhaFuncao);
  • I got it! This is a great solution expensive. It worked here!! Heheheheh About migrating to C#... I’ve even considered, but I was born in VB, I can’t leave now kkkkk And deep down the two share the same tools ^^

  • Solution: Dim response As Boolean = Await Task.Run(Of Boolean)(Function() (Minhafuncao))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.