Convert C# code to Android

17,241

You could check out Xamarin. You can create Android, iOS, Mac and Windows apps with C# and Xamarin Studio compiles it for you to native applications.

Share:
17,241

Related videos on Youtube

ishigh
Author by

ishigh

Updated on June 04, 2022

Comments

  • ishigh
    ishigh almost 2 years

    I'm new to android. I've implemented kind of a hole punching in C# and I'm trying to implement the same logic in android.

    Some of the classes from .NET solution that I Implemented new classes in android such as TimeSpan, though there are some classes that I need to implement and I got stacked.

    1. Trying to get class in android that behave as CancellationTokenSource in .NET
    2. There is some logic that works great in C# but I don't know how to implement it in android.

    CODE:

        private static Boolean rec_and_wait(TimeSpan interval)
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            try
            {
                CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();                
                byte[] data = new byte[1024];
                IAsyncResult ar = socket.BeginReceive(data, 0, data.Length, SocketFlags.None, null, null);
                int res = WaitHandle.WaitAny(new WaitHandle[] { ar.AsyncWaitHandle, _cancellationTokenSource.Token.WaitHandle }, interval.Add(TimeSpan.FromSeconds(10))); // allow extra 10 seconds for network delay
                switch (res)
                {
                    case 0: // response
                        return true;
                    case WaitHandle.WaitTimeout: // time out
                    case 1: //cancelled
                    default: // should not happen
                        return false;
                }
            }
            catch
            {
                return false;
            }
            finally
            {
                if (socket != null)
                {
                    socket.Close();
                    socket.Dispose();
                    socket = null;
                }
            }
        }
    

    Thanks

    • Display Name
      Display Name about 10 years
      learning Java and rewriting is the best option.
    • ishigh
      ishigh about 10 years
      I agree, and I'm on it, but some of the classes are not similar at all and need lots of adjusts, some of them are easy such as TimeSpan and some are very hard... that is why I'm asking it :-)
  • TChadwick
    TChadwick over 7 years
    It's free now! You can use it and do all your programming for Android and iOS in C#. And it is very mature now!