简体   繁体   English

将C#代码转换为Android

[英]Convert C# code to Android

I'm new to android. 我是android新手。 I've implemented kind of a hole punching in C# and I'm trying to implement the same logic in android. 我已经在C#中实现了打孔,并且试图在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. 我在android中实现了新类的.NET解决方案中的一些类,例如TimeSpan,尽管我需要实现一些类,但我还是将它们堆叠在一起。

  1. Trying to get class in android that behave as CancellationTokenSource in .NET 试图在Android中获取充当.NET中CancellationTokenSource的
  2. There is some logic that works great in C# but I don't know how to implement it in android. 有一些逻辑在C#中效果很好,但是我不知道如何在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 谢谢

You could check out Xamarin . 您可以查看Xamarin You can create Android, iOS, Mac and Windows apps with C# and Xamarin Studio compiles it for you to native applications. 您可以使用C#创建Android,iOS,Mac和Windows应用程序,Xamarin Studio会将其编译为本地应用程序。

Use a language converter tool. 使用语言转换器工具。 You can find some nice ones listed HERE 你可以在这里找到一些不错的

It wont give you a 100% result every time, but will take you close to the final result where with a few minor changes here and there you'll get what you want. 它不会每次都为您提供100%的结果,但会带您接近最终结果,在这里进行一些小的更改,您将获得想要的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM