Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

TaskScheduler Problems

I hvae an issue where tasks do not get executed until other tasks complete (using Task.Run()). I can have tasks like the one below wait for an ideterminaent amount of time before executing.

var t=  System.Threading.Tasks.Task.Run (() => {
                Console.WriteLine("task started" );  
                });

when statement gets executed it results in a WaitingToRun status ( t.Status == "WaitingToRun")

There is not a lot of multi threading happening in my app using System.Threading.ThreadPool.GetAvailableThreads after the previous Task statement results in:

Worker threads: 198
Asynchronous I/O threads: 200

Any ideas would be helpful. This is running on an x86 two core device. Android 4.4
thanks,

Posts

  • markgamachemarkgamache USBeta ✭✭

    of course after I post I seem to have found the issue. Although, I am not sure why.

    I had a looped sync (blocking) read that was in a task. Once the app started executing this loop no more tasks would be run. They would all sit in the "WaitingToRun" state.

    while (bytesRead = mInputStream.Read (buffer, 0, buffer.Length)) > 0) {
    //do something with the bytes -- not blocking here
    } 
    

    switching this call to

       while (bytesRead = await mInputStream.ReadAsync(buffer, 0, buffer.Length)) > 0) {
        //do something with the bytes -- not blocking here
        } 
    

    fixed the issue. Note the steam was from an Android local socket. Also, this seems like a recent Xamarin change (last few months) as this is an old bit of code.

  • markgamachemarkgamache USBeta ✭✭

    I thought this was resolved, but it's not. I've had to remove any TPL code from my project and start threads manually. This problem started a few months ago with a Xamarin update (not sure which one -- after October 2015)

Sign In or Register to comment.