Home Page
Articoli
Tips & Tricks
News
Forum
Archivio Forum
Blogs
Sondaggi
Rss
Video
Utenti
Chi Siamo
Contattaci
Username:
Password:
Login
Registrati ora!
Recupera Password
Home Page
Stanze Forum
App. WinForms / WPF .NET
Probelma con BackgroundWorker
mercoledì 18 ottobre 2006 - 00.31
Elenco Threads
Stanze Forum
Aggiungi ai Preferiti
Cerca nel forum
contiello
Profilo
| Senior Member
260
messaggi | Data Invio:
mer 18 ott 2006 - 00:31
Salve ragazzi ho il seguente problema ho una form che effettua attraverso un componente BackgroundWorker un processo appunto in background.
ora riesco a farlo funzionare nel componente BackgroundWorker ma non riesco ad associargli una progressbar
le istruzioni sono le seguenti e molto semplici
Public Class Form3
Public Sub pippo()
Dim i As Integer
For i = 0 To 100
Me.ProgressBar1.Value = i
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
pippo()
End Sub
l'errore che mi viene restituito è il seguente..
Operazione cross-thread non valida: è stato eseguito l'accesso al controllo 'ProgressBar1' da un thread diverso da quello da cui è stata eseguita la creazione.
ho capito l'errore ma non so come ovviare ad esso
Grazie mille
RedStripe
Profilo
| Newbie
31
messaggi | Data Invio:
mer 18 ott 2006 - 01:35
se usi il BackGroundWorker il lavoro passa a un altro thread
per avere notifiche sull' incremento del lavoro devi usare il metodo
ReportProgress dell' oggetto BackGroundWorker
Questo metodo ti solleva un evento (ProgressChanged) che è accessibile dal form e quindi dall' altro thread.
L' evento ti espone l' oggetto ProgressChangedEventArgs da cui puoi leggere l' incremento, così:
Private Sub backgroundWorker1_ProgressChanged( _
ByVal sender As Object, ByVal e As ProgressChangedEventArgs) _
Handles backgroundWorker1.ProgressChanged
Me.progressBar1.Value = e.ProgressPercentage
End Sub
Dimenticavo, devi settare anche queste due proprietà
per monitorare e sospendere il lavoro se necessario
Me.backgroundWorker1.WorkerReportsProgress = True
Me.backgroundWorker1.WorkerSupportsCancellation = True
CIAO!
Per approfondire :
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref3/html/T_System_ComponentModel_BackgroundWorker.htm
i
Torna su
Stanze Forum
Elenco Threads
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?
Dopo esserti registrato potrai chiedere
aiuto sul nostro
Forum
oppure aiutare gli altri
Consulta le
Stanze
disponibili.
Registrati ora !