0

我正在用 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()如果它仍然打开,则必须调用它。

4

1 回答 1

0

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

于 2013-06-03T19:45:51.633 回答