0

I'm trying to run some code in a new thread because I'm noticing a slowness on my device. It is compiling ok but at the app starting, it freezes and terminates with the message (MyApp is presenting errors constantly).

What I'm doing wrong?

using System.Threading.Tasks;

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace MyApp
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
        }

        protected override void OnStart()
        {
            new System.Threading.Thread(new System.Threading.ThreadStart(() => {
                TestaLogin();
            })).Start();
        }

        private void TestaLogin()
        {
            try
            {
                Context mContext = Android.App.Application.Context;
                ISharedPreferences pref = PreferenceManager.GetDefaultSharedPreferences(mContext);
                string uid = pref.GetString("uid", "0");
                string token = pref.GetString("token", "0");

                if (uid == "0" || token == "0")
                {
                    Device.BeginInvokeOnMainThread(() => { MainPage = new Login(); });
                }
                else
                {
                    if (Logar(uid, token))
                        Device.BeginInvokeOnMainThread(() => { MainPage = new MainPage(uid); });
                    else
                        Device.BeginInvokeOnMainThread(() => { MainPage = new Login(); });
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        private bool Logar(string user, string pass)
        {
            try
            {
                using (WebClient client = new WebClient())
                {
                    return client.DownloadString("https://www.example.com/mobile/login.php?u=" + user + "&t=" + pass) == "0" ? false : true;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}            

I also wanted to know if lines like this has to be inside the MainThread, I thought that because it's handling changing the page:

Device.BeginInvokeOnMainThread(() => { MainPage = new MainPage(uid); });
Éder Rocha
  • 1,538
  • 11
  • 29
  • 1
    Have you tried `Task.Run`? It's a more modern way of multi threading. See: https://stackoverflow.com/questions/13429129/task-vs-thread-differences – Stefan Sep 27 '18 at 19:41
  • if it crashes, you need to determine the cause of the crash. Step through it in the debugger, use logging, add try/catch, etc - to find the root exception causing the crash – Jason Sep 27 '18 at 19:43
  • Thank you for your fast reply. I will take a look, had not seen yet. – Éder Rocha Sep 27 '18 at 19:43
  • 2
    And, perhaps you can put a break point at the `re-throw` of the exception. ... btw, you should use `throw;` see: https://stackoverflow.com/questions/10805987/catch-vs-catch-exception-e-and-throw-vs-throw-e – Stefan Sep 27 '18 at 19:45
  • Thank you guys. I'm trying with Tasks. – Éder Rocha Sep 27 '18 at 19:52

0 Answers0