vb.net – 任务正在运行,无法完成

vb.net – 任务正在运行,无法完成,第1张

概述我的任务中有一些奇怪的行为没有完成.我一直使用这个,但我想它因为我传递给它的是迭代与形式 – 改变选择和刷新一些列表框可能因此它的堆栈,但我不确定.让我们看看代码: 这是我想在任务中运行的子: Public Sub UnselectExistingConnectionsItems() Dim SentenceId, SubSubKategorieId, SubSectionId A 我的任务中有一些奇怪的行为没有完成.我一直使用这个,但我想它因为我传递给它的是迭代与形式 – 改变选择和刷新一些列表框可能因此它的堆栈,但我不确定.让我们看看代码:

这是我想在任务中运行的子:

Public Sub UnselectExistingConnectionsItems()        Dim SentenceID,SubSubKategorIEID,SubSectionID As Integer        SubSectionID = CbSubSections.SelectedValue   'comboBox        If WithSubSubkategorIE = SubSubKategorIEEnum.Without Then            SubSubKategorIEID = 0        Else            SubSubKategorIEID = CbSubSubKategorIE.SelectedValue  'comboBox        End IfUnselect:        For i As Integer = 0 To LB_Sentences.SelectedItems.Count - 1            Dim sKey As ListBoxItem            sKey = LB_Sentences.SelectedItems(i)            SentenceID = HTMLDescription.HTMLSentence.GetSentenceIDByname(sKey.Text)            If HTMLDescription.HTMLSubSubSections_Sentences.CheckIfConnectionAlreadyExist(SentenceID,SubSectionID,SubSubKategorIEID) Then                sKey.IsSelected = False                LB_Sentences.Refresh()                GoTo Unselect            End If        Next    End Sub

我把它放到这样的任务:

Dim pic As New FrmCircularProgress(eCircularProgresstype.line)Dim work As Task = Task.Factory.StartNew(Sub()'--Run lenghty task                                                   UnselectExistingConnectionsItems()'--Close form once done (on GUI thread)pic.Invoke(New Action(Sub() pic.StopCircular()))pic.Invoke(New Action(Sub() pic.Close()))End Sub)'--Show the formpic.ShowDialog()Task.WaitAll(work)

和FrmCircularProgress只是形式(我几乎在任何地方使用它,我必须等待用户和它的工作除了这个特殊情况):

Public Class FrmCircularProgress    Sub New(progresstype As DevComponents.DotNetbar.eCircularProgresstype)        InitializeComponent()        CircularProgress1.ProgressbarType = progresstype        StartCircular()    End Sub    Public Sub StartCircular()        Me.CircularProgress1.IsRunning = True    End Sub    Public Sub StopCircular()        Me.CircularProgress1.IsRunning = False    End SubEnd Class

什么可能是错的?是因为程序与ListBox和combobxes交互?如果是这样如何解决这个问题,我会阅读有关调用ListBox和comboBoxes的内容,但不知道如何解决这个问题.

编辑:
我认为除了这些方面:

sKey.IsSelected = False                LB_Sentences.Refresh()

我必须做那些:

LB_Sentences.Invoke(Sub()  sKey.IsSelected = False              End Sub)LB_Sentences.Invoke(Sub()                                                                 LB_Sentences.Refresh()     End Sub)

因为我在不同的线程.不知怎的,我不知道如何转换这些行:

SubSectionID = CbSubSections.SelectedValue  SubSubKategorIEID = CbSubSubKategorIE.SelectedValue

可能还需要调用循环.等你的帮助.

解决方法 有一条规则说“可以修改窗口中控件的唯一线程是创建窗口的线程”.尝试修改窗口中某些内容的任何其他线程都将生成跨线程调用异常.

因此,在第一次编辑中,你做对了,你必须调用这些函数.

但是,这并不能解决您未完成任务的问题.

我相信做sKey.IsSelected = False不会取消选择ListBox中的任何内容,因此会导致无限循环…另外,Goto语句是非常糟糕的编程习惯,不应该使用.总有另一种解决方案可以使您的代码更容易调试/维护/读取…

ListBoxItem不是.Net Framework中存在的类型.所以要么你创造了那个类,要么是其他东西(我不知道是什么……)

你可以做些什么来解决你的问题:

>获取列表中所有选定项目的索引
>浏览您的列表,并检查是否应该选择它们:

>如果选择它们,则不执行任何 *** 作
>如果不这样做,请取消选择.

这使得你的代码就像这样(并且你删除了你在代码中不想要的那个丑陋的Label和Goto)……

Public Sub UnselectExistingConnectionsItems()    Dim SentenceID,SubSectionID As Integer    SubSectionID = CbSubSections.SelectedValue   'comboBox    If WithSubSubkategorIE = SubSubKategorIEEnum.Without Then        SubSubKategorIEID = 0    Else        SubSubKategorIEID = CbSubSubKategorIE.SelectedValue  'comboBox    End If    'We create an array to remind our initial selection    Dim sel = New Integer(LB_Sentences.SelectedItems.Count - 1) {}    LB_Sentences.Selectedindices.copyTo(sel,0)    For i = 0 To sel.Length - 1        Dim sKey As ListBoxItem        'We get our selected item        sKey = LB_Sentences(sel(i))        SentenceID = HTMLDescription.HTMLSentence.GetSentenceIDByname(sKey.Text)        If HTMLDescription.HTMLSubSubSections_Sentences.CheckIfConnectionAlreadyExist(SentenceID,SubSubKategorIEID) Then            'We must remove it from the selection            LB_Sentences.Invoke(Sub() LB_Sentences.SelectedItems.Remove(sKey))        End If    Next    'We do the Refresh at the end so we gain some process time...    LB_Sentences.Invoke(Sub() LB_Sentences.Refresh())End Sub
总结

以上是内存溢出为你收集整理的vb.net – 任务正在运行,无法完成全部内容,希望文章能够帮你解决vb.net – 任务正在运行,无法完成所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1212724.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存