VB.NET从启动的线程访问类对象

时间:2013-06-03 19:03:33

标签: vb.net synchronization

我正在VB.NET中编写一个应用程序,允许用户安排在以后发送的提交(电子邮件)。我使用线程等到发送特定提交的时间是正确的,但由于某种原因,我无法从侦听器线程访问其中一个类对象(或者其他正在发生的事情,这就是我想要弄清楚的)。以下是相关代码:

Public Class AppContext
  Inherits ApplicationContext

  Private submsnMngr As SubmissionManager

  Public Sub New()
    submsnMgr = New SubmissionManager()
    menuAddEdit = New ToolStripMenuItem("Add/Edit Submissions")
    ...
  End Sub

  Private Sub menuAddEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Handles menuAddEdit.Click
    ' The user clicking this tray button is the ONLY way that the form can be shown
    submsnMngr.ShowWelcome()
  End Sub

  ...

End Class

Class SubmissionManager

  Public currentSubmissions As SubmissionList
  Public WelcomeForm As Welcome

  Public Sub ShowWelcome()
    If WelcomeForm Is Nothing Then
      ' Welcome is the form that needs to be refreshed down in the MailSender subroutine
      WelcomeForm = New Welcome(Me)
    End If
    WelcomeForm.Show()
  End Sub

  Public Sub CheckDates()
    For Each submsn In currentSubmissions.Submissions
      SyncLock submsn
        If Today.Date >= submsn.EffDate.AddDays(-90).Date And Not submsn.Sent90 And Not submsn.Denied90 And submsn.Thread Is Nothing Then
          submsn.Send(1)
          submsn.Sent90 = True
          currentSubmissions.Save()
        ElseIf Today.Date = submsn.EffDate.AddDays(-91).Date And submsn.Thread Is Nothing Then
          Dim thd As New Thread(AddressOf MailSender)
          thd.IsBackground = True
          submsn.Thread = thd
          Dim args As New ThreadArgs(submsn.Insured, 1)
          thd.Start(args)
        End If
        If Today.Date >= submsn.EffDate.AddDays(-60).Date And submsn.Thread Is Nothing Then
          submsn.Send(2)
          currentSubmissions.RemoveSubmission(submsn)
          If WelcomeForm IsNot Nothing Then
            WelcomeForm.RefreshSubmissions()
          End If
        ElseIf Today.Date = submsn.EffDate.AddDays(-61).Date And submsn.Thread Is Nothing Then
          Dim thd As New Thread(AddressOf MailSender)
          thd.IsBackground = True
          submsn.Thread = thd
          Dim args As New ThreadArgs(submsn.Insured, 2)
          thd.Start(args)
        End If
      End SyncLock
    Next
  End Sub

  Private Sub DateListener()
    Do
      CheckDates()
      Thread.Sleep(3600000)
    Loop
  End Sub

  Private Sub MailSender(args As ThreadArgs)
    Dim wait As New TimeSpan(14 - DateTime.Now.Hour, 23 - DateTime.Now.Minute, 0)
    Thread.Sleep(wait.TotalMilliseconds)

    Dim submsn As Submission = currentSubmissions.GetSubmission(args.insured)
    SyncLock submsn
      submsn.Send(args.mode)
      If args.mode = 1 Then
        submsn.Sent90 = True
        submsn.Thread = Nothing
        currentSubmissions.Save()
      Else
        currentSubmissions.RemoveSubmission(submsn)
      End If
    End SyncLock
    If WelcomeForm IsNot Nothing Then
      ' Here is the issue, this code is not being run, even though WelcomeForm is set
      ' in New() above
      WelcomeForm.RefreshSubmissions()
    End If
  End Sub

End Class

特别注意上面代码中的几条注释行,为什么WelcomeForm Nothing在我明确设置为引用New()子例程中创建的表单时?我尝试将MailSender线程的引用作为参数发送,但同样的事情发生了。请注意,我需要If语句,因为用户可能在线程到达该点之前关闭了表单。但是,如果RefreshSubmissions()仍处于打开状态,则必须对其进行调用。

1 个答案:

答案 0 :(得分:0)

对不起伙计们,我意识到我的帖子在我的应用程序代码的其他地方被中止了。我上面发布的实际代码没有问题。

相关问题