georgedumaine
Programmer
I am having a problem processing callback message in a windows service app. Bascially the program reads a text file with a list of video file names and plays them back via a dll that returns 2 messages I'm interested in. Here's what's going on and the relevant code:
If I put the line "Call mpgCallbackFunction(AddressOf CallBack)" in the OnStart routine I never get any messages back. So, I put the statement into the timer elapsed event. Note that the timer continues to fire but that it only actually does something the 1st time it fires. After that it just hangs around so my service doesn't die. I dispose of it in the OnStop routine. (Is this the best way to do this ... any other ideas?). Anyway, with this structure I get the 1st callback message and the app dies a horrible death. I think I have it structured wrong and I think it has to do with where I put "Call mpgCallbackFunction(AddressOf CallBack)". I have also tried putting it in the InitCard() routine but that doesn't seem to help. Anything help would be greatly appreciated.
Delegate Sub CallBackDelegate(ByVal CardNum As Integer, ByVal ChanNum As Integer, ByVal Message As Short)
Declare Sub mpgCallbackFunction Lib "SomePlayback.dll" (ByVal ptrFunction As CallBackDelegate)
'declare callback function
Protected Overrides Sub OnStart(ByVal args() As String)
gLogFile = "C:\MyService.log"
gPlaylist = "C:\MyService.mpl"
gStopService = False
gStartService = True
Timer1.Enabled = True
End Sub
Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
If gStartService And Not gStopService Then
WriteToLog(0, 0, "***********************************************")
WriteToLog(5, 0, "Service Started.")
play_fNum = FreeFile()
FileOpen(play_fNum, gPlaylist, OpenMode.Input, OpenAccess.Read, OpenShare.Shared)
Call WriteToLog(0, 0, "Opened: " & gPlaylist)
Call Init_Card()
Call mpgCallbackFunction(AddressOf CallBack)
'This loads the first file and plays it, then the call back queue's subsequent files
gStartService = False 'because it's already started now so don't do this again.
Dim mFileToPlay As String = ""
Dim mImgFile As String
Call GetNextPLEntry(mPl)
If mPl.ContentID > 0 Then
mFileToPlay = GetCatalogEntry(mPl.ContentID)
mImgFile = GetCatalogEntry(mPl.ImageID)
Call DecPlay(mFileToPlay, mPl)
End If
End If
End Sub
Public Sub CallBack(ByVal CardNum As Integer, ByVal ChanNum As Integer, ByVal Message As Short)
'handle the messages
Select Case Message
Case MPG_FILE_PLAY_COMPLETE
WriteToLog(0, 0, "MPG_FILE_PLAY_COMPLETE")
gMPG_FILE_PLAY_COMPLETE = True
Case MPG_FILE_READ_START
WriteToLog(0, 0, "MPG_FILE_READ_START")
Call GetNextPLEntry(mPl)
If mPl.ContentID > 0 Then
Call WriteToLog(0, 0, "About to queue: " & GetCatalogEntry(mPl.ContentID))
Call mpgLoadNext(gCard, gZone, GetCatalogEntry(mPl.ContentID))
'mImgFile = GetCatalogEntry(mPl.ImageID)
Else
'We are done with the playlist
Call WriteToLog(0, 0, "Done with the playlist")
Call mpgStop(gCard, gZone)
Call mpgCloseDriver()
End If
Case Else
WriteToLog(0, 0, "Received message with value of " & Message)
End Select
End Sub
If I put the line "Call mpgCallbackFunction(AddressOf CallBack)" in the OnStart routine I never get any messages back. So, I put the statement into the timer elapsed event. Note that the timer continues to fire but that it only actually does something the 1st time it fires. After that it just hangs around so my service doesn't die. I dispose of it in the OnStop routine. (Is this the best way to do this ... any other ideas?). Anyway, with this structure I get the 1st callback message and the app dies a horrible death. I think I have it structured wrong and I think it has to do with where I put "Call mpgCallbackFunction(AddressOf CallBack)". I have also tried putting it in the InitCard() routine but that doesn't seem to help. Anything help would be greatly appreciated.
Delegate Sub CallBackDelegate(ByVal CardNum As Integer, ByVal ChanNum As Integer, ByVal Message As Short)
Declare Sub mpgCallbackFunction Lib "SomePlayback.dll" (ByVal ptrFunction As CallBackDelegate)
'declare callback function
Protected Overrides Sub OnStart(ByVal args() As String)
gLogFile = "C:\MyService.log"
gPlaylist = "C:\MyService.mpl"
gStopService = False
gStartService = True
Timer1.Enabled = True
End Sub
Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
If gStartService And Not gStopService Then
WriteToLog(0, 0, "***********************************************")
WriteToLog(5, 0, "Service Started.")
play_fNum = FreeFile()
FileOpen(play_fNum, gPlaylist, OpenMode.Input, OpenAccess.Read, OpenShare.Shared)
Call WriteToLog(0, 0, "Opened: " & gPlaylist)
Call Init_Card()
Call mpgCallbackFunction(AddressOf CallBack)
'This loads the first file and plays it, then the call back queue's subsequent files
gStartService = False 'because it's already started now so don't do this again.
Dim mFileToPlay As String = ""
Dim mImgFile As String
Call GetNextPLEntry(mPl)
If mPl.ContentID > 0 Then
mFileToPlay = GetCatalogEntry(mPl.ContentID)
mImgFile = GetCatalogEntry(mPl.ImageID)
Call DecPlay(mFileToPlay, mPl)
End If
End If
End Sub
Public Sub CallBack(ByVal CardNum As Integer, ByVal ChanNum As Integer, ByVal Message As Short)
'handle the messages
Select Case Message
Case MPG_FILE_PLAY_COMPLETE
WriteToLog(0, 0, "MPG_FILE_PLAY_COMPLETE")
gMPG_FILE_PLAY_COMPLETE = True
Case MPG_FILE_READ_START
WriteToLog(0, 0, "MPG_FILE_READ_START")
Call GetNextPLEntry(mPl)
If mPl.ContentID > 0 Then
Call WriteToLog(0, 0, "About to queue: " & GetCatalogEntry(mPl.ContentID))
Call mpgLoadNext(gCard, gZone, GetCatalogEntry(mPl.ContentID))
'mImgFile = GetCatalogEntry(mPl.ImageID)
Else
'We are done with the playlist
Call WriteToLog(0, 0, "Done with the playlist")
Call mpgStop(gCard, gZone)
Call mpgCloseDriver()
End If
Case Else
WriteToLog(0, 0, "Received message with value of " & Message)
End Select
End Sub