Download Code

Xamarin Thread Example Android

In this tutorial you will learn about thread,Exception and how to develop Xamarin Thread Example Android applications.First i will give some basic information Link.

Thread

A concurrent unit of execution.It has its own stack for methods to be invoked.

Every application have at least one running when application started known as “Main and UI Threads“.

Becuase it is responsible for every event occuring on UI.

There are two basic rules for UI Threads.

  • Do not block.
  • Do not access the Android UI toolkit from outside.

Exception

Exception is an error event that can happen during the execution of program and disturb program’s normal flow/execution.

There are some keywords we used while handling exceptions.

Exception is basically used to handle irregular inputs.

  • Throw
  • Throws
  • Try-Catch
  • Finally

For more detail about exception handling you can go through this website.

Create new Xamarin Thread Example android application project.

Your MainActivity.cs class will be at the start.

using Android.App;
using Android.Widget;
using Android.OS;
using System.Threading;
using System;
using Android.Content;

namespace XamarinThread
{
	[Activity(Label = "XamarinThread", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{
		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);
			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);

		}
	}
}

Next create Thread variable.Then initialize with ThreadStart delegate and call Thread.Start method.

using Android.App;
using Android.Widget;
using Android.OS;
using System.Threading;
using System;
using Android.Content;

namespace XamarinThread
{
	[Activity(Label = "XamarinThread", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{
		Thread unique;

		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);

			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);
			unique = new Thread(new ThreadStart(delegate
			{
				
			}));
			unique.Start();
		}
	}
}

Create new class with layout which will start after execution.

secondActivity.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace XamarinThread
{
	[Activity(Label = "secondActivity")]
	public class secondActivity : Activity
	{
		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);
			// Create your application here
			SetContentView(Resource.Layout.second);
		}
	}
}

Now put Exception handling in MainActivity.cs class.Start activity on completion of thread execution.
MainActivity.cs

using Android.App;
using Android.Widget;
using Android.OS;
using System.Threading;
using System;
using Android.Content;

namespace XamarinThread
{
	[Activity(Label = "XamarinThread", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{
		Thread unique;

		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);

			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);
			unique = new Thread(new ThreadStart(delegate
			{
				try
				{
					Thread.Sleep(2000);
				}
				catch (Exception e)
				{
					e.ToString();
				}
				finally
				{
					StartActivity(new Intent(Application.Context, typeof(secondActivity)));
				}
			}));
			unique.Start();
		}
	}
}

Now your Xamarin Thread Example Android application project.

Xamarin Thread,Xamarin Thread Example,Xamarin Thread Example Android,thread example android

This was a simple with low computations.But when user wants intensive work then this thread will perform poorly.

That’s it for Xamarin Thread Example Android tutorial.

Thanks.