0

VB.NET 2.0:

我正在从 VB.NET 应用程序向 SQL 服务器发出备份命令。我正在捕获它发出的消息并将它们附加到多行文本框中。

不过,我想做的是让应用程序继续响应 GUI 控制事件(主要是为了让用户可以调整输出窗口的大小或取消备份)。所以我BeginExecuteReader在一个循环中使用和旋转Application.DoEvents(). 但似乎一旦备份开始发出它的打印语句,我得到IAsyncResult.IsCompleted = True它并下降到EndExecuteReader,并且 GUI 现在再次被锁定。我怎样才能让它留在循环中,直到备份命令完成,但仍然得到那些输出语句并保持 GUI 响应?谢谢。

这是我的代码:

' Enable retrieve print statements from the server'
AddHandler oConn.InfoMessage, AddressOf LogToBufferHandler

strSQL &= vbNewLine & "begin try"
strSQL &= vbNewLine & " declare @BackupName as varchar(255)"
strSQL &= vbNewLine & " declare @BackupDesc as varchar(255)"
strSQL &= vbNewLine & " declare @backupTime as varchar(50)"
strSQL &= vbNewLine & " set @backupTime = (select convert(datetime, getdate(), 100))"
strSQL &= vbNewLine & " set @BackupName = (SELECT '[' + db_name() + '] Full Backup')"
strSQL &= vbNewLine & " set @BackupDesc = (SELECT 'Automated full backup of [' + db_name() + '] on ' + @backupTime + '.')"
strSQL &= vbNewLine & " "
strSQL &= vbNewLine & " BACKUP DATABASE [#Database#]"
strSQL &= vbNewLine & " TO DISK = @BackupFullPath"
strSQL &= vbNewLine & " WITH stats,"
strSQL &= vbNewLine & "      NAME = @BackupName,"
strSQL &= vbNewLine & "      DESCRIPTION = @BackupDesc;"
strSQL &= vbNewLine & " select [IsSuccessful] = 1"
strSQL &= vbNewLine & " end try"
strSQL &= vbNewLine & " begin catch"
strSQL &= vbNewLine & " SELECT [IsSuccessful] = 0"
strSQL &= vbNewLine & " end catch"

'Workaround: Backup Database requires the name of the object, not a string'
'            and I dont want to use dynamic SQL.'
strSQL = strSQL.Replace("#Database#", sb.InitialCatalog)

oConn.Open()
oCmd = New SqlCommand()
oCmd.Connection = oConn
oCmd.CommandText = strSQL
oCmd.CommandType = CommandType.Text
oCmd.Parameters.AddWithValue("@BackupFullPath", backupFullPath)
oCmd.CommandTimeout = 60 * 5

'Spin until complete, cancel, or timeout'
Dim result As IAsyncResult = oCmd.BeginExecuteReader(CommandBehavior.CloseConnection)
While Not result.IsCompleted
    Application.DoEvents()
    If blnCancel Then
        oCmd.Cancel()
    End If
    System.Threading.Thread.Sleep(50)
End While
Try
    oDataReader = oCmd.EndExecuteReader(result)
    oDataTable.Load(oDataReader)

    'Get results'
    ' (unfourtunately, you cannot do BeginExecuteScalar ASync in .Net 2.0,'
    ' so we are using a DataTable first column, row)'
    If oDataTable IsNot Nothing _
    AndAlso oDataTable.Rows.Count > 0 _
    AndAlso oDataTable.Columns.Contains("IsSuccessful") _
    AndAlso oDataTable.Rows(0).Item("IsSuccessful") = 1 Then
        eBackupResult = BackupStatus.Succeeded
        returnPath = backupFullPath 'Only set return path if the backup succeeded'
    Else
        eBackupResult = BackupStatus.Failed
    End If
Catch ex As Exception
    If Not ex.Message.Contains("cancelled by user") Then Throw ex
    eBackupResult = BackupStatus.Canceled
End Try
4

3 回答 3

1

DoEvents出于多种原因,轮询循环 ( ) 被认为是邪恶的。这可能是您切换到BackgroundWorker并放弃讨厌的 Begin-poll-End 方案的最简单方法。

如果你想保留它,这里有一个错误:接受阅读器可能很快,但这并不意味着所有结果都已经到达(让我说明一下:如果你查询了 1TB 的数据怎么办? - 阅读是一个漫长的过程)。您也需要以Read异步(轮询)方式。现在事情已经失控了。就放弃吧。

换句话说,oDataTable.Load是难以修复的阻塞。

于 2012-12-07T18:08:07.800 回答
0

BackGroundWorker 和 ReportProgress

BackgroundWorker 类

于 2012-12-07T17:54:49.027 回答
0

您可以为此使用任务。任务在概念上类似于线程。通过将代码放入任务中,主线程可以继续运行(这是接受用户输入并响应调整大小命令的线程)。

这是一篇更多地讨论任务并有一个 VB.NET 示例的文章。

http://www.dotnetcurry.com/ShowArticle.aspx?ID=491

玩得开心!

于 2012-12-07T17:31:44.367 回答