Autor Beitrag
chrisachern
Hält's aus hier
Beiträge: 14



BeitragVerfasst: Fr 09.03.07 11:36 
hallo. habe folgenden code

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:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace DelegateTest
{
    public partial class Form1 : Form
    {
        delegate void TextUpdate1(string text1);
        private bool running = true;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
                Thread f = new Thread(new ThreadStart(funk));
                f.Start();
        }

        private void funk()
        {
            for (int i=0; i<1000; i++)
            {
                     if (textBox1.InvokeRequired)
                    {
                        TextUpdate1 gg = new TextUpdate1(settext);
                        Invoke(gg, new object[] { i.ToString() });
                        Thread.Sleep(10);
                    }
                    else
                        textBox1.Text = i.ToString();
                             
            }
        }
        private void settext(string text1)
        {
            textBox1.Text = text1;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + "d";
        }
    }
}


funktioniert auch wunderbar. meine frage ist aber: wenn ich meine userform schließe, versucht der thread weiterhin auf die verworfene textbox1 zu schreiben. wie kann ich dies verhindern und den thread stoppen?

gruß chris

Moderiert von user profile iconKlabautermann: cs-Tags eingefügt.
Bex
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 47

Win XP, Linux
C# (VS 2008), Java (Eclipse)
BeitragVerfasst: Fr 09.03.07 12:45 
Wie wäre es damit, die "OnClosed()"-Methode von Form1 zu überschreiben?

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
protected override void OnClosed(EventArgs e)
    {
        // hier den Thread beenden
        ...

        base.OnClosed(e);
    }
LexyK
Hält's aus hier
Beiträge: 8



BeitragVerfasst: Sa 10.03.07 11:30 
Hallo zusammen,
was machen ich, wenn ich keine solche button1_Click Methode habe.

user profile iconchrisachern hat folgendes geschrieben:


ausblenden C#-Quelltext
1:
2:
3:
4:
5:
        private void button1_Click(object sender, EventArgs e)
        {
                Thread f = new Thread(new ThreadStart(funk));
                f.Start();
        }

Moderiert von user profile iconKlabautermann: cs-Tags eingefügt.


Also wenn ich nachdem die Form geladen wurde, der Thread starten soll?
Vielen Dank für Eure Hilfe

P.S. Sorry, daß ich Deine Nachricht mißbrauche. Wollte nur nicht nochmal einen neuen aufmachen.
Bex
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 47

Win XP, Linux
C# (VS 2008), Java (Eclipse)
BeitragVerfasst: Mo 12.03.07 10:50 
Auch dafür kann man die Methoden, die System.Windows.Form anbietet, benutzen, bspw. OnShown().