• Log in or sign up for an account here: https://www.sythe.org/login

Threads and FormClosing

Supah Fly

Active Member

Banned
Joined
Aug 22, 2007
Sythe Gold
0
Messages
202
Location
your face
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.
 
also i need a formclosing method... i dont know why mine isnt working -.- ive tried everything... even googling and trying those.
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/
 
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?
 
Member of the Month Winner
Sythe's 10th Anniversary
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.
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#.
 
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...
}
 
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();
}
 
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();
}

it just ignores the fact that its there.. i dont get it..
 
Member of the Month Winner
Sythe's 10th Anniversary
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.
 
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.
 
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
 
Member of the Month Winner
Sythe's 10th Anniversary
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.
What are you talking about? That code IS Windows Forms.
 
Replace the call to Console.WriteLine() with MessageBox.Show(), then.
It just may be slightly more annoying to run.
 
x&#8313;;4205590 said:
Replace the call to Console.WriteLine() with MessageBox.Show(), then.
It just may be slightly more annoying to run.
ah, I hoped he could figure that out by him self :laugh:
 
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

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

I think he got confused by "Console.WriteLine()":p

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.
 
Only visual basic has that for me. Not on C# or C++.
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
 
C#:
e0n2mc.png


VB.NET:
34t6gwl.png


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.
 
C#:
e0n2mc.png


VB.NET:
34t6gwl.png


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.

no no no, you are looking in the wrong place! here is a screenie:
ux1lk.jpg


EDIT: Sorry, you are maybe using Express edition? it doesn't look exact the same :p
 
Member of the Month Winner
Sythe's 10th Anniversary
Can you scale the image next time? >_>
 
Runescape Gamblingrunescape gambling
Back
Top