Threading causes Object Reference error

rogerm

Member
Joined
Apr 6, 2007
Messages
15
Programming Experience
3-5
I am using thread.abort in an application to allow user interface to end a process.In simple applications I can get it to work as intended but when used in the application of concern, I get an error "Form1, object reference not set to an instance of Object". This occures when it tries to call the 1st routine within the routine started by the thread.In a simple illistration below the error would occure when DoStuff() is called. Note Schedule(),DoStuff(),DoMoreStuff(), and DoLess() do not have any arguments.
VB.NET:
Dim T as Threading.Thread

Private Sub btnStart_Click()
  t=new Threading.thread(addressof Schedule)
  t.Start
End Sub

Private Sub btnCancel_Click()
  t.Abort
End Sub

Private Sub Schedule()
  ' stuff'
  DoStuff()
  DoMoreStuff()
  DoLess()
End Sub
Any direction on where I might start looking for my problem????
Thanks in advance.

My initial thaughts were maybe nested routines or recurrsions were the problem but simple trials with these do not seem to be a problem with threading.
 
Last edited by a moderator:
I have added several conditional statements prior to DoStuff(). I have tried to step through it several times and the error comes from the first couple of conditional statements (not necessarily from the same place) or when I first leave the Sub Schedule().
 
This is a stripped down version. At the end of btnDone2_Click() the thread starts Schedule(). Stepping off the the 1st line of the routine or the first couple of conditional statements is typicially where the Error occures.

Let me know if I need to send more.-Thanks for the replies&help.
VB.NET:
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Imports System.Collections
Imports System.Threading
Imports System.ComponentModel
Imports System.Windows.Forms
Public Class Form1
    Dim thd1 As Threading.Thread

    Private Sub btnDone2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDone2.Click
        Dim result As String
        Dim days As Integer
        Dim txt As String = "Calculating Schedule"
        Dim g As Graphics = pnlWoDur.CreateGraphics
        Dim brush1 As New System.Drawing.SolidBrush(Color.Black)
        Dim fntTxt As New Font("Times New Roman", 12, FontStyle.Bold)
        Dim nn As Integer
        Dim str As String
         pnlWoDur.Controls.Add(newProgBar)

        newProgBar.Top = 300
        newProgBar.Left = 200
        newProgBar.Anchor = AnchorStyles.Bottom
        ' Splitter2.Location = New Point(0, 488)
        newProgBar.Label1.BackColor = Color.LightGray
        newProgBar.lblTime.BackColor = Color.LightGray
        newProgBar.lblLocation.BackColor = Color.LightGray
        newProgBar.lblTime.Text = ""
        newProgBar.lblLocation.Text = ""
        'newProgBar.Refresh()

        ' Splitter2.Update()
        newProgBar.step = 1
        newProgBar.performStep()
        newProgBar.Visible = True
        newProgBar.CreateControl()
        'newProgBar.lblTime.Text = Now
        'newProgBar.lblTime.Refresh()
        newProgBar.Refresh()
        AddHandler newProgBar.btnAbort.Click, AddressOf abort
        newProgBar.btnAbort.Focus()

        Try
            _schStDate = System.Convert.ToDateTime(txtSt2.Text)
            _schEndDate = System.Convert.ToDateTime(txtEnd2.Text)
            days = _schEndDate.Subtract(_schStDate).Days
        Catch ex As Exception
            result = MessageBox.Show("Dates Not Valid", "Date Error", MessageBoxButtons.RetryCancel)
            If result = DialogResult.Retry Then
                txtSt2.Text = ""
                txtEnd2.Text = ""
                newProgBar.Visible = False
                Results1.lstResult2.Items.Clear()
                'pnlWoDur.Visible = False
                Exit Sub
            ElseIf result = DialogResult.Cancel Then
                txtSt2.Text = ""
                txtEnd2.Text = ""
                pnlSelTime.Visible = False
                newProgBar.Visible = False
                Results1.lstResult2.Items.Clear()
                'pnlWoDur.Visible = False
                Exit Sub
            End If
        End Try
        If _schStDate > _schEndDate Then
            result = MessageBox.Show("Dates Not Valid", "Date Error", MessageBoxButtons.RetryCancel)
            If result = DialogResult.Retry Then
                txtSt2.Text = ""
                txtEnd2.Text = ""
                newProgBar.Visible = False
                Results1.lstResult2.Items.Clear()
                Exit Sub
            ElseIf result = DialogResult.Cancel Then
                txtSt2.Text = ""
                txtEnd2.Text = ""
                pnlSelTime.Visible = False
                newProgBar.Visible = False
                Results1.lstResult2.Items.Clear()
                Exit Sub
            End If
        End If

        pnlSelTime.Visible = False
        _okToPaint = True
        '''-4-6-07
        'g.Dispose()
        '''
        _isChanged = True
        '''+/-6-1-07
       [B]   thd1 = New Threading.Thread(AddressOf Schedule)
         thd1.Start()
      [/B]  '''
        g.Dispose()
    End Sub


    Private Sub Schedule()
        Dim inputStart As String
        Dim inputEnd As String
        'lstResult.Items.Clear()
        If Results1.lstResult2.Items.Count > 0 Then
            Results1.lstResult2.Items.Clear()
        End If
        'need to clear woColl and Res coll here
        For i = WoColl.Count To 1 Step -1
            WoColl.Remove(i)
        Next
        For i = ResColl.Count To 1 Step -1
            ResColl.Remove(i)
        Next
        ' abort()
        _otDispCalc = False
        ReDim _otStAry(-1) 'clear arrays
        ReDim _otEndAry(-1)
        ReDim _otYRefAry(-1)
        ReDim _woTxtAry(-1)
        ReDim _woGrAry(-1)
        ReDim _WoLineAry(-1)
        ReDim _empTxtAry(-1)
        ReDim _empGrAry(-1)
        ReDim _empRLineAry(-1)
        ReDim _woGrAry2(-1)
        ReDim _empGrAry2(-1)
        _fillLocArys3 = True
        _fillLocArys4 = True
        _fillLocArys2 = True
        _fillLocArys = True
        _avTimeHash.Clear()
        _initFloatBaseLine = Nothing
        _initGroupK = Nothing
        ReDim _dltMaxAry(-1)

        _schStDateGhost = _schStDate
        GetResInfo() 'adds to resColl
       

        GetWoInfo() 'adds to woColl

        '''+2-25-07
        If WoColl.Count = 0 Then
            Results1.lstResult2.Items.Add("No Work Orders Selected for Schedule")
            dispSch1()
            Exit Sub
        End If
        '''
        CalculatePriority() 'calc fuzzy Pri
        SetLogDep() 'sets woColl up in order of logic/dependencies 
        
        
        dispSch1() 'displays res/avtime/work sch
        '''
        newProgBar.performStep()
        
        pnlWoDur.Controls.Remove(newProgBar)
        'added 10-29-06 to make emp sch ref start correctly
        _schStDate = _schStDateGhost
        '
    End Sub
end class
 
Back
Top