如何使用 VB Application.DoEvent?

2024-01-10

我有一个进程在针对小文件运行时运行良好,但给出了“Message=Managed Debugging Assistant 'ContextSwitchDeadlock':'CLR 无法从 COM 上下文 0xa5b8e0 转换到 COM 上下文 0xa5b828 60 秒”。针对大文件运行时出错。我对 VB 很陌生,做了一些调查,发现似乎建议使用 Application.DoEvent 。我希望有人可以向我展示如何使用它的示例。如果我正在执行一个名为“Process1”的子程序,我将如何使用 DoEvent 来防止它超时。理想情况下,我也想添加一个进度条,但我也不知道。我将不胜感激任何帮助。请保持简单,因为我是 VB/VS 的新手。

这是我显示代码的第一个问题的评论。 Process1 调用一个名为 ArchDtlCopyFile1 的子程序,该子程序滚动浏览列表视图中的值,将项目中指定的文件复制到不同的位置。然后,它调用 ArchDtlCheckCopy1 再次滚动列表视图以确保复制已完成。然后,它决定是否应删除源文件并根据需要执行删除操作。最后,它在 Access 表中插入一行记录所做的更改。

Private Sub Process1()
    If ReturnCode = 0 Then
        ArchDtlCopyFile1()
    Else
        ' MessageBox.Show("Error code coming in is: " & CStr(ReturnCode))
    End If

    If ReturnCode = 0 Then
        ArchDtlCheckCopy1()
    Else
        '  MessageBox.Show("Error code for check copy is: " & CStr(ReturnCode))
    End If
End Sub

Private Sub ArchDtlCopyFile1()

    intLVIndex = 0

    ' Copy the file from the source computer onto the NAS
    Do While intLVIndex < intMaxFileIndex
        Try

            ' Select the row from the LVFiles ListView, then move the first column (0) into strSourceFilePath and the last
            ' column (3) into strDestFilePath. Execute the CopyFile method to copy the file.
            LVFiles.Items(intLVIndex).Selected = True
            strSourceFilePath = LVFiles.SelectedItems(intLVIndex).SubItems(0).Text
            strDestFilePath = LVFiles.SelectedItems(intLVIndex).SubItems(3).Text
            My.Computer.FileSystem.CopyFile(strSourceFilePath, strDestFilePath, overwrite:=False)

        Catch ex As Exception

            ' Even if there's an error with one file, we should continue trying to process the rest of the files
            Continue Do

        End Try

        intLVIndex += 1

    Loop

End Sub

Private Sub ArchDtlCheckCopy1()

    intLVIndex = 0
    intLVError = 0

    '    ' Check each file was copied onto the NAS
    Do While intLVIndex < intMaxFileIndex


        ' Select the row from the LVFiles ListView, then move the last column (3) into strDestFilePath.
        ' Use the FileExists method to ensure the file was created on the NAS. If it was, call the
        ' ADetDelete Sub to delete the source file from the user's computer.

        LVFiles.Items(intLVIndex).Selected = True
        strSourceFilePath = LVFiles.SelectedItems(intLVIndex).SubItems(0).Text
        strDestFilePath = LVFiles.SelectedItems(intLVIndex).SubItems(3).Text
        strSourceFile = LVFiles.SelectedItems(intLVIndex).SubItems(1).Text
        Try

            If My.Computer.FileSystem.FileExists(strDestFilePath) Then
                ' Archive file was created so go ahead and delete the source file
                'If strSourceFile = myCheckFile Then
                '    strDestFile = LVFiles.SelectedItems(intLVIndex).SubItems(3).Text
                'End If
                If RBArchive.Checked = True Then
                    ArchDtlDeleteFile(strSourceFilePath)
                End If
                PrepareDtlVariables()
                ADtlAddRow()

            Else
                MessageBox.Show("File not found. " & strDestFilePath)
            End If

        Catch ex As Exception

            ErrorCode = "ARC6"
            MessageCode = "Error while checking file copy"
            ReturnCode = 8

        End Try

        intLVIndex += 1
    Loop

End Sub

这是一个简化的示例:

Imports System.ComponentModel
Imports System.IO
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        BackgroundWorker1.WorkerReportsProgress = True
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Button1.Enabled = False
        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        For i As Integer = 1 To 20
            BackgroundWorker1.ReportProgress(i) ' you can pass anything out using the other overloaded ReportProgress()
            System.Threading.Thread.Sleep(1000)
        Next
    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        ' you can also use e.UserState to pass out ANYTHING
        Label1.Text = e.ProgressPercentage
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        MessageBox.Show("Done!")
        Button1.Enabled = True
    End Sub

