PC Review


Reply
Thread Tools Rate Thread

Basic Threading question...

 
 
KC
Guest
Posts: n/a
 
      10th Aug 2004
Hey,

I'm trying to implement a cancel button in my app (this is after the bulk of
the program has been built). To do this I, of course, need to use a thread
to run the time consuming method while the UI continues on it's merry way. I
created a basic thread just as an initial test and I half expected it to
crash - and it did. My problem is I don't understand why and that's making
it hard to debug.

The whole thing crashes (I get the old 'object reference not set to an
instance of an object' error) after the method completes. The method that
I'm threading pulls data off a website, crunches some numbers, puts the
results in a datatable that is part of a dataset that is bound to a
datagrid.

Is it because I'm trying to update a control (which was created in the UI
thread)? That's the blank spot for me. I don't know what's suppose to happen
when my method/thread finishes.

I'm reading
http://msdn.microsoft.com/library/de...et09272002.asp
about background processes but my methods and functions are built and use a
lot of info from the UI. Trying to separate them into a 'worker' class looks
problematic. Ideas? Links to simpler way of implementing a cancel button?

---
Ken


 
Reply With Quote
 
 
 
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      10th Aug 2004
Hi,

Post some code

Ken
-----------------
"KC" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
Hey,

I'm trying to implement a cancel button in my app (this is after the bulk of
the program has been built). To do this I, of course, need to use a thread
to run the time consuming method while the UI continues on it's merry way. I
created a basic thread just as an initial test and I half expected it to
crash - and it did. My problem is I don't understand why and that's making
it hard to debug.

The whole thing crashes (I get the old 'object reference not set to an
instance of an object' error) after the method completes. The method that
I'm threading pulls data off a website, crunches some numbers, puts the
results in a datatable that is part of a dataset that is bound to a
datagrid.

Is it because I'm trying to update a control (which was created in the UI
thread)? That's the blank spot for me. I don't know what's suppose to happen
when my method/thread finishes.

I'm reading
http://msdn.microsoft.com/library/de...et09272002.asp
about background processes but my methods and functions are built and use a
lot of info from the UI. Trying to separate them into a 'worker' class looks
problematic. Ideas? Links to simpler way of implementing a cancel button?

---
Ken



 
Reply With Quote
 
KC
Guest
Posts: n/a
 
      10th Aug 2004
I was hoping you wouldn't ask that. Anyway, here are the essentials...

This first routine is from my run button. I declare a thread, set my cancel
variable (which the second routine uses), bind the table to a grid, create
and run the thread...

Private Sub butRunTrunk_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles butRunTrunk.Click
Dim t As Thread
cancelOperation = False 'set cancel operation switch
DataGrid10.SetDataBinding(germany, "TrunkBH")
t = New Thread(AddressOf trunkBHusage)
t.Start()
End Sub

This is the routine that is run in the thread. It very big and I just
included the part at the end where things come apart. I use to have the
binding part at the end of the routine, but I got an error that said
'Controls created on one thread cannot be parented to a control on a
different thread.' So I moved it to a point before the thread. That's where
I am now.

<MethodImpl(MethodImplOptions.Synchronized)> _
Private Sub trunkBHusage()

Try

[... lots of stuff that works ok...]

'Create columns
StatusBar1.Panels(0).Text = "Building datatable..."
If dt.Columns.Count = 0 Then
For x = 0 To UBound(trkColumn, 1)
dc = New DataColumn 'Create the first column
dc.DataType = System.Type.GetType(trkColumn(x, 1)) 'set
the data type
dc.ColumnName = trkColumn(x, 0) 'give the column a name
dc.ReadOnly = True 'make the column read only
dc.Unique = False 'column elements do not have to be
unique
dt.Columns.Add(dc) 'add the column to the table
created earlier
Next
End If

'insert data to table
For Each hkey In hPeakEr.Keys
junk = hkey.ToString.Split(New [Char]() {"@"c})

dr = dt.NewRow()
dr(0) = hMTX(hkey) 'MTX
dr(1) = junk(1) 'Trunk/BSC
dr(2) = hPeakBH(hkey).ToString 'Busy Hour
dr(3) = CDbl(hPeakEr(hkey)) 'Peak Erlangs
dt.Rows.Add(dr)
Next
StatusBar1.Panels(0).Text = "Table built..."

If dt.Rows.Count = 0 Then
StatusBar1.Panels(0).Text = "No 'SBS' trunks found for the
given MTX(s)."
Else
StatusBar1.Panels(0).Text = "Trunk BH Usage Done!"
End If

StatusBar1.Panels(0).Text = "Housekeeping..."
pbTUoverall.Value = 0
pbTUdaily.Value = 0
trkMenu.Enabled = True
butExpTrk.Enabled = True
elapsedTime(False)

Catch ex As Exception
' Let the user know what went wrong.
'MessageBox.Show(ex.Message)
errorMessages = ex.Message + vbCrLf + "subroutine: trunkBHusage"
+ vbCrLf + _
"Last status message: " + StatusBar1.Panels(0).Text + _
vbCrLf + Now.ToString + vbCrLf + vbCrLf
StatusBar1.Panels(0).Text = "See error log."
End Try

End Sub


"Ken Tucker [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> Post some code
>
> Ken
> -----------------
> "KC" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> Hey,
>
> I'm trying to implement a cancel button in my app (this is after the bulk

of
> the program has been built). To do this I, of course, need to use a thread
> to run the time consuming method while the UI continues on it's merry way.

