Autor Beitrag
snowy1980
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 23



BeitragVerfasst: Do 30.04.09 11:24 
Hallo,

Ich arbeite mit grad in Sachen in Backgroundworker ein.

Auf einem Formular habe ich einen Button "Start". Bei klicken soll erstmal nur ein Text in der Listbox erscheinen.
Das soll dann mal ersetzt werden, durch zeitunbegrenzte Operationen. Deshalb soll ein Abbrechen möglich sein.

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
namespace Test_Backgroundworker
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //Fortschritt anzeigen
        void ShowProgress(string t)
        {
            // Make sure we're on the UI thread
            Debug.Assert(this.InvokeRequired == false);

            // Display progress in UI
            this.listBox1.Items.Add(t);
        }

        //Kommunikationsobject?
        class TestUserState
        {
            public readonly string t;
            
            //Konstruktor
            public TestUserState(string t)
            {
                this.t = t;
            }
        }

        //intensive Berechnungsfunktion
        void ToDo(string s)
        {
            //"Berechnung"

            MessageBox.Show("ToDo");
            

            // Report continuing progress
            this.backgroundWorker1.ReportProgress(0,new TestUserState(s));

            // Check for cancelation
            if (this.backgroundWorker1.CancellationPending) 
            { 
                return;
            }
        }

        //Backgroundworker starten
        //Buttonlogik
        private void Start(object sender, EventArgs e)
        {
            // Don't process if cancel request pending
            // (Should not be called, since we disabled the button...)
            if (this.backgroundWorker1.CancellationPending) { return; }

            // If worker thread currently executing, cancel it
            if (this.backgroundWorker1.IsBusy)
            {
                this.btnstart.Enabled = false;
                this.backgroundWorker1.CancelAsync();
                return;
            }
            
            // Set calculating UI
            this.btnstart.Text = "Cancel";

            MessageBox.Show("Start");
            // Begin asynchronously
            this.backgroundWorker1.RunWorkerAsync("Test");
        }
        
        //Backgroundworker Arbeitsmethode
        private void BW_DoWork(object sender, DoWorkEventArgs e)
        {
            MessageBox.Show("BW_DoWork");
            ToDo((string)e.Argument);

            // Indicate cancelation
            if (this.backgroundWorker1.CancellationPending)
            {
                e.Cancel = true;
            }

        }

        //Backgroundworker Fortschritt mitteilen
        private void BW_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            MessageBox.Show("ProgressChanged");
            // Show progress
            TestUserState progress = (TestUserState)e.UserState;
            ShowProgress(progress.t);
        }


    }
}


Es wird nichts ausgegeben in der Listbox. Es erscheint nur die Messagebox "Start".

VG
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4807
Erhaltene Danke: 1061

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Do 30.04.09 11:41 
Sind die Events des Backgroundworker denn auch abonniert (in der zugehörigen designer.cs-Datei)?
snowy1980 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 23



BeitragVerfasst: Do 30.04.09 11:45 
Hab den Fehler grad gefunden. Habe die Ereignisse nicht bestückt :-).

Jetzt erscheint der Text. Nur der Button bleibt auf Cancel. An welcher Stelle muss ich ihn wieder auf Start coden?

Im nächsten Schritt möchte bei Klick auf Start, das der Text in bestimmten Thread.Sleep(1000) Zeitabständen in die Listbox geschrieben wird. Dazu soll dann das Abbrechen möglich sein.

Wo bau ich die Endlosschleife ein?
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4807
Erhaltene Danke: 1061

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Do 30.04.09 12:21 
Zitat:

Habe die Ereignisse nicht bestückt

wußt ich's doch, denn das heißt offiziell "abonnieren".

Für das Ende eines Backgroundworker-Prozesses gibt es das RunWorkerCompleted-Ereignis.

Und die Schleife gehört natürlich in das DoWork-Ereignis bzw. die ToDo-Methode.