线程在异步运行方法时中止

时间:2013-10-17 09:40:43

标签: c# multithreading asynchronous begininvoke

您好我正在尝试在.net c#中异步运行一个方法,为此我已声明该方法的委托并使用begin invoke来触发该方法。 但有时在执行此异步方法时线程会中止。请在下面找到示例代码......

// this is the delegate 

public delegate bool obj_del(string p1, string p2, int p3, 
      string strConnectionStringAlias, string p4, bool p5);
obj_del objGetData = null;

// this is how i call the method

objGetData = new obj_del(blnGetRealTimeSeatCount);
objGetSeatCount.BeginInvoke(p1, p2, p3, strConnectionStringAlias, p4, p5, null, null);

// this is the method
 private bool blnGetRealTimeSeatCount(string strAppCode, string strVenueCode, int lngTransactionId, string strConnectionStringAlias, string strBMSTransactionCode = "", bool blnDebug = false)
{
    const string udcErrorMethod = "blnGetRealTimeSeatCount(string strAppCode, string strVenueCode, int lngTransactionId, string strConnectionStringAlias, string strBMSTransactionCode , bool blnDebug = false)";


    clsCommon objCommon = new clsCommon();
    clsDBEngine objDBEngine = null;
    int intReturnVal = 0;

    string strReturnData = "";

    try
    {
        objDBEngine = new clsDBEngine(clsMisc.strGetConnectionString(strConnectionStringAlias));
        objDBEngine.blnParamClear();
        objDBEngine.blnParamAdd(ParameterDirection.Input, "@strAppCode", SqlDbType.VarChar, 10, strAppCode);
        objDBEngine.blnParamAdd(ParameterDirection.Input, "@strVenueCode", SqlDbType.VarChar, 10, strVenueCode);
        objDBEngine.blnParamAdd(ParameterDirection.Input, "@lngTransactionId", SqlDbType.Int, 4, lngTransactionId);
        objDBEngine.blnParamAdd(ParameterDirection.Output, "@strSqlErrorMsg", SqlDbType.VarChar, 255, null);
        //objDBEngine.blnParamAdd(ParameterDirection.Input, "@intSessionId", SqlDbType.Int, 10, lngSessionId);
        //objDBEngine.blnParamAdd(ParameterDirection.Input, "@strAreaCatCode", SqlDbType.VarChar, 10, strAreaCatCode);
        intReturnVal = objDBEngine.lngExecuteSP("spWSGetSessionRealTimeSeatCount", true);
        switch (intReturnVal)
        {
            case 0:

                ArrayList arrData = objDBEngine.arlResultSet;
                if (!blnFormatData(arrData, "XML", strAppCode, strBMSTransactionCode, strVenueCode, out strReturnData))
                {
                    return false;
                }
                //Post the Data to url 
                subPostData(System.Web.HttpUtility.UrlEncode(Encoding.ASCII.GetBytes(strReturnData)), strAppCode, strBMSTransactionCode, strVenueCode, blnDebug);
                break;

            default:
                //strReturnData = objDBEngine.objParamValue("@strException").ToString() != "" ? objDBEngine.objParamValue("@strException").ToString() : objDBEngine.strErrorMessage;
                //blnLogDebug(udcErrorSource, udcErrorMethod, "Failed to get Real time seat count from database. sql error code =" + intReturnVal.ToString() + " comm lib error=" + objDBEngine.strErrorMessage + " sql error desc=" + objDBEngine.objParamValue("@strSqlErrorMsg").ToString(), "", strAppCode, "", strVenueCode, "");
                objCommon.blnLogError(udcErrorSource, udcErrorMethod, "Executing Method blnGetRealTimeSeatCount", "Failed to get Real time seat count from database. sql error code =" + intReturnVal.ToString() + " comm lib error=" + objDBEngine.strErrorMessage + " sql error desc=" + objDBEngine.objParamValue("@strSqlErrorMsg").ToString(), false, strAppCode, strBMSTransactionCode, strVenueCode, "");
                return false;
        }
    }
    catch (Exception objEx)
    {
        //clsLog.blnLogError(udcErrorSource, udcErrorMethod, "Retreving real time seat count data", ex.Message);
        objCommon.blnLogError(udcErrorSource, udcErrorMethod, "Executing Method blnGetRealTimeSeatCount", " Error in getting real seat count of a session, DNF Error Occured : " + objEx.ToString(), false, strAppCode, strBMSTransactionCode, strVenueCode, "");

        return false;
    }
    finally
    {
        objDBEngine.blnCloseConnection();
    }
    return true;
}

0 个答案:

没有答案