Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对dll的函数调用导致程序挂起_C#_Wpf_Dll - Fatal编程技术网

C# 对dll的函数调用导致程序挂起

C# 对dll的函数调用导致程序挂起,c#,wpf,dll,C#,Wpf,Dll,我有一个问题,我的代码在调用Dll中的登录函数时冻结,Dll用于连接到服务器上的应用程序,但这种情况并不经常发生。这只发生在我登录时,有一个连接,然后是一个没有连接的尝试,在超时之前重新连接(多次尝试登录),然后再次尝试登录,此时它永远位于对Dll的函数调用处。从服务器获取所需数据后,每次登录后都会进行注销。这是引入Dll函数的方式: [DllImport("SafeComDLLs\\scApi.dll",CharSet=CharSet.Auto, CallingConvention=C

我有一个问题,我的代码在调用Dll中的登录函数时冻结,Dll用于连接到服务器上的应用程序,但这种情况并不经常发生。这只发生在我登录时,有一个连接,然后是一个没有连接的尝试,在超时之前重新连接(多次尝试登录),然后再次尝试登录,此时它永远位于对Dll的函数调用处。从服务器获取所需数据后,每次登录后都会进行注销。这是引入Dll函数的方式:

    [DllImport("SafeComDLLs\\scApi.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.Cdecl)]
    private static extern int sc_LoginByLogon(string szlpAddr,string szuserLogon,string szPassword);
这是我用来连接到服务器的代码:

    public int LogIn(string ipAddress, string userName, string password)
    {
        int result = -1;

        try
        {
             result = sc_LoginByLogon(ipAddress, userName, password);
             if (result == 0)
                 _log.Info("Log in Success");
             else
                 _log.Error("Log in failed, error message : " + result.ToString());
        }
        catch (Exception e)
        {
            _log.Error("sc_LoginByLogon failed with exception : " + e.Message);
        }
        return result;
    }
正如您所看到的,我捕获异常,但没有抛出异常,代码仅位于这一部分:

         result = sc_LoginByLogon(ipAddress, userName, password);

你的问题是关于什么的?您想知道如何防止冻结您的UI?问题是如何在此时阻止登录冻结?代码在处理来自Dll的函数调用时是否有任何错误?UI是分离的,只等待它返回。抱歉,实际上应该在问题中提出一个问题。从这里我可以看出,对本机函数的重复调用可能会导致死锁。可能您想锁定访问权限,使其无法同时调用。谢谢您的帮助,但这是导致问题的代码的另一部分。我还实现了您关于创建访问锁的建议。