Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / May 2006

Tip: Looking for answers? Try searching our database.

App Hang with BackgroundWorker

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JT - 11 May 2006 06:53 GMT
HI,
I am upgrading a Windows Forms app to ADO.NET 2.0 from 1.1.  I tried using a
background worker to load my dataset for a particular form in a separate
thread, being careful not to try to update the UI from this thread.  The form
loads and works well, but hangs when I hit the close button.  This behavior
does NOT occur in VS2005 when debugging, but does if the app is started
without debugging.

Any hints on what I may be doing wrong here?

Thanks.
Signature

John

Linda Liu [MSFT] - 11 May 2006 10:56 GMT
Hi John,

Thank you for posting.

Based on your description, I performed a test on this issue.  However, I
was unable to reproduce the problem of the program hanging.

Firstly I set up a project in VS.Net2003. I created a DataSet and a form in
the project. I put an instance of the DataSet ,a DataGrid, an
SqlDataAdapter instance onto the form. I  wrote code in the form's Load
event handler to invoke a thread to retrieve data from database to my
DataSet instance. The following code is picked out from my project.
     private void LoadData()
     {
              this.sqlDataAdapter1.Fill(this.dataset11.Persons);
      }
      private void Form1_Load(object sender, System.EventArgs e)
      {
    Thread thread = new Thread(new ThreadStart(LoadData));
    thread.Start();
       }
When running either with or without debugging, the form was loaded and
showed the Persons DataTable in the dataset1 with data retrieved from
database. Then I closed the form, the program exited properly.

Secondly I upgraded the VS.Net2003 project to VS2005. When running either
with or without debugging, the result turned out to be the same as in
VS.Net2003. The form was loaded and showed the data in the DataGrid. When I
closed the form, the program exited normally.

Is there any difference between your project and mine? If so, would you
please give me a detailed description or  related code in your project?

I am looking forward to your response. Thanks.

Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
JT - 11 May 2006 21:23 GMT
Hi Linda,
Thanks for your reply.  I am sending you a copy of the offending code.  I
appreciate your time on this...

Private Sub DeniedClaimsForm_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

       '' Start the asynchronous operation.
       BackgroundWorker1.RunWorkerAsync()

       Me.Cursor = Cursors.WaitCursor

       Me.SuspendLayout()

       Me.cmdPrint.Visible = True

       ConfigureClaimDataGrid()
       ConfigureSvcLineDataGrid()

       DisableDGAddNew(Me.dgDeniedClaims, Me)
       DisableDGDelete(Me.dgDeniedClaims, Me)

       Me.cmdClose.Focus()

       Me.ResumeLayout()

       Me.Cursor = Cursors.Default

   End Sub
   ' This event handler is where the actual work is done.
   Private Sub backgroundWorker1_DoWork( _
   ByVal sender As Object, _
   ByVal e As DoWorkEventArgs) _
   Handles BackgroundWorker1.DoWork

       ' Get the BackgroundWorker object that raised this event.
       Dim worker As BackgroundWorker = _
           CType(sender, BackgroundWorker)

       ' Assign the result of the data loading
       ' to the Result property of the DoWorkEventArgs
       ' object. This is will be available to the
       ' RunWorkerCompleted eventhandler.
       e.Result = Me.LoadDataSet(worker, e)

   End Sub 'backgroundWorker1_DoWork
   ' This event handler deals with the results of the
   ' background operation.
   Private Sub backgroundWorker1_RunWorkerCompleted( _
   ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) _
   Handles BackgroundWorker1.RunWorkerCompleted

       Try

           Me.Cursor = Cursors.Default

           ' First, handle the case where an exception was thrown.
           If Not (e.Error Is Nothing) Then

               LogExceptions(Me.Name, "RunWorkerCompleted", e.Error)

               MessageBox.Show(strDBerror, strSrvrError,
MessageBoxButtons.OK, MessageBoxIcon.Error)

               Me.Close()

           ElseIf CBool(e.Result) = False Then

               MessageBox.Show("There are no unresolved denied claims")

               Me.Close()

           Else
               Me.lbDeniedBal.Text = Me.ReturnTotalDeniedBalance

           End If

       Catch ex As Exception
           MessageBox.Show(ex.ToString)
       Finally
           Me.BackgroundWorker1.Dispose()
       End Try

   End Sub 'backgroundWorker1_RunWorkerCompleted
   
   Private Function LoadDataSet(ByVal worker As BackgroundWorker, ByVal e
As DoWorkEventArgs) As Boolean
       'Private Function LoadDataSet() As Boolean

       Dim cn As New SqlConnection(strConn)

       Try

           Me.daDeniedClms.SelectCommand.Connection = cn
           Me.daDeniedSvcLines.SelectCommand.Connection = cn

           With Me.DsDeniedClaims1

               .Clear()

               'worker.ReportProgress(25)

               cn.Open()

               ' Report progress as a percentage of the total task.
               If Me.daDeniedClms.Fill(.dtDeniedClaims) > 0 Then
                   'worker.ReportProgress(50)
                   Me.daDeniedSvcLines.Fill(.dtDeniedServiceLines)
                   'worker.ReportProgress(95)

               Else
                   cn.Close()

                   Return False
               End If

               If cn.State = ConnectionState.Open Then cn.Close()

               .EnforceConstraints = True

           End With

           Return True

       Catch ex As Exception
           LogExceptions(Me.Name, "LoadDataSet", ex)
           Return False
       Finally
           cn.Dispose()

       End Try

   End Function
Signature

John

> Hi John,
>
[quoted text clipped - 40 lines]
> from your issue.
> ====================================================
Linda Liu [MSFT] - 12 May 2006 06:06 GMT
Hi John,

Thank you for your quickly response.

I performed a test based on your code. Unfortunately, I couldn't reproduce
the problem you mentioned. The program didn't hang either with or without
debugging when I hit the close button on the form.

If possible, would you please set up a simple project which could just
reproduce the problem and send it to me? To get my actual email, please
remove the "online" from my displayed email address. Thanks.

Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
JT - 16 May 2006 02:39 GMT
Hi Linda,
I fixed my problem.  I basically moved the following code from Form_Load to
RunWorkerCompleted...

       Me.SuspendLayout()

       Me.cmdPrint.Visible = True

       ConfigureClaimDataGrid()
       ConfigureSvcLineDataGrid()

       DisableDGAddNew(Me.dgDeniedClaims, Me)
       DisableDGDelete(Me.dgDeniedClaims, Me)

       Me.cmdClose.Focus()

       Me.ResumeLayout()

       Me.Cursor = Cursors.Default

Works fine now.  Thanks.

John

> Hi John,
>
[quoted text clipped - 17 lines]
> from your issue.
> ====================================================
Linda Liu [MSFT] - 16 May 2006 06:25 GMT
Hi John,

Thank you for your response. I am glad to hear that the problem has been
fixed.

I think the problem may be cause by calling the ConfigureClaimDataGrid()
and ConfigureSvcLineDataGrid() methods in the form's load event handler.
Since the BackgroundWorker is dealing with the DataTable in your project,
there may be a conflict between the BackgroundWorker and
ConfigureClaimDataGrid() or ConfigureSvcLineDataGrid() methods.

If you have any other concerns or need anything else, please do not
hesitate to contact us. It is always our pleasure to be of assistance.

Have a nice day!

Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.