Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    4,826

    background worker e semafori

    ciao.
    Ho due background worker uno che si deve attivare ogni 10 sec e uno ogni 50 sec.
    e devosincronizzarli , ho pensato di usare un semaforo , dichiarato come shared(vb.net) in un wrapper attorno al background worker.
    posto subito il codice un po semplificato:

    codice:
    'Background worker ogni 10 sec
    Public Class BWSpooler
      Dim bw As BackgroundWorker = New BackgroundWorker
      Public Sub New()
        bw.WorkerSupportsCancellation = True
        bw.WorkerReportsProgress = True
        AddHandler bw.DoWork, AddressOf DoWork
        'AddHandler bw.ProgressChanged, AddressOf bw_ProgressChanged
        AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted
    
      End Sub
    
      Private Sub DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
        Try
          Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
          Dim i As Integer
    
          For i = 1 To 10
            If bw.CancellationPending = True Then
              e.Cancel = True
              Exit For
    
            Else
    
              Threading.Thread.Sleep(10000)
    
              BWScheduler.mainCount += 1
    
              Try
                BWScheduler.semap.Release()
              Catch
    
              End Try
    end sub
    end class
    e il primo ogni 50 sec:
    codice:
    Public Class BWScheduler
      Dim bw As BackgroundWorker = New BackgroundWorker
      Public Shared semap As New System.Threading.Semaphore(0, 1)
      Public Shared releaseCount As Integer = 0
      Public Shared mainCount As Integer = 0
      Private n As Integer = 0
      Public Sub New()
    
        AddHandler bw.DoWork, AddressOf bw_DoWork
        'AddHandler bw.ProgressChanged, AddressOf bw_ProgressChanged
        AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted
    
      End Sub
    
      Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
        Try
    
          Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
          Dim i As Integer
    
          For i = 1 To 10
            If bw.CancellationPending = True Then
              e.Cancel = True
    
              Exit For
            Else
              BWScheduler.semap.WaitOne()
              ' Perform a time consuming operation and report progress.
              System.Threading.Thread.Sleep(50000)
              BWScheduler.releaseCount += 1
    next
    end sub
    end class
    ora , ho provato col try catch nel
    codice:
              Try
                BWScheduler.semap.Release()
              Catch
    
              End Try
    e dopo la prima volta mi da un errore , ho provato a mettere nel catch un BWScheduler.semap.WaitOne() ma dopo mi si interrompe il thread.

    ho tntato di fare questo per sincronizzare i due background worker.
    Ho anche provato a mettere in ciascun backround worker un BWScheduler.semap.WaitOne() all'inizio e un release alla fine ma cosi devo aspettare il bw piu lento e non funziona come vorrei .

    ps. voglio sincronizzare pperchè i due bw nell'applicazione reale lavorano sulle stesse risorse e non voglio sovrapposizioni o casini di ogni genere
    grazie.
    Ultima modifica di giuseppe500; 16-07-2014 a 10:03

  2. #2
    Non ho capito molto del tuo codice... ma ti dico quello che farei io (se ho ben capito cosa intendi per sincronizzare).

    Creati BW1 e BW2 inserirei nel codice 2 Timers, uno con intervallo 10 secondi e l' altro con intervallo 50 secondi.

    In Timer1:

    BW1.RunWorkerAsync

    in Timer2:

    Do While BW1.IsBusy
    Sleep(100)
    Application.DoEvents
    Loop
    BW2.RunWorkerAsync
    Ultima modifica di eziogsv; 17-07-2014 a 14:20

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.