Very short after new Thread started Application Crash without err message

JPbachmannJPbachmann USMember
edited September 2014 in Xamarin.Android

Hey,

I tried to simply create a Thread to run async work, but short after the creation of that thread, the entire application crashes without any error or log. The last log i get is the Console.WriteLine i implemented:

Code:

        Console.WriteLine("Run Login Thread");
        var backgroundProcess = new Thread(this.Run);
        backgroundProcess.Name = "Sharpotify.Background";
        backgroundProcess.Start();

But not even the first line of Run will be executed. The only output i recive is:

...
Run Login Thread

09-28 21:50:23.384 I/mono-stdout( 3413): Run Login Thread

09-28 21:50:23.514 D/dalvikvm( 3413): GC_EXPLICIT freed 4K, 24% free 19998K/26012K, paused 6ms+5ms, total 35ms

09-28 21:50:23.514 D/Mono ( 3413): GC_OLD_BRIDGE num-objects 4 num_hash_entries 6 sccs size 6 init 0.00ms df1 0.21ms sort 0.00ms dfs2 0.37ms setup-cb 0.03ms free-data 0.06ms links 2/2/2/1 dfs passes 12/8

09-28 21:50:23.514 D/Mono ( 3413): GC_MAJOR: (mature allocation failure) pause 90.27ms, total 90.42ms, bridge 36.04ms major 4352K/16816K los 0K/0K

Thread started: Sharpotify.Background #2
The program 'Mono' has exited with code 0 (0x0).

Next thing is, i can not debug any line of code. I know this bug should be fixed but i am using the latest version and i am still not able to debug, if the debugger would run into a breakpoint, the program stops again with no error and ends with:

The program 'Mono' has exited with code 0 (0x0).

Posts

  • CheesebaronCheesebaron DKInsider, University mod

    Why don't you use Task and async/await? Also if you really want to use Thread why are you not using ThreadPool to help you?

  • JPbachmannJPbachmann USMember
    edited September 2014

    Because i want to create a long time Action that is listening to a network Port.
    OK when i try to use a Threadpool with:

                Console.WriteLine("Run Login Thread");
                ThreadPool.QueueUserWorkItem(s =>
                {
                    Console.WriteLine("Test");
                });
    

    the whole app is Freezing without err.
    Using a task causes the same strange behavior than a thread does:

                Console.WriteLine("Run Login Thread");
                Task.Run(() =>
                {
                    Console.WriteLine("Test");
                });
    

    No error and program end.

  • CheesebaronCheesebaron DKInsider, University mod

    I can't reproduce your problem. Where do you do this?

  • JPbachmannJPbachmann USMember
    edited September 2014

    Just inside the OnCreate of my MainActivity.

    I can create a blank Activity, start a thread and the Program Crash.

    Update:

                Console.WriteLine("Run Login Thread");
                ThreadPool.QueueUserWorkItem(s =>
                {
                    Console.WriteLine("Test");
                });
    

    produces a crash too

    Edit:

    I will reInstall Xamarin Completly.

  • JPbachmannJPbachmann USMember
    edited September 2014

    OK even after a complete Reinstall:

    Nether setting a breakpoint than starting a Thread,Task or a UserWorkItem does work!

    Edit:

    OK i Uninstalled my VisualStudio 2013 Ultimate Version. Then i tried to compile in VS2012 and it works fine at the first time ( Debugging and Thread creation), but then at the second Compile everything was like before and noting is working.
    And not even the simplest app does work ( Blank App Template ):

    using System;
    using System.Threading;
    using Android.App;
    using Android.Content;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.OS;
    
    namespace ThreadTestApp
    {
        [Activity(Label = "ThreadTestApp", MainLauncher = true, Icon = "@drawable/icon")]
        public class MainActivity : Activity
        {
            int count = 1;
    
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
    
                // Set our view from the "main" layout resource
                SetContentView(Resource.Layout.Main);
    
                ThreadPool.QueueUserWorkItem((s) =>
                {
                    Console.WriteLine("Test");
                });
            }
        }
    }
    
  • JPbachmannJPbachmann USMember
    edited September 2014

    Ok it seems to be a big problem with the Debugger.

    Example Application:

    [Activity(Label = "ThreadTestApp", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        int count = 1;
    
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
    
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
    
            var element = base.FindViewById<TextView>(Resource.Id.textView1);
    
            ThreadPool.QueueUserWorkItem((s) =>
            {
                element.Text = "WORK";
            });
        }
    }
    

    does show this window (Attached)

    But only when i Deploy the app and THEN start it. Where the heck is the Problem?

    Edit:

    OK skip that, When i do some long term work inside my thread the whole application freeze... the Action i give to QueueUserWorkItem are executed sequencel!

  • CheesebaronCheesebaron DKInsider, University mod
    ThreadPool.QueueUserWorkItem((s) =>
    {
        element.Text = "WORK";
    });
    

    You app might actually crash here, because you are trying to change the TextView from another thread. You can only update it from the thread you created it, which is the UI thread.

  • JPbachmannJPbachmann USMember

    ja that makes sense, but it works and that lead me to the only possible solution. Running a UserWorkItem causes that the action is executed synchronously

Sign In or Register to comment.