2013-06-03 2 views
0

사용자가 제출할 일정 (전자 메일)을 나중에 보낼 수 있도록 VB.NET에서 응용 프로그램을 작성하고 있습니다. 특정 제출을 보낼 때까지 스레드를 기다리지 만, 어떤 이유 때문에 리스너 스레드에서 클래스 객체 중 하나에 액세스 할 수 없거나 (또는 ​​뭔가 다른 일이 일어나고 있습니다. 그게 내가 알아 내려는 것입니다.). 이유는 명확 New() 서브 루틴에서 만든 양식을 참조하도록 설정하면 WelcomeFormNothing입니다시작된 스레드에서 클래스 개체에 액세스하는 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 

위의 코드에서 몇 가지 코멘트 라인에 특별한주의를 지불 : 여기에 관련 코드인가? 양자 택일로 참조를 스레드로 MailSender에 보내는 것을 시도했지만, 같은 일이 일어났습니다. 스레드가 그 지점에 도착하기 전에 사용자가 양식을 닫을 수 있으므로 문이 If 필요합니다. 그러나 아직 열려있는 경우 RefreshSubmissions()을 호출해야합니다.

+0

스레드에서 RefreshSubmissions()를 직접 호출하면 안됩니다. GUI 스레드에 대한 호출을 마샬링하려면 Invoke()를 사용해야합니다 ... –

+0

'Invoke()'코드는'RefreshSubmissions()'자체에 있습니다. –

+0

하 ... 시원한 콩! 다행스럽게 생각 했어. –

답변

0

죄송합니다. 내 스레드가 내 응용 프로그램 코드의 다른 곳에서 중단되고 있다는 것을 깨달았습니다. 위에 게시 한 실제 코드에는 아무런 문제가 없습니다.

관련 문제