Thread beenden, auserhalb einer Methode



  • Hallo,

    ich habe folgenden Code:

    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    				 //Thread starten
    					ThreadStart^ threadDelegate = gcnew ThreadStart( &Parkour::Start_Run );
    					Thread^ runThread = gcnew Thread( threadDelegate );
    					runThread->Start();
    	}
    

    Nun startet der Thread auch aber ichwenn ich ihn durch eine andere Methode beenden will z.B.:

    private: System::Void Form1_FormClosed(System::Object^  sender, System::Windows::Forms::FormClosedEventArgs^  e) {
    				 //Thread beenden
    					runThread->Abort();
    	}
    

    Dan kommt ein fehler das folgendes:

    1>c:\dokumente und einstellungen\bnightspeeder\eigene dateien\visual studio 2005\projects\robot controller\robot controller\Form1.h(538) : error C2065: 'runThread' : undeclared identifier
    1>c:\dokumente und einstellungen\bnightspeeder\eigene dateien\visual studio 2005\projects\robot controller\robot controller\Form1.h(538) : error C2227: left of '->Abort' must point to class/struct/union/generic type

    Könnte mir jemand sagen wieso das nicht geht?



  • runThread ist nur innerhalb der Methode (button1_Click) definiert:

    Anstatt innerhalb der Methode (button1_Click) sollte man runThread in der Klasse (Form1) definieren



  • Hier?

    ///          'Resource File Name' property for the managed resource compiler tool
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    	public ref class Form1 : public System::Windows::Forms::Form
    	{
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    			//
    			//TODO: Add the constructor code here
    			//
    			ThreadStart^ threadDelegate = gcnew ThreadStart( &Parkour::Start_Run );
    			Thread^ runThread = gcnew Thread( threadDelegate );
    		}
    
    	protected:
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		~Form1()
    		{
    			if (components)
    			{
    				delete components;
    			}
    		}
    

    [cpp]

    Nur kommt dan ein Fehler!



  • Äh, ... knapp daneben (ich hab' es wahrscheinlich falsch beschrieben):

    /* Ich versuch mal es mal, jedoch ich bin mir nicht sicher ob es semantisch oder logisch richtig ist */
    
    public ref class Form1 : public System::Windows::Forms::Form
    {
    	private:
    		Thread^ runThread = null; // <--- Hier hin
    	public:
    		Form1(void) {
    			InitializeComponent();
    		}
    	protected:
           	~Form1() {
    			if (components)
    			{
    				delete components;
    			}
    			// Falls das Fenster geschlossen wurde / wird 
    			// ohne das der Thread-abbrechen-butten gedrückt wurde:
    			if (runThread != null)
    			{
    				runThread.Abort(); // Vielleicht nicht ganz richtig
    				runThread == null;
    			}
    		}
    
    	private: 
    		System::Void button1_Click(
    				System::Object^  sender,
    				System::EventArgs^  e) {
    			//Thread starten
    			ThreadStart^ threadDelegate = gcnew ThreadStart( &Parkour::Start_Run );
    			runThread = gcnew Thread( threadDelegate );
    			runThread->Start();
    		}
    
    	private:
    		System::Void Form1_FormClosed(
    				System::Object^  sender,
    				System::Windows::Forms::FormClosedEventArgs^  e) {
    			//Thread beenden
    			if (runThread != null)
    			{
    				runThread->Abort();
    			}
    		}
    }
    


  • public ref class Form1 : public System::Windows::Forms::Form
    {
        private:
            static Thread^ runThread = null; // <--- Hier musste noch ein static hin
        public:
            Form1(void) {
                InitializeComponent();
            }
        protected:
               ~Form1() {
                if (components)
                {
                    delete components;
                }
                // Falls das Fenster geschlossen wurde / wird
                // ohne das der Thread-abbrechen-butten gedrückt wurde:
                if (runThread != null)
                {
                    runThread->Abort(); // So ist es richtig
                    runThread == null;
                }
            }
    
        private:
            System::Void button1_Click(
                    System::Object^  sender,
                    System::EventArgs^  e) {
                //Thread starten
                ThreadStart^ threadDelegate = gcnew ThreadStart( &Parkour::Start_Run );
                runThread = gcnew Thread( threadDelegate );
                runThread->Start();
            }
    
        private:
            System::Void Form1_FormClosed(
                    System::Object^  sender,
                    System::Windows::Forms::FormClosedEventArgs^  e) {
                //Thread beenden
                if (runThread != null)
                {
                    runThread->Abort();
                }
            }
    }
    

    Also es kamen zei Fehler, einmal das es nicht static ist, was ich schon geändert habe, aber dan kommt noch einer den ich überhaupt nicht kenne:

    : error C2065: 'null' : undeclared identifier



  • null => nullptr



  • thx, funktioniert einwandfrei!

    //Edit:

    Man sollte noch verhindern das ein Thread doppelt gestartet wird, am besten wenn man eine if drüber macht:

    if (runThread_Parkour == nullptr) { //Überprüfen ob schon gestartet
    							ThreadStart^ threadDelegate = gcnew ThreadStart( &Parkour::Start_Run );
    							runThread_Parkour = gcnew Thread( threadDelegate );
    							runThread_Parkour->Start(); //Starten
    						}
    

Anmelden zum Antworten