VB.NET – The task is running, can’t complete

There are some weird behaviors in my task that are not completed. I have been using this, but I think it is because what I pass to it is iteration and form-change selection and refresh some list boxes Maybe because of its stack, but I’m not sure. Let’s take a look at the code:

This is the child I want to run in the task:

< pre>Public Sub UnselectExistingConnectionsItems()
Dim SentenceId, SubSubKategorieId, SubSectionId As Integer
SubSectionId = CbSubSections.SelectedValue’combobox
If WithSubSubkategorie = SubSubKategorieEnum.Without ThenK
= 0SubSubKaterieEnum.Without Then
/> Else
SubSubKategorieId = CbSubSubKategorie.SelectedValue’combobox
End If
Unselect:
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.HtmlDescription.HtmlSubSections_SentenceSubId SubSections_SentenceSubId, SubKifConnectionAlreadyId,
/> sKey.IsSelected = False
LB_Sentences.Refresh()
GoTo Unselect
End If
Next
End Sub

I put it like this Task:

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 form
pic.ShowDialog()
Task.WaitAll(work)

and FrmCircularProgress are just forms (I use it almost anywhere, I have to wait for the user and its work except for this special case):

Public Class FrmCircularProgress
Sub New(progressType As DevComponents.DotNetBar.eCircularProgressType)
InitializeComponent()
CircularProgress1.ProgressBarType = progressType
StartCircular()< br /> End Sub

Public Sub StartCircular()
Me.CircularProgress1.IsRunning = True
End Sub

Public Sub StopCircular()
Me.CircularProgress1.IsRunning = False
End Sub
End Class

What could be wrong? Is it because the program interacts with listbox and combobxes? If so how to solve this problem, I will read about calling listbox and comboboxes, but I don’t know how to solve this problem.

Edit:
I think in addition to these aspects:

< p>

sKey.IsSelected = False
LB_Sentences.Refresh()

I have to do those:

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

Because of me In a different thread. Somehow, I don’t know how to convert these lines:

SubSectionId = CbSubSections.SelectedValue
SubSubKategorieId = CbSubSubKategorie.SelectedValue

< p>You may also need to call the loop. Wait for your help.

There is a rule that says “can be modified The only thread of the controls in the window is the thread that created the window”. Any other thread that tries to modify something in the window will generate a cross-thread call exception.

So, in the first edit , You got it right, you must call these functions.

However, this does not solve the problem of your unfinished tasks.

I believe doing sKey.IsSelected = False will not cancel Selecting anything in the ListBox will therefore cause an infinite loop… Also, the Goto statement is a very bad programming practice and should not be used. There is always another solution to make your code easier to debug/maintain/read…< /p>

ListBoxItem is not a type that exists in .Net Framework. So either you created that class or something else (I don’t know what it is…)< /p>

What can you do to solve your problem:

>Get the index of all selected items in the list
>Browse your list and check if they should be selected:

>If you select them, do nothing.
>If you don’t, deselect them.

This makes your code look like this (and you delete The ugly Label and Goto you don’t want in the code)……

Public Sub UnselectExistingConnectionsItems()
Dim SentenceId, SubSubKategorieId, SubSectionId As Integer
SubSectionId = CbSubSections.SelectedValue'combobox
If WithSubSubkategorie = SubSubKategorieEnum.Without Then
SubSubKategorieId = 0
Else
SubSubKategorieId = CbSubSubKategorie.SelectedValue'combo Ifbox
Endgorie.SelectedValue'combo Ifbox
/>
'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))< br /> SentenceId = HtmlDescription.HtmlSentence.GetSentenceIdByName (sKey.Text)
If HtmlDescription.HtmlSubSubSections_Sentences.CheckIfConnectionAlreadyExist(SentenceId, SubSectionId, SubSubKategorieId) Then
'We must remove it from the selection
LB_Sentences.Invoke(Sub() LB_Sentences. 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

There are some weird behaviors in my task that are not completed. I have been using this, but I want it because I passed it to It’s iteration and form-changing the selection and refreshing some of the list boxes may therefore its stack, but I’m not sure. Let’s take a look at the code:

This is what I want to run in the task Sub:

Public Sub UnselectExistingConnectionsItems()
Dim SentenceId, SubSubKategorieId, SubSectionId As Integer
SubSectionId = CbSubSections.SelectedValue'combobox
If WithSubSubkatego = SubSubKategorieEnum.Without Then
SubSubKategorieId = 0
Else
SubSubKategorieId = CbSubSubKategorie.SelectedValue'co mbobox
End If
Unselect:
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
s Refresh()
GoTo Unselect
End If
Next
End Sub

I put it in a task like this:

< /p>

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 form
pic.ShowDialog()
Task .WaitAll(work)

and FrmCircularProgress are just forms (I use it almost anywhere, I have to wait for the user and its work except for this special case):

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 Sub
End Class

What could be wrong? Is it because the program interacts with listbox and combobxes? If so how to solve this problem, I will read about calling listbox and comboboxes, but I don’t know how to solve this problem.

Edit:
I think in addition to these aspects:

< p>

sKey.IsSelected = False
LB_Sentences.Refresh()

I have to do those:

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

Because of me In a different thread. Somehow, I don’t know how to convert these lines:

SubSectionId = CbSubSections.SelectedValue
SubSubKategorieId = CbSubSubKategorie.SelectedValue

< p>You may also need to call the loop. Wait for your help.

There is a rule that says “The only thread that can modify the controls in the window is the thread that creates the window.” Any other thread that tries to modify something in the window will generate a cross-thread call exception.

So, in the first edit, you got it right, you must call these functions.

However, this does not solve your problem of unfinished tasks.

I believe doing sKey.IsSelected = False will not deselect anything in the ListBox, so it will cause an infinite loop …In addition, Goto statements are very bad programming habits and should not be used. There is always another solution to make your code easier to debug/maintain/read…

ListBoxItem is not in the .Net Framework The type of existence. So either you created that class or something else (I don’t know what…)

What can you do to solve your problem:

>Get the index of all selected items in the list
>Browse your list and check if it should be selected Select them:

>If you select them, do nothing.
>If you don’t, please deselect them.

This makes your code look like this (and You deleted the ugly Label and Goto that you didn’t want in the code)……

Public Sub UnselectExistingConnectionsItems()
Dim SentenceId, SubSubKategorieId, SubSectionId As Integer
SubSectionId = CbSubSections.SelectedValue'combobox
If WithSubSubkategorie = SubSubKategorieEnum.Without Then
SubSubKategorieId = 0
Else
SubSubKategorieId = CbSubSubKategorie.SelectedValue /> 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_Sentence s.CheckIfConnectionAlreadyExist(SentenceId, SubSectionId, SubSubKategorieId) Then
'We must remove it from the selection
LB_Sentences.Invoke(Sub() LB_Sentences.SelectedItems.Remove(sKey))
End If< br /> Next
'We do the Refresh at the end so we gain some process time...
LB_Sentences.Invoke(Sub() LB_Sentences.Refresh())
End Sub

Leave a Comment

Your email address will not be published.