Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Xamarin.Forms stops all threads when minimized

$
0
0

Ok so I created an application in Xamarin.Android which has a Service running, this service has an instance off a Socket which recieved data at any given time data is sent from a server application. This works perfectly in my Xamarin.Android application even if the app is minimized. I used the same approach in Xamarin.Forms BUT when I do the same in Xamarin.Forms and I press the back button to minimize the app the Socket.Recieve(..); will get freezed (after a few seconds or a minute, it always differ) and eventually timeout EXCEPT when I re-enter the aplication and do a task that sends bytes to the server application then the Socket.Recieve(...) would resume with the bytes that was sent while it was freezed

I tried using a while loop inside the Task,
I tried using Thread runner = new Thread(Reader) with a while loop( without the Task )

Here is a code snippet

    private void Reader()
    {
        if (!IsOnline)
            return;
        try
        {
            var message = ReadMessage();
            if (message != null)
                MessageRecieved(message);
        }
        catch (ObjectDisposedException)
        {
            Dispose();
            OnDisconnected();
        }
        Task.Factory.StartNew(Reader);
    }

    private MessageResult ReadMessage()
    {
        var sizeBytes = ReadBytes();
        var size = BitConverter.ToInt32(sizeBytes, 0);
        var totalBytes = ReadBytes(size);
        var message = new MessageResult(totalBytes, Socket.Compressor);
        return message;
    }
    private byte[] ReadBytes(int size = 4)
    {
        var bytes = new byte[size];
        var total = 0;
        do
        {
            var current = Socket.Receive(bytes, total, size - total);
            if (current == 0)
            {
                throw new ObjectDisposedException("Socket Has Been Closed");
            }
            total += current;
            Debug.WriteLine("Client recieved {0} bytes", total);
        } while (total != size);
        return bytes;
    }

Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>