检测到上下文切换死锁

时间:2017-09-21 12:56:58

标签: excel vb.net contextswitchdeadlock

我试图像使用数据库一样使用Excel文件。我已经编写了这段代码来打开我用来工作的Excel文档

Private Sub Importa_XLS(ByVal fileData As String, ByVal dbVuoto As String, ByVal dbDest As String)

    Dim bw As BackgroundWorker = New BackgroundWorker
    bw.WorkerSupportsCancellation = True
    bw.WorkerReportsProgress = True
    If My.Computer.FileSystem.FileExists(dbDest) Then My.Computer.FileSystem.DeleteFile(dbDest)
    My.Computer.FileSystem.CopyFile(dbVuoto, dbDest)

    Dim capitoli As New cCapitoli
    Dim paragrafi As New cParagrafi
    Dim voci As New cVoci

    Dim fileStream As FileStream = New FileStream(fileData, FileMode.Open)
    Dim file(fileStream.Length) As Byte
    Dim percorso As String
    percorso = "C:\Users\User\Desktop\prova.xlsx"
    System.Threading.Thread.Sleep(3000)
    fileStream.Read(file, 0, fileStream.Length)

    fileStream.Close()

    Dim ExcelEngine As ExcelEngine = New ExcelEngine()
    Dim application As IApplication = ExcelEngine.Excel
    Dim workbook As IWorkbook = application.Workbooks.Open(New MemoryStream(file), ExcelOpenType.Automatic)
    bw.CancelAsync()
    Dim gecc As New Syncfusion.GridExcelConverter.GridExcelConverterControl

    For Each sheet As IWorksheet In workbook.Worksheets

        Dim grid As New GridModel
        bw.RunWorkerAsync(percorso)
        System.Threading.Thread.Sleep(3000)
        gecc.ExcelToGrid(sheet, grid)
        bw.CancelAsync()

当编译器执行此指令 gecc.ExcelToGrid(sheet,grid)时,我等一分钟,然后Visual Studio向我显示此错误:

  

CLR无法从COM上下文0xcad028转换到COM上下文0xcad0e0 60秒。拥有目标上下文/公寓的线程很可能是在非抽空等待或处理非常长时间运行的操作而不抽取Windows消息。这种情况通常会对性能产生负面影响,甚至可能导致应用程序变得无响应或内存使用量随时间不断累积。为了避免这个问题,所有单线程单元(STA)线程都应该使用抽取等待原语(例如CoWaitForMultipleHandles)并在长时间运行操作期间定期泵送消息。

我在线阅读解决方案,所以我尝试使用Backgroundworker,但错误信息仍在这里。也许我不太了解如何使用Backgroundworker。 谁能解释一下我在哪里做错了?

0 个答案:

没有答案