c# – 应用程序崩溃无一例外或日志(似乎是xamarin/mono bug)

前端之家收集整理的这篇文章主要介绍了c# – 应用程序崩溃无一例外或日志(似乎是xamarin/mono bug)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是一个奇怪的情况:),我已经在我的片段中实现了这个代码来扫描多线程环境中的TCP端口,它在调试时崩溃,有时在发布模式下也会因为消息而崩溃:

E / art(12972):检测到嵌套信号 – 报告原始信号

F / art(12972):art / runtime / fault_handler.cc:117]检查失败:!initialized_

tried to set Target Android to 23 that didn’t work.

tried running adb shell setprop debug.mono.env
MONO_DEBUG=soft-breakpoints
that didn’t work.

tried to compile using different Android API's Versions

任何想法为什么会发生这种情况?

HERE IS MY CODE:

public void start()
    {
        for (int i = 0; i < 50; i++)
        {
            Task.Run(() => RunScanTcp());
            //ThreadPool.QueueUserWorkItem(RunScanTcp);
            //Thread thread = new Thread(new ThreadStart(RunScanTcp));
            //thread.Start();
        }
    }


    public void RunScanTcp()
    {
        while (abort != true)
        {
                port = port + 1;
                Log.Info("PORT SCANNER",port.ToString());
        }
    }

    public class PortList
    {
        private int start;
        private int stop;
        private int ports;
        private static readonly object _syncRoot = new object();

        public PortList(int starts,int stops)
        {
            start = starts;
            stop = stops;
            ports = start;
        }

        public bool MorePorts()
        {
            lock (_syncRoot)
            {
                return (stop - ports) >= 0;
            }
        }

        public int NextPort()
        {
            lock (_syncRoot)
            {
                if (MorePorts())
                {
                    return ports++;
                }
                return -1;
            }
        }
    }

我编译使用:

Android版(Android 7.1牛轧糖)

最低Android版本:

Android 4.1(API级别16 – Jelly Bean)

目标Android版本:

使用SDK版本编译

更新:

在Visual Studio Update 15.2(26430.12)和Xamarin 4.5.0.476 – 30/05/2017(dd / mm / yyyy)之后,应用程序崩溃,同时连接到调试器…

Here is the debugger output :

referenceTable GDEF length=814 1
referenceTable GSUB length=11364 1
referenceTable GPOS length=47302 1
referenceTable head length=54 1
referenceTable GDEF length=428 1
referenceTable GSUB length=2302 1
referenceTable GPOS length=43252 1
referenceTable head length=54 1
referenceTable GDEF length=808 1
referenceTable GSUB length=11364 1
referenceTable GPOS length=49128 1
referenceTable head length=54 1
referenceTable GDEF length=808 1
referenceTable GSUB length=11364 1
referenceTable GPOS length=47320 1
referenceTable head length=54 1
05-31 04:31:51.590 F/art     (17427): art/runtime/fault_handler.cc:117] Check Failed: !initialized_

谢谢大家 …

解决方法

正如您所描述的那样,它的工作没有代码的那部分……所以错误必须在您的代码中的某处.

我们来看看吧.

public void start()
{
    for (int i = 0; i < 50; i++)
    {
        Task.Run(() => RunScanTcp());
    }
}


public void RunScanTcp()
{
    while (abort != true)
    {
            port = port + 1;
            Log.Info("PORT SCANNER",port.ToString());
    }
}

在start方法中,您启动了50个任务 – 而不是等待它们中的任何一个 – 这意味着可能所有任务并行运行(至少肯定某些任务会尝试同时访问端口).

这些任务执行的是访问端口的方法,该端口必须是包含类的字段.

如果没有锁定,从多个任务/线程访问共享字段绝不是一个好主意.

请尝试使用以下代码(如果由于某种原因确实需要并行任务).

public class YourClass
{

    private bool abort;
    private int port;
    private object portLock = new object();

    public void start()
    {
        for (int i = 0; i < 50; i++)
        {
            Task.Run(() => RunScanTcp());
        }
    }


    public void RunScanTcp()
    {
        while (abort != true && this.port < int.MaxValue)
        {
            lock (this.portLock) {
                port = port + 1;
                Log.Info("PORT SCANNER",port.ToString());
            }
        }
    }

}

可能更好的方法是仅使用一个任务并在某处等待它.

public class YourClass
{

    private bool abort;
    private int port;
    private object portLock = new object();

    public Task start()
    {
        return Task.Run(() => RunScanTcp());
    }


    public void RunScanTcp()
    {
        lock (this.portLock) {
            while (abort != true && this.port < int.MaxValue)
            {
                port = port + 1;
                Log.Info("PORT SCANNER",port.ToString());
            }
        }
    }

}

如果这解决了您当前的问题,您可能想要添加另一个问题,如何正确解决您实际尝试解决的问题(因为您的当前代码基本上只是递增端口,直到它被中止或应用程序崩溃(因为它计数在上面Int32 MaxValue)

原文链接:https://www.f2er.com/csharp/99275.html

猜你在找的C#相关文章