End Class
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 VB Application.DoEvent? 的相关文章

  • 从空白启动时 VSTO 功能区不显示解决方案

    如果我从 文件 新建项目 菜单创建一个新的 Excel 2013 和 2016 VSTO 加载项 项目 然后单击 项目 添加新项目 gt 功能区 可视化设计器 则一切正常 我启动了应用程序 我的功能区显示在 Excel 中 但是 如果我首先
  • 父窗体中的居中消息框[重复]

    这个问题在这里已经有答案了 有没有一种简单的方法可以在 net 2 0中将MessageBox居中于父窗体中 我在 C 中确实需要这个并发现中心消息框 C http bytes com topic c sharp answers 26712
  • 由于继承抽象类而禁用设计器?

    我有一个项目的解决方案 那个项目中有 40 或 50 种形式 我制作了 4 个基本形式 所有其他形式都可以继承 所有 4 个基本表单都继承 System Windows Forms Form 几乎 90 的形式继承了前 2 个基本形式之一
  • 根据所选单选按钮启用文本框

    我有一个单选按钮列表 其中列出了不同的业务类别 最后一个选项是Other类别 当用户选择Other类别 我希望能够启用一个文本框 用户可以输入更多信息来解释Other选择 目前 我正在尝试 If rblCategory SelectedIn
  • 无法在 VB MVC 应用程序中创建 HtmlHelper 方法

    我无法弄清楚以下代码中缺少什么 我有一个应该添加 虚拟 辅助扩展的方法 Imports System Runtime CompilerServices Namespace HtmlHelpers Public Module HelpExte
  • 如何在 VB.NET 中声明和初始化多维数组?

    我想做这个 Dim Numbers As Integer 1 2 3 4 5 6 7 IDE 的下划线4 5 6 7并说Array initializer has 3 too many elements 我究竟做错了什么 以下应该有效 Di
  • Visual Studio 改变 Ctrl-K-D 的工作方式

    In Visual Studio I m using 2012 is there any way of editing the way that Ctrl K D combinations handles its Auto Formatti
  • 从字体到跨度(大小和颜色)和背面的正则表达式(VB.NET)

    我正在寻找一个正则表达式 可以将我的字体标签 仅具有大小和颜色属性 转换为具有相关内联CSS的span标签 如果有帮助的话 这将在 VB NET 中完成 我还需要一个正则表达式来实现相反的效果 下面详细说明的是我正在寻找的转换示例 font
  • Outlook 加载项,无法读取未定义的属性“BeginRequestEventArgs”

    我使用 Visual Studio 开发了 Outlook 插件 我的插件有一个按钮 用于填充会议邀请正文中的详细信息并添加所需的与会者 这在 99 的情况下都有效 但是 时不时地它会给我下面的 JavaScript 错误 Uncaught
  • SQL存储过程执行时间差异

    我在 win form 应用程序中遇到奇怪的问题 我正在调用一个存储过程 并且执行大约需要 6 秒 此存储过程接受多个参数 包括一个输出参数 从应用程序级别我使用 Dim dt1 DateTime Now cmd ExecuteNonQue
  • 如何检查主音量是否静音

    如何在 Windows 7 操作系统中检查主音量是否静音我有静音或取消静音的代码 IE Public Const APPCOMMAND VOLUME MUTE As Integer H80000 Public Const APPCOMMAN
  • 将日期时间转换为指定格式

    我有这个日期格式yy MM dd HH mm ss ex 12 02 21 10 56 09 问题是 当我尝试使用以下代码将其转换为不同格式时 CDate 12 02 21 10 56 09 ToString MMM dd yyyy HH
  • 如何使用网络浏览器控件填写 html 表单

    在VB6 classic中 我们可以这样做 Private Sub Form Load WebBrowser1 Navigate2 http yourSite com End Sub Private Sub Command1 Click W
  • ASP.NET DropDownList OnSelectedIndexChanged 事件未触发

    我试图同时使用一些 AJAX 和 ASP Net 来运行函数而无需刷新整个页面 但我在执行此操作时偶然发现了一个问题 这是我的代码
  • VB.Net 中的文件比较

    我需要知道两个文件是否相同 起初我比较了文件大小和创建时间戳 但这不够可靠 我想出了下面的代码 似乎可行 但我希望有人有更好 更简单或更快的方法 基本上我正在做的是将文件内容流式传输到字节数组 并通过 System Security Cry
  • 在VB.net中动态添加用户控件

    我在 Vb net Windows 应用程序 中制作了自定义 UserControl 如何将其动态添加到表单中 UserControl 本质上只是另一个类 它继承自 Control 因此您可以使用控件执行各种操作 但除此之外它只是一个类 因
  • windows XP中如何设置默认编码?

    我尝试使用 StreamReader 打开文件并设置编码 但我希望它采用默认 Windows 编码 我如何更改我的 Windows 编码 区域和语言选项控制面板项目 高级选项卡 影响整个计算机
  • 以编程方式设置 IIS 6.0 的服务器绑定

    我正在尝试设置安装程序来注册网站 目前 我已经在 Windows Server 2003 下创建了应用程序池和网站 不幸的是 每当我尝试修改 ServerBindings 属性来设置 IP 地址时 它都会向我抛出异常 我第一次尝试这个是因为
  • 打印“X”个字符数与“X”字符串长度的所有可能组合(暴力破解)

    我正在尝试编写一个单词组合生成器 我的意思是打印 X 个字符数与 X 字符串长度的所有可能组合 首先 我需要说的是 我在 StackOverFlow 中看到了一个关于这个问题的问题 其中有很多单词生成器的答案来执行此操作 在不同的语言上 但
  • 如何隐藏数据列表中的项目

    我想隐藏一个项目datalist根据某种条件起诉ItemBound how Wrap a PlaceHolder控制整个内容ItemTemplate 然后在 ItemDataBound 事件中 您可以执行以下操作 Protected Sub

随机推荐