mvc4 vb.net中的异步函数

时间:2014-10-10 08:56:58

标签: vb.net asp.net-mvc-4 asynchronous asp.net-web-api

我有以下结构:

这是我的控制者:

   Public Function InsertRegistration() as string

          dim res = t.Insertregistration(jsonparam.status)

          return res
   End Function

这是我的班级:

   Public Function InsertRegistration(byval status as string) as string

         sql.Insertregistration(status)

         return "1"

   End Function

这是我的模特功能:

    Public Function InsertRegistration(ByVal status As String) As Boolean
    Dim TConnSQL As New SqlConnection(sql)
    Dim CommSQL As New SqlClient.SqlCommand("JK_SP_INSERT_PROFILE", TConnSQL)
    Dim paramSQL As SqlClient.SqlParameter
    Dim data_ada As SqlDataAdapter
    Dim dt As DataSet
    Try

        CommSQL.CommandType = CommandType.StoredProcedure

        paramSQL = New SqlClient.SqlParameter("@STATUS", SqlDbType.NVarChar, 100)
        paramSQL.Direction = ParameterDirection.Input
        paramSQL.Value = STATUS
        CommSQL.Parameters.Add(paramSQL)

        CommSQL.ExecuteNonQuery()

        InsertRegistration= True

    Catch ex As System.Data.SqlClient.SqlException
        WriteToText("Nbl.InsertRegistration", ex.ToString)
        InsertRegistration= False
    Catch ex As Exception
        WriteToText("Nbl.InsertRegistration", ex.ToString)
        InsertRegistration= False
    Finally
        If (IsNothing(CommSQL) = False) Then CommSQL.Dispose()
        If TConnSQL.State <> ConnectionState.Closed Then TConnSQL.Close()
    End Try
End Function

有一种方法可以在我将结果返回给客户端后以异步方式返回结果1,使函数sql.Insertregistration(status)在后​​台继续运行吗?

1 个答案:

答案 0 :(得分:0)

只需将您的函数sql.Insertregistration(status)设置为以下代码:

(new Task<bool>(() =>
{
    return sql.Insertregistration(status);
})).Start();

注意:请注意跨线程操作。