2015-08-25 48 views
0

我的項目包含調用SetSession ManyTime SomeTime它崩潰了。請任何人都可以幫助我。例外:試圖讀取或寫入受保護的內存

這部分代碼:

public Function SetSession(sSessionName As String) As Boolean 
Dim intCount As Integer 
'Call Refresh 
SetSession = False 
Dim oSessions As ExtaSessions = Nothing 
Dim oSession As ExtraSession = Nothing 
moSession = Nothing 
oSessions=TryCast(moSystem.Sessions,ExtraSessions) 
moSession=TryCast(oSessions.Item(sSessionName),ExtraSession)'=> Crashing in this line 
If Information.Err().Number <> 0 Then 
For intcount = 1 To oSessions.Count 
oSession = TryCast(oSessions.Item(intCount), ExtraSession) 
if oSession.Name = sSessionName then 
moSession = oSession 
Exit for 
End if 
NExt 
End if 
if moSession is Nothing then 
Exit Function End If 
SetSession = True 
End Function 

在後面附件PRINTSCREEN異常

enter image description here

回答

0

Finnaly我解決我的問題

變更前:

Dim oSessions As ExtaSessions = Nothing 
Dim oSession As ExtraSession = Nothing 
moSession = Nothing 
oSessions=TryCast(moSystem.Sessions,ExtraSessions) 
moSession=TryCast(oSessions.Item(sSessionName),ExtraSession) 

變更後:

Dim oSessions As EXTRA.ExtraSessions 
Dim oSession As EXTRA.ExtraSession 
moSession = Nothing 
moSystem = New Extra.ExtraSystem 
Try 
oSessions = DirectCast(moSystem.Sessions,EXTRA.ExtraSessions) 
moSession = DirectCast(oSessions.Item(sSessionName),EXTRA.ExtraSession) 
Catch ex As Exception 
Interaction.MsgBox(ex) 
End Try 

我只是初始化moSystem到Extra.ExtraSystem 和我改變ExtaSessions到EXTRA.ExtraSessions 和DirectCast到trycast和它的工作100%

THX :)

相關問題