what's 
Dim conn as new connection??as you may know, BeginInvoke is run on a pooled thread, what are you doing after WriteLog("test")I cannot reproduce your error with the following code, maybe you should look into more about what you are doing 
Imports System
Imports System.IO
Imports System.Data.SqlClient
Imports Microsoft.VisualBasicDelegate Function XXSyncDelegate(ByVal ID As Long) As Boolean class TestMain  shared sub main()
    Dim objTestDelegate As New TestDelegate
    Dim obj As New XXSyncDelegate(AddressOf objTestDelegate.AddDataToDataBase)
    Dim Result As IAsyncResult = obj.BeginInvoke(1234, Nothing, Nothing)
    Console.ReadLine()
    'try
' obj.EndInvoke(Result)
    'Catch Ex As Exception
    'Console.WriteLine(Ex.Message)
   'End try
    
  end sub
end class
class  TestDelegatePublic Function AddDataToDataBase(ByVal ID As Long) As Boolean
 Try
   WriteLog("test")
   Dim Conn As new SqlConnection("server=localhost;database=pubs;uid=sa;pwd=;")
   Dim cmd as new SqlCommand("select count(*) from authors", conn)
   conn.Open()
   Console.Write(cmd.ExecuteScalar())
   conn.Close()
  
 Catch Ex As Exception
   WriteLog(Ex.Message)
 Finally
 End try
    'Throw New Exception("hello world")
End FunctionPublic Shared Function WriteLog(ByVal Message As String)
   Dim FilePath As String
   Dim msgWriter As TextWriter
   FilePath = "C:\"
   FilePath = FilePath & "Message.txt"
   msgWriter = File.AppendText(FilePath)
   TextWriter.Synchronized(msgWriter)
   msgWriter.WriteLine(Message & "----" & Now())
   msgWriter.Close()
   msgWriter = Nothing
End Function  
end class

