Hi,
Ich möchte noch anmerken, dass die Methode mit Application.DoEvents()
eher die Quick and VERY DIRTY - Methode ist.
Besser ist es, das Ganze mit Threads zu machen, was ja dank
Backgroundworker überhaupt keine Hexerei ist.
Anbei ein Kleines Beispiel, wie das Ganze mit dem BW funktioniert (inkl.
Parameterübergabe, Ergebnisrückgabe und Fortschrittsmeldung
zwischendurch)
1 | private BackgroundWorker m_BackgroundWorker = new BackgroundWorker();
|
2 |
|
3 | public Form1() {
|
4 | InitializeComponent();
|
5 |
|
6 | m_BackgroundWorker.DoWork += new DoWorkEventHandler(m_BackgroundWorker_DoWork);
|
7 | m_BackgroundWorker.ProgressChanged += new ProgressChangedEventHandler(m_BackgroundWorker_ProgressChanged);
|
8 | m_BackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(m_BackgroundWorker_RunWorkerCompleted);
|
9 | m_BackgroundWorker.WorkerReportsProgress = true;
|
10 | }
|
11 |
|
12 | private void m_BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
|
13 | // GUI - Thread
|
14 |
|
15 | //e.Cancelled ... Indicates if the Operation was cancelled
|
16 | //e.Error ... An Exception which was thrown inside the Async Operation
|
17 | //e.Result ... A Result Object
|
18 | }
|
19 |
|
20 | private void m_BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
|
21 | // GUI - Thread
|
22 |
|
23 | //z.B.:
|
24 | //m_Progressbar.Value = e.ProgressPercentage
|
25 | //Do Something with e.UserState
|
26 | }
|
27 |
|
28 | private void m_BackgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
|
29 | //Worker - Thread
|
30 |
|
31 | //Do Something with e.Argument
|
32 | int i = 0;
|
33 | do {
|
34 | m_BackgroundWorker.ReportProgress(i++ % 100/*, Optional: any Object*/);
|
35 | } while (!e.Cancel);
|
36 | //e.Result = Any result Object
|
37 | }
|
38 |
|
39 | private void button1_Click(object sender, EventArgs e) {
|
40 | m_BackgroundWorker.RunWorkerAsync(/*Optional: Any Object*/);
|
41 | }
|
42 |
|
43 | private void button2_Click(object sender, EventArgs e) {
|
44 | m_BackgroundWorker.CancelAsync();
|
45 | }
|
greets
PS: Ich appeliere nochmal an dich BITTE nimm nicht
Application.DoEvents(), du handelst dir damit früher oder später nur
Probleme ein.