Reading messages freezes app...
#1
Can anyone tell me where I'm going wrong here please....

Code:
Try
            If (xbmcClient.Connected) Then
                xbmcStream = xbmcClient.GetStream()
                If xbmcStream.CanRead Then
                    Dim myReadBuffer(1024) As Byte
                    Dim myCompleteMessage As StringBuilder = New StringBuilder()
                    Dim numberOfBytesRead As Integer = 0
                    Do
                        numberOfBytesRead = xbmcStream.Read(myReadBuffer, numberOfBytesRead, myReadBuffer.Length)     <-- FREEZES here...
                        myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead))
                    Loop While xbmcStream.DataAvailable
                    xbmcMethod = jsonParser(myCompleteMessage.ToString(), "method")
                    Select Case xbmcMethod
                        Case "Player.OnPlay"
                            debugMessage("xbmcMethod: Player.OnPlay")
                            currentState = "Playing..."
                            statusWindow.ShowDialog()

                        Case "Player.OnPause"
                            debugMessage("xbmcMethod: Player.OnPause")
                            currentState = "Paused..."
                            statusWindow.ShowDialog()

                        Case Else
                            debugMessage("xbmcMethod: " + xbmcMethod)
                    End Select
                Else
                    debugMessage("Unable to read from XBMC Stream")
                End If
            Else
                xbmcConnect()
            End If
        Catch ex As Exception
            debugMessage("xbmcMethod Exception: " + ex.Message)
        End Try

I have the above code but everytime it tries to read data (as indicated) the program just freezes and never completes the read instruction.

If XBMC isn't running there is no issue as obviously there is no connection but if it IS running, it freezes until there is some activity in XBMC; by activitiy I mean a notification like Player.OnPlay or Player.OnPause. Obviously, what's happeneing is a synchronous read rather than an asynchronous one so how can I change it? The code is called from a timer which has an interval of 1 second (1000 ms).

EDIT: Nevermind. Think I had a lapse in logic and added a ReadTimeout.
Image
Reply
#2
You should not have ReadTimeout for notifications ....

The point of notifications is to wait for incoming message until one comes you should have a thread running in background that do the wait and react to notifications.

A timer should only do real json queries that will gives a direct result, like Player.GetActivePlayers of GetProperties.
Reply
#3
(2012-08-02, 12:07)Tolriq Wrote: You should not have ReadTimeout for notifications ....

The point of notifications is to wait for incoming message until one comes you should have a thread running in background that do the wait and react to notifications.

A timer should only do real json queries that will gives a direct result, like Player.GetActivePlayers of GetProperties.

Yes, I agree. I should have updated my post as the ReadTimeout has now been removed. What I'm having an issue with now is when I send a request (like GetProperties) the response isn't instant so I'm putting in a do while not stream.DataAvailable loop with a thread sleep of 100ms and a break clause which seems to do the job. I'm still new to TCP stuff so I'm still learning... Thanks for any tips/help you can offer. I do appreciate them!
Image
Reply
#4
Well this is not only true for TCP but when you do anything that might take long (i mean usually more than 350ms) you should not do that on the main thread since it will lock the UI and the user will see it as unresponsive.

For JSON there's something cool about request it's the Id you send is send back with the response (and notification don't have ids).

So you should really start to look at threads and callbacks (don't know how they work in VB but this a so common way that there must be toons of samples).
Reply
#5
(2012-08-03, 10:04)Tolriq Wrote: Well this is not only true for TCP but when you do anything that might take long (i mean usually more than 350ms) you should not do that on the main thread since it will lock the UI and the user will see it as unresponsive.

For JSON there's something cool about request it's the Id you send is send back with the response (and notification don't have ids).

So you should really start to look at threads and callbacks (don't know how they work in VB but this a so common way that there must be toons of samples).

I did initially look at threading but I didn't like the unhandled exceptions generated when using thread.abort. I'm thinking of going back to that route though as I'm currently in a situation where my program, as you quite rightly state, holds up the UI whilst waiting for a message to be responded to. I sincerely appreciate your assitance with this, given that you have YATSE out there!

One thing I forgot to ask is, is it advisable to use HTTP to get media information or just use TCP? I noticed that using HTTP seems to produce more reliable (and less "hangs") or do you think it might just be the way I'm doing the TCP calls?
Image
Reply
#6
Yatse 2 For Windows had it's great days Smile If you can do better or something different there's no problem competition is there for better end user life.

You can do HTTP to get media information it works quite well (and you have to use http to get images) but http is a little slower and it's a little less stable in current Xbmc.
Reply
#7
(2012-08-03, 17:57)Tolriq Wrote: Yatse 2 For Windows had it's great days Smile If you can do better or something different there's no problem competition is there for better end user life.

You can do HTTP to get media information it works quite well (and you have to use http to get images) but http is a little slower and it's a little less stable in current Xbmc.

The way I am getting images at the moment is to get whichever backdrop is displayed in XBMC and just using that exact same file from the XMBC user folder. This way there is no downloading required, just an image load, but this only works on a remote on the same PC, which is what this program is intended for.

What I'm a bit concerned about right now is the fact that the only way you can send key presses (as far as I can tell) is to use the old HTTP API which will obviously change in the next release so I'm hesitant on releaseing it too early only to have to re-release it as soon as the next version of XBMC is released.

I think I'm going to rewrite my code and put notifications back in a thread and handle information getting by my old method using HTTP which seamed to work quite well.
Image
Reply
#8
By HTTP I meant JSON over http not old HTTP api Smile

For images you should already test Frodo alphas since images values send back from Xbmc have changed and your method may not works anymore .
Reply
#9
Can anyone give me a pointer as to why the following is happening? Is there a limit on how many requests XBMC can handle?

Code:
[Friday, 10 August, 2012 00:30:38]: DEBUG: postXBMCRequest: postData: {"jsonrpc":"2.0","method":"Player.GetActivePlayers","id":"0"}
[Friday, 10 August, 2012 00:30:38]: DEBUG: postXBMCRequest: ReceivedData: {"id":"0","jsonrpc":"2.0","result":[]}

The bit I'm interested in is the "result":[] ....

Everything after that returns, well, Failed to execute method as it's obviously not getting the playerid.

EDIT: Nevermid, I figured out what was going on. A stopped player returns an active player but you can't get information about the media (like position, media name etc.) as it returns null information
Image
Reply

Logout Mark Read Team Forum Stats Members Help
Reading messages freezes app...0