Threads and FormClosing

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

Threads and FormClosing
  1. Unread #1 - Dec 18, 2008 at 11:36 PM
  2. Supah Fly
    Joined:
    Aug 22, 2007
    Posts:
    202
    Referrals:
    1
    Sythe Gold:
    0

    Supah Fly Active Member
    Banned

    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.
     
  3. Unread #2 - Dec 19, 2008 at 9:26 AM
  4. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    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/
     
  5. Unread #3 - Dec 19, 2008 at 4:12 PM
  6. Supah Fly
    Joined:
    Aug 22, 2007
    Posts:
    202
    Referrals:
    1
    Sythe Gold:
    0

    Supah Fly Active Member
    Banned

    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.
     
  7. Unread #4 - Dec 20, 2008 at 4:54 AM
  8. hampe-92
    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0

    hampe-92 Forum Addict

    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?
     
  9. Unread #5 - Dec 20, 2008 at 5:15 AM
  10. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    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#.
     
  11. Unread #6 - Dec 20, 2008 at 2:43 PM
  12. Supah Fly
    Joined:
    Aug 22, 2007
    Posts:
    202
    Referrals:
    1
    Sythe Gold:
    0

    Supah Fly Active Member
    Banned

    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...
    }
     
  13. Unread #7 - Dec 20, 2008 at 3:22 PM
  14. hampe-92
    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0

    hampe-92 Forum Addict

    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();
    }
     
  15. Unread #8 - Dec 20, 2008 at 5:00 PM
  16. Supah Fly
    Joined:
    Aug 22, 2007
    Posts:
    202
    Referrals:
    1
    Sythe Gold:
    0

    Supah Fly Active Member
    Banned

    Threads and FormClosing

    it just ignores the fact that its there.. i dont get it..
     
  17. Unread #9 - Dec 20, 2008 at 8:41 PM
  18. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    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.
     
  19. Unread #10 - Dec 20, 2008 at 11:28 PM
  20. Supah Fly
    Joined:
    Aug 22, 2007
    Posts:
    202
    Referrals:
    1
    Sythe Gold:
    0

    Supah Fly Active Member
    Banned

    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.
     
  21. Unread #11 - Dec 21, 2008 at 4:10 AM
  22. hampe-92
    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0

    hampe-92 Forum Addict

    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
     
  23. Unread #12 - Dec 21, 2008 at 5:16 AM
  24. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    Threads and FormClosing

    What are you talking about? That code IS Windows Forms.
     
  25. Unread #13 - Dec 21, 2008 at 6:01 AM
  26. hampe-92
    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0

    hampe-92 Forum Addict

    Threads and FormClosing

    I think he got confused by "Console.WriteLine()":p
     
  27. Unread #14 - Dec 21, 2008 at 8:07 AM
  28. x&#8313;
    Referrals:
    1

    x&#8313; Guest

    Threads and FormClosing

    Replace the call to Console.WriteLine() with MessageBox.Show(), then.
    It just may be slightly more annoying to run.
     
  29. Unread #15 - Dec 21, 2008 at 1:02 PM
  30. hampe-92
    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0

    hampe-92 Forum Addict

    Threads and FormClosing

    ah, I hoped he could figure that out by him self :laugh:
     
  31. Unread #16 - Dec 21, 2008 at 3:45 PM
  32. Supah Fly
    Joined:
    Aug 22, 2007
    Posts:
    202
    Referrals:
    1
    Sythe Gold:
    0

    Supah Fly Active Member
    Banned

    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.
     
  33. Unread #17 - Dec 21, 2008 at 4:47 PM
  34. hampe-92
    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0

    hampe-92 Forum Addict

    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
     
  35. Unread #18 - Dec 21, 2008 at 5:05 PM
  36. Supah Fly
    Joined:
    Aug 22, 2007
    Posts:
    202
    Referrals:
    1
    Sythe Gold:
    0

    Supah Fly Active Member
    Banned

    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.
     
  37. Unread #19 - Dec 21, 2008 at 6:26 PM
  38. hampe-92
    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0

    hampe-92 Forum Addict

    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
     
  39. Unread #20 - Dec 21, 2008 at 8:30 PM
  40. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    Threads and FormClosing

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

Users viewing this thread
1 guest


 
 
Adblock breaks this site