Adblock breaks this site

Threads and FormClosing

Discussion in 'Programming General' started by Supah Fly, Dec 18, 2008.

  1. Supah Fly

    Supah Fly Active Member
    Banned

    Joined:
    Aug 22, 2007
    Posts:
    202
    Referrals:
    1
    Sythe Gold:
    0
    Threads and FormClosing

    i need a thread exit method (Like FormClosing but like ThreadEnding i guess)

    Code:
    			for (int i = 0; i < numericUpDown1.Value; i++)
    			{
    				if (runningThreads <= numericUpDown1.Value)
    				{
    					az = new Thread(bg2a);
    					try
    					{
    						az.Start();
    					}
    					catch (Exception ex) {
    						MessageBox.Show(Convert.ToString(ex.Message));
    					}
    					runningThreads++;
    					label10.Text = "Threads running: " + runningThreads;
    				}
    			}
    theres the code im running. i need something just for when the thread exits.

    also i need a formclosing method... i dont know why mine isnt working -.- ive tried everything... even googling and trying those.
     
  2. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    Threads and FormClosing

    You want a method to close the form? or to do something when the form is closing?

    You should find this resource helpful too.
    http://www.albahari.com/threading/
     
  3. Supah Fly

    Supah Fly Active Member
    Banned

    Joined:
    Aug 22, 2007
    Posts:
    202
    Referrals:
    1
    Sythe Gold:
    0
    Threads and FormClosing

    when the form is closing.

    and i need something for when the thread is exiting.. i read all of that before and again today, nothing there that helps.
     
  4. hampe-92

    hampe-92 Forum Addict

    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0
    Threads and FormClosing

    I don't really understand what you mean but the FormClosing event won't work if you are using this.Dispose() .. you have to use this.Close() for that to work..

    and just a tip: "MessageBox.Show(Convert.ToString(ex.Message));" is the same as "MessageBox.Show(ex.Message.ToString());" but this is shorter ;)
    but anyway.. could you explain a little further what you actually mean?
     
  5. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    Threads and FormClosing

    You could try adding each of the threads to a list, then upon form close or any sort of exception simply loop through that list and terminate them all.

    More information on lists: http://msdn.microsoft.com/en-au/library/6sh2ey19.aspx

    In C++ I would normally use pointers for this, but unfortunately Microsoft didn't implement pointers very well in to C#.
     
  6. Supah Fly

    Supah Fly Active Member
    Banned

    Joined:
    Aug 22, 2007
    Posts:
    202
    Referrals:
    1
    Sythe Gold:
    0
    Threads and FormClosing

    i just want to change what happens when the thread exits and when the form exits...

    Ex:
    Code:
    private void Form1_FormClosing(object sender, EventArgs e) {
    // code i want to do in here...
    }
    Code:
    private void Thread_Exit(object sender, EventArgs e) {
    // code i want to do when the thread exits here...
    }
     
  7. hampe-92

    hampe-92 Forum Addict

    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0
    Threads and FormClosing

    well, I can't see what the problem with FormClosing is :confused:
    just cancel the closing and then do what you want to do and then close the form if you wish to do so..

    Code:
    private void Form1_FormClosing(object sender, EventArgs e) {
    e.Cancel = true;
    
    // Do whatever you want to do before the form is closed
    
    this.Close();
    }
     
  8. Supah Fly

    Supah Fly Active Member
    Banned

    Joined:
    Aug 22, 2007
    Posts:
    202
    Referrals:
    1
    Sythe Gold:
    0
    Threads and FormClosing

    it just ignores the fact that its there.. i dont get it..
     
  9. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    Threads and FormClosing

    Here's an example I put together:

    Code:
    using System;
    using System.Windows.Forms;
    
    using System.Threading;
    
    namespace ThreadingExample
    {
        public partial class Form1 : Form
        {
            protected Thread MyThread;
    
            public Form1()
            {
                InitializeComponent();
    
                MyThread = new Thread(new ThreadStart(RunThread));
    
                MyThread.Start();
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                MyThread.Join();
    
                Console.WriteLine("Form closing ...");
            }
    
            private void RunThread()
            {
                int i = 0;
    
                Console.WriteLine("Thread starting ...");
    
                do
                {
                    Console.Write("{0}, ", ++i);
                    Thread.Sleep(1000);
                } while (i < 10);
    
                Console.WriteLine();
    
                ThreadExiting();
            }
    
            private void ThreadExiting()
            {
                Console.WriteLine("Thread exiting ...");
            }
        }
    }
    
    The FormClosing() event works for me. I'm still not exactly sure what you're trying to do, but I hope this helps.
     
  10. Supah Fly

    Supah Fly Active Member
    Banned

    Joined:
    Aug 22, 2007
    Posts:
    202
    Referrals:
    1
    Sythe Gold:
    0
    Threads and FormClosing

    I'm running windows forms and not console applications, maybe that is why?

    Thanks for the thread exiting part I hadn't thought of that.
     
  11. hampe-92

    hampe-92 Forum Addict

    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0
    Threads and FormClosing

    you do know that there is an FormClosing event to select in the list of events for the Form, right?
    if you don't, go to design view then to the properties tab and click the lightening icon (events) and scroll down to "FormClosing" and double click it (I guess you knew the first part);)
    If you knew this I don't understand at all what the problem is
     
  12. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    Threads and FormClosing

    What are you talking about? That code IS Windows Forms.
     
  13. hampe-92

    hampe-92 Forum Addict

    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0
    Threads and FormClosing

    I think he got confused by "Console.WriteLine()":p
     
  14. x&#8313;

    x&#8313; Guest

    Referrals:
    1
    Threads and FormClosing

    Replace the call to Console.WriteLine() with MessageBox.Show(), then.
    It just may be slightly more annoying to run.
     
  15. hampe-92

    hampe-92 Forum Addict

    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0
    Threads and FormClosing

    ah, I hoped he could figure that out by him self :laugh:
     
  16. Supah Fly

    Supah Fly Active Member
    Banned

    Joined:
    Aug 22, 2007
    Posts:
    202
    Referrals:
    1
    Sythe Gold:
    0
    Threads and FormClosing

    Only visual basic has that for me. Not on C# or C++.

    Well obviously this was used in a console application, I thought, since it wasn't/isn't working, it might be different on windows forms applications.
     
  17. hampe-92

    hampe-92 Forum Addict

    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0
    Threads and FormClosing

    what?? that's weird.. you must have it there.. are you sure??:confused: check once again for "FormClosing".. there should be both a "FormClosing" and a "FormClosed" if you order them in categories it should be in "Behavior".. I promise, it IS there
     
  18. Supah Fly

    Supah Fly Active Member
    Banned

    Joined:
    Aug 22, 2007
    Posts:
    202
    Referrals:
    1
    Sythe Gold:
    0
    Threads and FormClosing

    C#:
    [​IMG]

    VB.NET:
    [​IMG]

    Took those screenshots a few seconds ago. That's what I see. I know what you are talking about though.

    Oh.. I didn't get what you were saying before, I see it now. That makes alot more sense now. It works, thanks.
     
  19. hampe-92

    hampe-92 Forum Addict

    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0
    Threads and FormClosing

    no no no, you are looking in the wrong place! here is a screenie:
    [​IMG]

    EDIT: Sorry, you are maybe using Express edition? it doesn't look exact the same :p
     
  20. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    Threads and FormClosing

    Can you scale the image next time? >_>
     
< i need to understand the basics | FAWG-Online >


 
 
Adblock breaks this site