Buongiorno a tutti.
oggi un altro piccolo passo avanti, seguito da un altro passo indietro..:
ora riesco a processare la mia SUB in background ed apportate le modifiche al form nel mentre il programma principale è in attesa per la finestra modale di InpuBox.
sono arrivato anche passargli un parametro alla sub (non tramite il comando Thread.Start, ma tramite la chiamata successiva alla subThreading...ma questo non è un grosso problema...)
il problema è che quando ho terminato di immettere da tastiera la stringa nella inputbox e dò l'OK, assolutamente la finestra rimane aperta fino a quando il thread in background abbia terminato il suo processo
, (purtroppo non capisco il perchè e come sia possibile sto fenomeno) .. non dovrebbe andare avanti chiudendo la finestra e proseguendo nel codice??
vi posto tutto il codice d'esempio (si tratta dell'esempio di MSDN che ho adattato alla Mia SUB CARICA(S as string) emulando processi in background (in questo caso riempie ListBox), con l'aggiunta della chiamata parallela a Inputbox (stilata nel codice di Button_Click)
codice:
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Threading
Public Class MyFormControl
Inherits Form
Delegate Sub ModifierForm(ByVal s As String)
Public myDelegate As ModifierForm
Private myButton As Button
Private myThread As Thread
Private myListBox As ListBox
Public Sub New()
myButton = New Button()
myListBox = New ListBox()
myButton.Location = New Point(72, 160)
myButton.Size = New Size(152, 32)
myButton.TabIndex = 1
myButton.Text = "Add items in list box"
AddHandler myButton.Click, AddressOf Button_Click
myListBox.Location = New Point(48, 32)
myListBox.Name = "myListBox"
myListBox.Size = New Size(200, 95)
myListBox.TabIndex = 2
ClientSize = New Size(292, 273)
Controls.AddRange(New Control() {myListBox, myButton})
Text = " 'Control_Invoke' example"
myDelegate = New ModifierForm(AddressOf Carica)
End Sub 'New
Shared Sub Main()
Dim myForm As New MyFormControl()
myForm.ShowDialog()
End Sub 'Main
Public Sub Carica(ByVal S As String)
'Questa sub simula originariamente il Caricamento file e modifica del form
' il parametro S farà riferimento ad un select case S
'
'..............
'.............. codice
'..............
'
' questo codice emula il processo in elabolazione
For I As Long = 0 To 20000
myListBox.Items.Add(I)
myListBox.Update()
Application.DoEvents()
Next I
'Fine Sub
End Sub
Private Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim S As String = "Tutto"
myThread = New Thread(New ThreadStart(AddressOf ThreadFunction))
myThread.Start()
InputBox("Prova", "Prova InputBox") '<<<--- si bocca quà e non va avanti nonostante sia premuto OK??????
Beep() '<<---questo punto, viene eseguito solo se MyThred è termitato!! Come mai?????
End Sub 'Button_Click
Private Sub ThreadFunction()
Dim S As String = "Tutto"
Dim myThreadClassObject As New MyThreadClass(Me, S)
myThreadClassObject.Run()
End Sub
End Class 'MyFormControl
' The following code assumes a 'ListBox' and a 'Button' control are added to a form,
' containing a delegate which encapsulates a method that adds items to the listbox.
Public Class MyThreadClass
Private myFormControl1 As MyFormControl, Stringa As String
Public Sub New(ByVal myForm As MyFormControl, ByVal S As String)
myFormControl1 = myForm
Stringa = S
End Sub 'New
Public Sub Run()
' Execute the specified delegate on the thread that owns
' 'myFormControl1' control's underlying window handle.
myFormControl1.Invoke(myFormControl1.myDelegate, Stringa)
End Sub 'Run
End Class 'MyThreadClass
per cortesia, sto passando troppo tempo su questa implementazione, qualcuno mi può dare una dritta per poter andare avanti!!
Grazie ancora