Entwickler-Ecke
WinForms - Text von textBox sofort ändern und anzeigen
deepmessage - So 13.01.08 13:51
Titel: Text von textBox sofort ändern und anzeigen
Hallo,
mein Problem betrifft eine Klasse, die von "Form" ableitet".
Es gibt einen Button, der wie folgt aussieht:
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:
| private void button1_Click(object sender, EventArgs e) { textBoxStatus.Text = "Verbinde zu Server..."; ftp = new FTPclient("unitedserver.de", "web1f229", "6ri554tOp?Gs"); if (testFTPConnection()) { button1.Enabled = false; fileInformation = new OFileInformations(); textBoxStatus.Text = "Suche vorhandene Dateien..."; fileInformation.init(this, ftp);
listBox1.Sorted = true; listBox1.BeginUpdate();
foreach (String s in fileInformation.allFileNames()) this.listBox1.Items.Add(s);
listBox1.EndUpdate(); textBoxStatus.Text = "fertig"; } } |
Das Problem ist, dass das Form etwa 10 Sekunden nicht reagiert und dann in der textBoxStatus nur noch "fertig" steht. Alles andere wurde nicht anzeigt.
Wie kann ich das Problem beheben und die anderen Status dem Benutzer anzeigen?
Gruß
Alex
Robert_G - So 13.01.08 14:16
Titel: Re: Text von textBox sofort ändern und anzeigen
Du solltest die langwierigen Operationen (also Verbindungstest, Holen der Daten etc) in einem Thread ausführen.
Das könnte so aussehen. Aber man könnte den Code mit weniger Invoke-Calls lösen, wenn "fileInformation" eine lokale variable wäre.
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:
| private void button1_Click(object sender, EventArgs e) { textBoxStatus.Text = "Verbinde zu Server..."; WebRequest ftp = FtpWebRequest.Create("ftp://unitedserver.de"); ftp.Credentials = new NetworkCredential("web1f229", "6ri554tOp?Gs");
button1.Enabled = false;
WaitCallback asyncCall = delegate { if (testFTPConnection()) {
Invoke((MethodInvoker)delegate { textBoxStatus.Text = "Suche vorhandene Dateien..."; fileInformation = new OFileInformations(); listBox1.Sorted = true; listBox1.BeginUpdate(); });
fileInformation.init(this, ftp);
foreach (String s in fileInformation.allFileNames()) Invoke((MethodInvoker)delegate { listBox1.Items.Add(s); });
Invoke((MethodInvoker)delegate { listBox1.EndUpdate();
textBoxStatus.Text = "fertig"; }); } Invoke((MethodInvoker)delegate { button1.Enabled = true; }); };
ThreadPool.QueueUserWorkItem(asyncCall); } |
deepmessage - So 13.01.08 15:14
Titel: Threads...
wow - danke Robert! Das funktioniert genau so, wie ich es haben wollte!
Alex
Entwickler-Ecke.de based on phpBB
Copyright 2002 - 2011 by Tino Teuber, Copyright 2011 - 2026 by Christian Stelzmann Alle Rechte vorbehalten.
Alle Beiträge stammen von dritten Personen und dürfen geltendes Recht nicht verletzen.
Entwickler-Ecke und die zugehörigen Webseiten distanzieren sich ausdrücklich von Fremdinhalten jeglicher Art!