in

DotNetSide

Dot Net South Italy Developers User Group

Latest post 31 Jul 2007 12:07 by leo.alario. 11 replies.
Page 1 of 1 (12 items)
Sort Posts: Previous Next
  • 11 Jun 2007 19:10

    • pixel
    • Top 25 Contributor
    • Joined on 30 May 2007
    • Posts 36
    • Points 430

    Come e quando sfruttare i Thread?

    Salve,

    sto realizzando un'applicazione che acquisisce dati da un dispositvo collegato alla rete LAN. L'interfaccia grafica del mio sw. deve aggiornarsi al cambiamento di alcuni dati (es. deve accendersi un led al momento in cui un dispositivo viene attivato e cose del genere...). Sto realizzando una Classe che sfrutti i drivers di comunicazione del dispositivo e metta a disposizione dei metodi per leggere e scrivere le informazioni. L'acquisizione dei dati deve essere fatta a ciclo continuo e ho il dubbio se sfruttare un controllo timer nel form principale o dotare la classe di un timer e scatenare un evento al quale passare le locazioni del dispositivo da leggere. Ho letto in alcuni libri che utilizzare il controllo Timer equivale a realizzare uno pseudo-Thread che permette di separare l'elaborazione dal form principale.Ick!

    Spero di essere stato chiaro

    P.S: ricordatevi che non sono espertissimo....Embarrassed

    • Post Points: 20
  • 12 Jun 2007 15:19 In reply to

    • Tiziana
    • Top 10 Contributor
      Female
    • Joined on 20 Mar 2006
    • Bari
    • Posts 588
    • Points 10,060

    Re: Come e quando sfruttare i Thread?

    Ok Ok...Parto dall'ultima frase che hai scritto e quindi ti dò da studiare Stick out tongue..Wink...scusa deformazione professionale Stick out tongue.

    A parte gli scherzi ...pixel ,per quanto intuito dalla tua richiesta, sarebbe utile approfondire l'"Event-based Asynchronous Pattern" qui ne trovi una Overview; infatti se l'applicazione non ha troppe pretese, il componente BackgroundWorker può esserti d'aiuto.Ci sono infatti molti esempi dell'utilizzo di questo componente anche usato insieme al controllo Timer .

    Se invece la tua applicazione ha maggiori pretese ti consiglio di leggere questo articolo  http://msdn2.microsoft.com/en-us/library/ms228966.aspx e decidere cosa è meglio nel tuo caso.

    Ciao!

     

     

     

    Tiziana Loporchio
    Software Solutions Architect

    Microsoft Certified Trainer
    Microsoft Certified Professional Developer
    My Blog - .NetSide Team Member

    • Post Points: 20
  • 14 Jun 2007 18:06 In reply to

    • pixel
    • Top 25 Contributor
    • Joined on 30 May 2007
    • Posts 36
    • Points 430

    Re: Come e quando sfruttare i Thread?

    Grazie per la risposta....

    ma ho un problema.... l'inglese non lo mastico molto bene Ick!EmbarrassedEmbarrassedEmbarrassed hai qualche riferimento in ITA

    Grazie ancora

    • Post Points: 20
  • 14 Jun 2007 18:39 In reply to

    • Tiziana
    • Top 10 Contributor
      Female
    • Joined on 20 Mar 2006
    • Bari
    • Posts 588
    • Points 10,060

    Re: Come e quando sfruttare i Thread?

    pixel:

    ma ho un problema.... l'inglese non lo mastico molto bene Ick!EmbarrassedEmbarrassedEmbarrassed hai qualche riferimento in ITA

    No no no...non ci siamo proprio...."The pen is on the table..." è fondamentale! Big Smile

    http://msdn2.microsoft.com/it-it/library/wewwczdw(VS.80).aspx

    ciao pigrone...Stick out tongueStick out tongue

    Tiziana Loporchio
    Software Solutions Architect

    Microsoft Certified Trainer
    Microsoft Certified Professional Developer
    My Blog - .NetSide Team Member

    • Post Points: 20
  • 14 Jun 2007 19:00 In reply to

    • pixel
    • Top 25 Contributor
    • Joined on 30 May 2007
    • Posts 36
    • Points 430

    Re: Come e quando sfruttare i Thread?

    EmbarrassedEmbarrassedEmbarrassedEmbarrassedEmbarrassedEmbarrassedEmbarrassedEmbarrassedEmbarrassed

    Mitica.... Grazie ancora ora vado a studiare

    Ciao

    • Post Points: 5
  • 04 Jul 2007 11:17 In reply to

    • pixel
    • Top 25 Contributor
    • Joined on 30 May 2007
    • Posts 36
    • Points 430

    Re: Come e quando sfruttare i Thread?

    Ciao,

    ho stuiato e devo dire che sarò "de coccio" io ma le guide MSDN e Microsoft in generale non misono molto simpatiche anche il odice alle volte sembra un pò troppo articolato.Ick!

    Comunque credo di essere riuscitoa creare un piccolo Form che esgue una operazione in un Thread separato con il controllo BackgroundWorker. Big SmileBig SmileBig Smile

    la funzione che viene eseguita in Background non fa altro che incrementare, attraverso un ciclo for, una variabile da 1 ad un valore molto grande. Tutto sembra fuzionare correttamente (il form si ridisegna, una progress bar incrementa la percentuale di avanzamento) eccetto che per il fatto che la form semba essere "inchiodata" Tongue Tied sull'operazione che sta eseguendo... mi spiego meglio se provo a spostare la form nmon succede nulla e, ancora più strano, non riesco a cancellare l'operazione col tasto deputato al comando cancelasync.

    Naturalmnete la proprieta "WorkerSupportCancellation" è impostata a True

    Grazie

    Pixel

    • Post Points: 5
  • 04 Jul 2007 11:18 In reply to

    • pixel
    • Top 25 Contributor
    • Joined on 30 May 2007
    • Posts 36
    • Points 430

    Re: Come e quando sfruttare i Thread?

    Ciao,

    ho stuiato e devo dire che sarò "de coccio" io ma le guide MSDN e Microsoft in generale non misono molto simpatiche anche il odice alle volte sembra un pò troppo articolato.Ick!

    Comunque credo di essere riuscitoa creare un piccolo Form che esgue una operazione in un Thread separato con il controllo BackgroundWorker. Big SmileBig SmileBig Smile

    la funzione che viene eseguita in Background non fa altro che incrementare, attraverso un ciclo for, una variabile da 1 ad un valore molto grande. Tutto sembra fuzionare correttamente (il form si ridisegna, una progress bar incrementa la percentuale di avanzamento) eccetto che per il fatto che la form semba essere "inchiodata" Tongue Tied sull'operazione che sta eseguendo... mi spiego meglio se provo a spostare la form nmon succede nulla e, ancora più strano, non riesco a cancellare l'operazione col tasto deputato al comando cancelasync.

    Naturalmnete la proprieta "WorkerSupportCancellation" è impostata a True

    Grazie

    Pixel

    • Post Points: 20
  • 04 Jul 2007 12:30 In reply to

    Re: Come e quando sfruttare i Thread?

    Ciao Pixel

    eccetto che per il fatto che la form semba essere "inchiodata"  sull'operazione che sta eseguendo

    Il fatto che la form sembri inchiodata mi fa pensare al fatto che *forse* l'operazione non venga realmente eseguita in un secondo thread.
    Puoi postare un po' di codice così gli do uno sguardo?

    Ciao

    Filed under:
    • Post Points: 20
  • 07 Jul 2007 19:44 In reply to

    • pixel
    • Top 25 Contributor
    • Joined on 30 May 2007
    • Posts 36
    • Points 430

    Re: Come e quando sfruttare i Thread?

    Scusate il ritardo nella risposta...

    ecco il codice :

     

    Imports System.ComponentModel

    System.ComponentModel

    Public Class Form1

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

    Dim bw As backgroundworker = CType(sender, backgroundworker)

     

    e.Result = Incremento(e.Argument, bw, e)

    If bw.CancellationPending Then

    Class Form1

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

    Dim bw As backgroundworker = CType(sender, backgroundworker)

     

    e.Result = Incremento(e.Argument, bw, e)

    If bw.CancellationPending Then

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

    Dim bw As backgroundworker = CType(sender, backgroundworker)

     

    e.Result = Incremento(e.Argument, bw, e)

    If bw.CancellationPending Then

    Dim bw As backgroundworker = CType(sender, backgroundworker)

     

    e.Result = Incremento(e.Argument, bw, e)

    If bw.CancellationPending Then

    If bw.CancellationPending Then

    e.Cancel = True

    True

    End If

    End If

    End Sub

    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted

    If e.Cancelled Then

    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted

    If e.Cancelled Then

    If e.Cancelled Then

    MessageBox.Show("Operazione Cancellata")

    ElseIf (e.Error IsNot Nothing) Then

    "Operazione Cancellata")

    ElseIf (e.Error IsNot Nothing) Then

    ElseIf (e.Error IsNot Nothing) Then

    MessageBox.Show("Errore")

    Else

    "Errore")

    Else

    Else

    MessageBox.Show(e.Result.ToString)

    End If

    End If

    End Sub

    End Sub

    Private Function Incremento(ByVal fine As Double, ByVal worker As BackgroundWorker, ByVal e As DoWorkEventArgs) As Double

    Private Function Incremento(ByVal fine As Double, ByVal worker As BackgroundWorker, ByVal e As DoWorkEventArgs) As Double

    Static i As Double

    Static i As Double

    Dim result As Double = 0

    Dim percent As Double

    Dim result As Double = 0

    Dim percent As Double

    Dim percent As Double

    If worker.CancellationPending Then

    If worker.CancellationPending Then

    e.Cancel = True

    True

    Else

    Else

    For i = 1 To fine Step 1

    If i >= fine Then Exit For

    For i = 1 To fine Step 1

    If i >= fine Then Exit For

    If i >= fine Then Exit For

    percent = (i / fine) * 100

    worker.ReportProgress(percent)

    Next i

    End If

    Next i

    End If

    End If

    result = i

    Return result

    End Function

    Return result

    End Function

    End Function

    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged

    Me.ProgressBar1.Value = e.ProgressPercentage

    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged

    Me.ProgressBar1.Value = e.ProgressPercentage

    End Sub

    Me.ProgressBar1.Value = e.ProgressPercentage

    End Sub

    End Sub

    Private Sub StartBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartBtn.Click

    Me.BackgroundWorker1.RunWorkerAsync(1000000)

    End Sub

    Private Sub StartBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartBtn.Click

    Me.BackgroundWorker1.RunWorkerAsync(1000000)

    End Sub

    Me.BackgroundWorker1.RunWorkerAsync(1000000)

    End Sub

    End Sub

    Private Sub cancelBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelBtn.Click

    Me.BackgroundWorker1.CancelAsync()

    End Sub

    Private Sub cancelBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelBtn.Click

    Me.BackgroundWorker1.CancelAsync()

    End Sub

    Me.BackgroundWorker1.CancelAsync()

    End Sub

    End Sub

     

    Ciao e Grazie

    • Post Points: 5
  • 12 Jul 2007 21:24 In reply to

    • pixel
    • Top 25 Contributor
    • Joined on 30 May 2007
    • Posts 36
    • Points 430

    Re: Come e quando sfruttare i Thread?

    Ciao a tutti 

    Ancora non ho avuto modo di applicarmi bene al problema... ma la cosa strana è che l'interfaccia sembra aggiornarsi correttamente e quindi credo che la crazione e l'elaborazione del thread avvenga.... bhoo!

    Con pazienza attendo

    • Post Points: 5
  • 31 Jul 2007 10:33 In reply to

    • pixel
    • Top 25 Contributor
    • Joined on 30 May 2007
    • Posts 36
    • Points 430

    Re: Come e quando sfruttare i Thread?

    Ma il buon vecchio DoEvents non è più supportato vero? [:'(]
    • Post Points: 20
  • 31 Jul 2007 12:07 In reply to

    Re: Come e quando sfruttare i Thread?

    Application.DoEvents()
    • Post Points: 5
Page 1 of 1 (12 items)
Powered by Community Server (Commercial Edition), by Telligent Systems