解决方案 »

  1.   

    To:saucer(思归)
      I'm sorry.
      Dim conn as new SqlConnection.
      Attention,my application is web system.
      Thanks! 
      

  2.   

    same, I cannot reproduce your error, but you might know that the page is executed very quickly and that the code in AddDataToDataBase may not have executed before you see your output, you probably want to show your code
    <%@ Import Namespace="System.IO"%>
    <%@ Import Namespace="System.Data"%>
    <%@ Import Namespace="System.Data.SqlClient"%><script language="VB" runat=server>Delegate Function XXSyncDelegate(ByVal ID As Long) As Boolean Sub Page_Load()
        Dim objTestDelegate As New TestDelegate
        Dim obj As New XXSyncDelegate(AddressOf objTestDelegate.AddDataToDataBase)
        Dim Result As IAsyncResult = obj.BeginInvoke(1234, Nothing, Nothing)
        
        'try
    ' obj.EndInvoke(Result)
        'Catch Ex As Exception
        'Console.WriteLine(Ex.Message)
       'End try
        
    end subclass  TestDelegatePublic Function AddDataToDataBase(ByVal ID As Long) As Boolean
     Try
       WriteLog("test")
       Dim Conn As new SqlConnection("server=localhost;database=pubs;uid=sa;pwd=;")
       Dim cmd as new SqlCommand("select count(*) from authors", conn)
       conn.Open()
        WriteLog(cmd.ExecuteScalar().ToString())
       conn.Close()
      
     Catch Ex As Exception
       WriteLog(Ex.Message)
     Finally
     End try
        'Throw New Exception("hello world")
    End FunctionPublic Shared Function WriteLog(ByVal Message As String)
       Dim FilePath As String
       Dim msgWriter As TextWriter
       FilePath = "C:\"
       FilePath = FilePath & "Message.txt"
       msgWriter = File.AppendText(FilePath)
       TextWriter.Synchronized(msgWriter)
       msgWriter.WriteLine(Message & "----" & Now())
       msgWriter.Close()
       msgWriter = Nothing
    End Function  
    end class</script>
      

  3.   

    不可能呀,下面是我的所有语句。
    WEBFORM.VB
    Imports System.Data.SqlClient
    Imports System.Data.odbc
    Imports TestTextWriter
    Public Class WebForm1
        Inherits System.Web.UI.Page#Region " Web Form Designer Generated Code "    'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()    End Sub
        Protected WithEvents Button1 As System.Web.UI.WebControls.Button    'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub#End Region
        Delegate Function SendReportSyncDelegate(ByVal OE_ORDER_NO As Long) As Boolean    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load    End Sub    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim objTestDelegate As New TestDelegate        Dim obj As New SendReportSyncDelegate(AddressOf objTestDelegate.AddDataToInvoiceSearchOptionHeader)        Dim OE_ORDER_NO As Long
            OE_ORDER_NO = 1246076
            Dim ThreadID As Integer
            ThreadID = AppDomain.GetCurrentThreadId()
            Dim Result As IAsyncResult = obj.BeginInvoke(OE_ORDER_NO, Nothing, Nothing)
        End Sub
    End Class#######
    Imports System.Data.SqlClient
    Imports System.Data.odbc
    Imports TestTextWriter
    Public Class TestDelegate
        Private _connStr As String = "user id=cns;password=cns;initial catalog=CNSPROJECT;data source=SQL2000;Connect Timeout=30;"
        Public Function AddDataToInvoiceSearchOptionHeader(ByVal INVOICE_NO As Long) As Boolean
            Dim obj As New Utility
            obj.WriteLog("Avful--Header Connect Avful DataBase")
            Dim sqlConn As SqlConnection
            Dim sqlCmd As SqlCommand
            Dim sqlDataTable As DataTable
            Dim OE_ORDER_NO As Long
            Try
                sqlConn = New SqlConnection(_connStr)
                sqlCmd = New SqlCommand("sp_AddInvoiceSearchOptionHeader", sqlConn)
                sqlCmd.CommandType = CommandType.StoredProcedure
                sqlDataTable = GetDataToInvoiceSearchOptionHeader(INVOICE_NO)
                If sqlDataTable.Rows.Count <= 0 Then
                    Return False
                    Exit Function
                End If 
                ...
                PSSDataAdapter.SelectCommand = PSScmd
                PSSDataAdapter.Fill(PSSDataSet, "InvoiceSearchOptionHeader")
                Return PSSDataSet.Tables("InvoiceSearchOptionHeader")
            Catch Ex As Exception
                obj.WriteLog("DB2--Header " & Ex.Message)
                obj.WriteLog("DB2--Header INVOICE_NO =" & INVOICE_NO)
                Return Nothing
            Finally
                If PSSConn.State = ConnectionState.Open Then
                    PSSConn.Close()
                End If
            End Try
        End Function    Private Function GetDataToInvoiceSearchOptionHeader(ByVal INVOICE_NO As Long) As DataTable
            Dim obj As New Utility
            obj.WriteLog("DB2--Header Connect DB2 DataBase")
            Dim PSSConn As SqlConnection
            Dim PSScmd As SqlCommand
            Try
                PSSConn = New SqlConnection
                Dim strPSSCon As String
                PSScmd = New SqlCommand
                Dim PSSDataAdapter As New SqlDataAdapter
                Dim PSSDataSet As New DataSet
                strPSSCon = "DSN=aaa;"
                PSSConn.ConnectionString = _connStr
                PSScmd.Connection = PSSConn
                PSScmd.CommandText = "..."
                PSSDataAdapter.SelectCommand = PSScmd
                PSSDataAdapter.Fill(PSSDataSet, "InvoiceSearchOptionHeader")
                Return PSSDataSet.Tables("InvoiceSearchOptionHeader")
            Catch Ex As Exception
                obj.WriteLog("DB2--Header " & Ex.Message)
                obj.WriteLog("DB2--Header INVOICE_NO =" & INVOICE_NO)
                Return Nothing
            Finally
                If PSSConn.State = ConnectionState.Open Then
                    PSSConn.Close()
                End If
            End Try
        End Function
        Private Function ConvertStringToLong(ByVal strSource As String) As Long
            Try
                ConvertStringToLong = CLng(strSource.ToString().Trim())
            Catch
                ConvertStringToLong = Nothing
            End Try
            Return ConvertStringToLong
        End Function
    End Class
    #####
    Imports System.IO
    Public Class Utility
        Public Function WriteLog(ByVal Message As String)
            Dim threadId As Integer
            threadId = AppDomain.GetCurrentThreadId()        Dim FilePath As String
            Dim msgWriter As TextWriter
            FilePath = System.Web.HttpRuntime.BinDirectory
            FilePath = FilePath & "Message.txt"
            'FilePath = FilePath & "Message" & Now.Second & ".txt"
            msgWriter = File.AppendText(FilePath)
            TextWriter.Synchronized(msgWriter)
            msgWriter.WriteLine(Message & "----" & Now())
            msgWriter.Close()
            msgWriter = Nothing
        End Function
        Public Shared Function FormatStringTodate(ByVal InputString As String) As Date
            Try
                If InputString.Length = 8 Then
                    InputString = InputString.Substring(0, 4) & "\" & InputString.Substring(4, 2) & "\" & InputString.Substring(6, 2)
                    FormatStringTodate = CDate(InputString)
                    Return FormatStringTodate
                Else
                    Return Nothing
                End If
            Catch
                Return Nothing
            End Try
        End Function
    End Class
      

  4.   

    Error Message:
        The Thread Was Being Aborted
      

  5.   

    I think my error is this statement "FilePath = System.Web.HttpRuntime.BinDirectory".
      

  6.   

    see, if you don't simplify your code to allow us to test if (since we don't have your database), how could we help you?>>>I think my error is this statement "FilePath = System.Web.HttpRuntime.BinDirectory".nice, you are changing something in the bin subdirectory, it will restart the web application
      

  7.   

    There is a  Thread conflict in your  Progamme
      

  8.   

    To:saucer(思归)
       我想我的判断是对的,因为我测试了一下。如果向bin目录下写东西的话,就会出错。你的解释如果向bin目录写东西那么App会重新启动的解释不知道Microsoft有没有这样的知识库。麻烦告诉一下地址。
       非常感谢你的帮助。
      

  9.   

    see
    Things that causes application restart
    http://manand.typepad.com/dotnet/2003/10/why_aspnet_rest.html
      

  10.   

    To:saucer(思归)
       你的那篇文章我看到了,但我不知道MS是怎样解释的或我想看看MS的文章。你有链接吗?
       谢谢!
      

  11.   

    as you might know, that is how ASPNET works, allowing you to dynamically change code without shutting down IISthe ASPNET processes monitor those files (global.asax/web.config/machine.config) and locations like bin subdirectory. If it finds any changes, it will shutdown the relevant web application  and restart it