[C#] Backgroundworker

OP #1359518
Lesenswert?
• ▲
▼
Hallo zusammen

Ich habe ein Problem mit dem Backgroundworker. Und zwar rufe ich eine 
Endlosschlaufe, welche immer überprüft ob abgebrochen wurde, auf. Nun 
kann ich diese aber nicht mehr beenden.
1
        private void main()
2
        {
3
            bw.RunWorkerAsync();
4
            if (MessageBox.Show("writing data finished!\r\npress ok to continue...") == DialogResult.OK)
5
            {
6
                bw.CancellationPending=true
7
            }
8
        }
9

10
        private void bw_DoWork(object sender, DoWorkEventArgs e)
11
        {
12
            PBSendData(0xF0);
13
            PBSendData(0x00);
14
            PBSendData(0x00);
15
            for (int j = 0; j < 3; j++)
16
            {
17
                readData();
18
            }
19
            for ( int i = 0; i < 1; i = 0)
20
            {
21
                PBSendData(0xFF);
22
                readData();
23
                if (bw.CancellationPending)
24
                {
25
                    e.Cancel = true;
26
                    return;
27
                }
28
            }
29
            
30
        }

Gruss Dexter
#1359867
Lesenswert?
• ▲
▼
Probiers einfach mal mit bw.CancelAsync();
die Eigenschaft ist meines Wissens Read-Only.

Siehe auch 
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker_methods.aspx

PS:
besser wäre:
1
while( !bw.CancellationPending) {
2
  PBSendData(0xFF);
3
  readData();
4
} 
5
e.Cancel = bw.CancellationPending;
so ist auf den ersten Blick erkennbar, wodurch die Schleife beendet 
werden kann und dass es sich nicht um eine "Zählschleife" handelt
#1359940
Lesenswert?
• ▲
▼
Ahh, das heißt, das Abbrechen Funktioniert aber du kannst ihn nicht neu 
starten.

Versuchst du den Neustart sofort nach dem CancelAsync()? vieleicht ist 
der BW wirklich noch nicht fertig und hängt noch in z.B. der readData 
Methode.

Ansonsten gäbe es noch den Hack, einfachden BW neu zu erzeugen also 
folgendermaßen:
1
private BackgroundWorker bw = null;
2

3
        private void main()
4
        {
5
            StartBW();
6
            if (MessageBox.Show("writing data finished!\r\npress ok to continue...") == DialogResult.OK)
7
            {
8
                bw.CancellationPending=true
9
            }
10
        }
11

12
  private void StartBW() {
13
    if(bw != null) {
14
      bw.DoWork -= bw_DoWork;
15
      bw.Dispose();
16
    }
17
    bw = new BackgroundWorker();
18
    bw.DoWork += bw_DoWork;
19
    bw.RunWorkerAsync();
20
  }
21

22
        private void bw_DoWork(object sender, DoWorkEventArgs e)
23
        {
24
            PBSendData(0xF0);
25
            PBSendData(0x00);
26
            PBSendData(0x00);
27
            for (int j = 0; j < 3; j++)
28
            {
29
                readData();
30
            }
31
            while( !bw.CancellationPending) {
32
              PBSendData(0xFF);
33
              readData();
34
            } 
35
            e.Cancel = bw.CancellationPending;
36
        }

Alles ohne Garantie auf Richtigkeit, da ich im Mom leider kein VS zur 
verfügung hab.

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren