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
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
解决方案 »
- Microsoft.Office.Interop.Excel 缺少Interop 的引用
- winform项目安装部署出现问题
- Treeview我选的就是树根啊 怎么还会报错呢
- 请教C#开发USB端口时用libUsbDotNet库遇到的endpointID问题
- 该进程无法访问文件C:\Documents and Settings\Bird\桌面\log.txt,因为该文件正由另一进程使用
- 怎么样在C/S结构中向WEB服务器的指定目录上传大文件,急,分不够再加!
- 为什么dataGrid的示例里会出现系统错误?
- 如何获取javaScript的内容
- 用visual c#建dll的问题
- 关于TextChanged 事件
- 在vs.net2003中using System.Windows时没有Windows类怎么办?
- 短信AT指令在程序中如何调用?
I'm sorry.
Dim conn as new SqlConnection.
Attention,my application is web system.
Thanks!
<%@ 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>
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
The Thread Was Being Aborted
我想我的判断是对的,因为我测试了一下。如果向bin目录下写东西的话,就会出错。你的解释如果向bin目录写东西那么App会重新启动的解释不知道Microsoft有没有这样的知识库。麻烦告诉一下地址。
非常感谢你的帮助。
Things that causes application restart
http://manand.typepad.com/dotnet/2003/10/why_aspnet_rest.html
你的那篇文章我看到了,但我不知道MS是怎样解释的或我想看看MS的文章。你有链接吗?
谢谢!