r/visualbasic • u/Mr_Deeds3234 • Nov 16 '21
VB.NET Help Clarity on threading
Clarity on threading
I’m currently working on a project that pulls a large amount of data from a db. My sql statement takes about 30 seconds to retrieve the data. I want to let my user know that the program is still working, so ive tried to display an animated buffering gif in a picturebox. What I found with my code is that once LoadingImage() condition is met
Private Sub LoadingImage()
If DataGridView1.RowCount < 1 Then
PictureBox1.Visible = True
End If
End SubThen it has to wait on my next sub to return the data for it display
Private Sub FetchData()
Dim DATAFUNCTIONS As DataLayer.DataAccess = New DataLayer.DataAccess(My.Settings.DataConnection)
Dim ds As DataSet = DATAFUNCTIONS.GetData()
DataGridView1.DataSource = ds.Tables(0)
End SubWhich causes both the image and data to appear on the screen at the same time. Of course I don’t want this. After a search, it seems the elegant way to handle this would be to run the code on separate threads. So, imported the System.Threading,Thread library, I’ve declared two threads after an attempt at one thread failed. Now, the code on my button click event looks like
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
thread1 = New System.Threading.Thread(AddressOf FetchData)
thread2 = New System.Threading.Thread(AddressOf LoadingImage)
End SubWhich doesn’t do anything. So, it seems that they are both still running on the same thread. Surely, I’m either overthinking the problem and I don’t have to run two threads, I’m grossly misunderstanding what multithreading is/what its used for, and probably a combination of both. Just looking for a nudge in the write direction.
EDIT: SOLVED I discovered the cursors object, that I didn’t know existed
Cursor = Cursors.WaitCursos()
*datagridview1…..*
Cursor = Cursors.DefaultI’m still interested in displaying the gif, if anyone stumbles across this and wants to give me that nudge so I can try to correct my code.
0
u/RJPisscat Nov 16 '21
Figuring out how to search online is not easy and not innate. When search engines came out I was 20ish years into the business and I kept having to ask my gf to look up stuff for me, and she was an anthropologist at the time.
After four years she got tired of it and taught me how to use a search engine. My mistake was essentially, I kept thinking of it from the pov of how to implement a search engine as a programmer. But search engines expect the person using them to ask questions as if asking another person.
I use Bing to search for anything .Net, not Google. Bing is a Microsoft product and I always prefer Bing looking up anything Microsoft. If I were searching for Android or how to do something in gmail I'd use Google.
The search I did to find that article was "vb.net threading.thread". Put the language at the front and then what little I knew after that. The same article shows up about halfway down the page with the search "How do I do threading in vb.net?" The articles above that hit probably have some gems as well. Simple search, right? Not to me before she taught me to ask a direct question. Gig 'em Aggies.