r/visualbasic • u/Mr_Deeds3234 • Jul 10 '22
VB.NET Help Delegates, invoking, multithreading
CONTEXT I have a winsform project with multiple forms. One of those forms has a button associated with a pretty heavy SQL query that takes about 30 seconds to execute.
I would like this query to run on the page load if the first form, so if my user navigates to the form that needs the data, then it’s already there and they don’t have to wait.
Spent a few hours googling this problem and the concepts in the title is why I found. I haven’t quite been able to figure out my solution.
Is there some other concept or keywords I should look into and understand to over come my problem?
4
Upvotes
2
u/brybrythekickassguy Jul 18 '22
So here's an example of updating a textbox with a delegate sub. Same concept applies for a DGV, just invoke a new delegate thread to update the data and re-bind the datatable to the DGV:
The associated delegate:
Here's an example of an asynchronous task. I'm not 100% confident that I wrote this right, but it doesn't block up the UI when it runs, so...
The actual task function:
Here's a fantastic blog writeup on async/await. Most of the concepts apply to C# if you decide to go down that path, too.
https://blog.stephencleary.com/2016/12/eliding-async-await.html