Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Convert code to use threading

Posted on 2008-06-13
2
Medium Priority
?
149 Views
Last Modified: 2012-05-05
I have a winform app that loads a large quantity of data into a datagrid at start, the apps responsiveness once the data is loaded is good, but after the uses logs in the app looks like it has hung, the login box becomes and empty white square and the main form doesnt load for up to a minute and an half while the dataset that feeds the datagrid is being loaded.  After a little research I think the best method to resolve this is to put the sub that fills the dataset an binds to the datagrid into its own thread (at this point if someone has a better approach please let me know).  With that said, I started trying to find a few examples that explained threading in a way I could implement into my code. What I found were a number of simple examples that left me confused (this makes absolutely no sense to me at all).  

This is roughly the code in my main form.

Private Sub Form1_Load(ByVal sender ....
          Get_Data()
          ... do other stuff
End Sub

Private Sub Get_Data()
      Dim strConn As String = CType(ConfigurationSettings.AppSettings("connectionString"), String)
      Dim ds As DataSet
      Dim distinctds As DataSet

      ds = SqlHelper.ExecuteDataset(strConn, "sp_camps_reg_players", Integer.Parse(lblActiveSeason.Text))

      Dim dt As DataTable
      Dim dt1 As DataTable
      dt = ds.Tables.Item(0)

      'DistinctList is creating by another sub that takes a datatable
      'and returns a dataset that has the duplicates removed from it
      distinctds = DistinctList(dt, "playerid")

      dt1 = distinctds.Tables.Item(0)
      dv = New DataView(dt1)
      DataGrid1.AutoGenerateColumns = False
      DataGrid1.DataSource = dv
      txtfilter.Focus()
End Sub

I am not sure if it matter but the above sub does call another sub (called DistinctList).  Any advice on how to approach this problem will be greatly appreciated.

Thanks,
Clint...
0
Comment
Question by:clintnash
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Accepted Solution

by:
AkisC earned 2000 total points
ID: 21785870
If you want to load something in the backgound use background worker
Just drag-drop a backgroundworker control on your form
doubleclick on it
You have the sub
Private Sub backgroundworker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles backgroundworker1.DoWork
///put your long to load code here///

Private Sub Form1_Load(ByVal sender ....
          backgroundworker1.RunWorkerAsync()
          ... do other stuff
End Sub

    Private Sub backgroundworker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles backgroundworker1.RunWorkerCompleted
       ///it comes here when bachground work is finished////
    End Sub
0
 
LVL 1

Author Closing Comment

by:clintnash
ID: 31467674
Thanks for the reply. I hit one small snag when I tried to implement it so I thought I would share this for others.  When I tried to implement  it as shown it generated an error, "Cross-thread operation no valid: Control Datagrid1 accessed from a thread other than the thread it was created on".  After a little research I changed the code the following and it works perfectly.  I changed the GetData sub to a function so it would return the datatable and used the following...

Private Function GetData()

        Dim strConn As String = CType(ConfigurationSettings.AppSettings("connectionString"), String)

      .. other code

        dv = New DataView(dt1)

        Return dv

End Function

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

        e.Result = GetData()

End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted

        Dim returnedData As DataView = TryCast(e.Result, DataView)

        DataGrid1.AutoGenerateColumns = False
        DataGrid1.DataSource = returnedData

        lblLoading.Text = "Data loaded successfully"
End Sub


Thanks again for your help,
Clint

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question