I
> created a basic thread just as an initial test and I half expected it to
> crash - and it did. My problem is I don't understand why and that's making
> it hard to debug.
>
> The whole thing crashes (I get the old 'object reference not set to an
> instance of an object' error) after the method completes. The method that
> I'm threading pulls data off a website, crunches some numbers, puts the
> results in a datatable that is part of a dataset that is bound to a
> datagrid.
>
> Is it because I'm trying to update a control (which was created in the UI
> thread)? That's the blank spot for me. I don't know what's suppose to

happen
> when my method/thread finishes.
>
> I'm reading
>

http://msdn.microsoft.com/library/de...et09272002.asp
> about background processes but my methods and functions are built and use

a
> lot of info from the UI. Trying to separate them into a 'worker' class

looks
> problematic. Ideas? Links to simpler way of implementing a cancel button?
>
> ---
> Ken
>
>
>



 
Reply With Quote
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      10th Aug 2004
Hi,

You are probably better off using a delegates to execute a method
asynchronously than a thread. Here is an article from the msdn magazine
that shows how.

http://msdn.microsoft.com/msdnmag/is...asicInstincts/

Ken
-----------------------
"KC" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
I was hoping you wouldn't ask that. Anyway, here are the essentials...

This first routine is from my run button. I declare a thread, set my cancel
variable (which the second routine uses), bind the table to a grid, create
and run the thread...

Private Sub butRunTrunk_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles butRunTrunk.Click
Dim t As Thread
cancelOperation = False 'set cancel operation switch
DataGrid10.SetDataBinding(germany, "TrunkBH")
t = New Thread(AddressOf trunkBHusage)
t.Start()
End Sub

This is the routine that is run in the thread. It very big and I just
included the part at the end where things come apart. I use to have the
binding part at the end of the routine, but I got an error that said
'Controls created on one thread cannot be parented to a control on a
different thread.' So I moved it to a point before the thread. That's where
I am now.

<MethodImpl(MethodImplOptions.Synchronized)> _
Private Sub trunkBHusage()

Try

[... lots of stuff that works ok...]

'Create columns
StatusBar1.Panels(0).Text = "Building datatable..."
If dt.Columns.Count = 0 Then
For x = 0 To UBound(trkColumn, 1)
dc = New DataColumn 'Create the first column
dc.DataType = System.Type.GetType(trkColumn(x, 1)) 'set
the data type
dc.ColumnName = trkColumn(x, 0) 'give the column a name
dc.ReadOnly = True 'make the column read only
dc.Unique = False 'column elements do not have to be
unique
dt.Columns.Add(dc) 'add the column to the table
created earlier
Next
End If

'insert data to table
For Each hkey In hPeakEr.Keys
junk = hkey.ToString.Split(New [Char]() {"@"c})

dr = dt.NewRow()
dr(0) = hMTX(hkey) 'MTX
dr(1) = junk(1) 'Trunk/BSC
dr(2) = hPeakBH(hkey).ToString 'Busy Hour
dr(3) = CDbl(hPeakEr(hkey)) 'Peak Erlangs
dt.Rows.Add(dr)
Next
StatusBar1.Panels(0).Text = "Table built..."

If dt.Rows.Count = 0 Then
StatusBar1.Panels(0).Text = "No 'SBS' trunks found for the
given MTX(s)."
Else
StatusBar1.Panels(0).Text = "Trunk BH Usage Done!"
End If

StatusBar1.Panels(0).Text = "Housekeeping..."
pbTUoverall.Value = 0
pbTUdaily.Value = 0
trkMenu.Enabled = True
butExpTrk.Enabled = True
elapsedTime(False)

Catch ex As Exception
' Let the user know what went wrong.
'MessageBox.Show(ex.Message)
errorMessages = ex.Message + vbCrLf + "subroutine: trunkBHusage"
+ vbCrLf + _
"Last status message: " + StatusBar1.Panels(0).Text + _
vbCrLf + Now.ToString + vbCrLf + vbCrLf
StatusBar1.Panels(0).Text = "See error log."
End Try

End Sub


"Ken Tucker [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> Post some code
>
> Ken
> -----------------
> "KC" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> Hey,
>
> I'm trying to implement a cancel button in my app (this is after the bulk

of
> the program has been built). To do this I, of course, need to use a thread
> to run the time consuming method while the UI continues on it's merry way.

I
> created a basic thread just as an initial test and I half expected it to
> crash - and it did. My problem is I don't understand why and that's making
> it hard to debug.
>
> The whole thing crashes (I get the old 'object reference not set to an
> instance of an object' error) after the method completes. The method that
> I'm threading pulls data off a website, crunches some numbers, puts the
> results in a datatable that is part of a dataset that is bound to a
> datagrid.
>
> Is it because I'm trying to update a control (which was created in the UI
> thread)? That's the blank spot for me. I don't know what's suppose to

happen
> when my method/thread finishes.
>
> I'm reading
>

http://msdn.microsoft.com/library/de...et09272002.asp
> about background processes but my methods and functions are built and use

a
> lot of info from the UI. Trying to separate them into a 'worker' class

looks
> problematic. Ideas? Links to simpler way of implementing a cancel button?
>
> ---
> Ken
>
>
>




 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
basic threading question NoSpammer Microsoft VB .NET 2 10th Dec 2009 01:28 PM
Basic question about threading. GeezerButler Microsoft Dot NET Framework 2 5th Jun 2007 04:20 AM
Basic Threading question frankiespark@yahoo.com Microsoft VB .NET 19 17th May 2007 02:43 PM
Basic Threading Question Flashster Microsoft VB .NET 2 1st Dec 2005 01:13 PM
Basic ASP. Net threading question Peter Beck Microsoft ASP .NET 0 16th Mar 2004 03:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:23 AM.