Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
c# winform related

i have a form that loads a tabcontrol. the tab control load a usercontrol.
for each usercontrol it runs a threaded socket connection to a different server using the same usercontrol. all works fine till i want to stop the threads running in the usercontrols that are running.

i have a function setup to shut down the threads running. if i open only one tab and from form i can stop the thread and dispose of the tab. but if i load more then one tab with this same usercontrol and try shutting down the threads it seems to only shut down one of the threads from one of the userscontrol while the others are still running and leave the app running in the taskman.

any insight is would be helpful.

thanks

C#
this is the close method i have within the usercontrol
  public void CloseAllActions()
        {
            if (MyConnection.isConnected)
              {
                  this.Client.Abort();
                  Client.Join();
                  this.ServerDetails.Abort();
                  ServerDetails.Join();
                  MyConnection.CloseConnection();
              }
        }
from the main from this is what i have to run that method within that usercontrol
 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MYControl!= null)
            {
                e.Cancel = true;
                    MYControl.CloseAllActions();
                    MYControl.Dispose();
                    tab.Dispose();
                    Thread.Sleep(200);
                this.Dispose();

            }
            else
            {
                e.Cancel = false;
                // this.Dispose();
            }
        }
Posted
Updated 20-Feb-11 6:25am
v3
Comments
Dave Kreskowiak 20-Feb-11 12:12pm    
Without seeing your code that goes to each user control and tells it to stop, it's impossible to tell you what's going on.

You basically have to enumerate each tab in your tab control, go through their Controls collections, find each instance of your user control and tell them to stop by whatever method you've exposed.
Sergey Alexandrovich Kryukov 20-Feb-11 16:56pm    
Nevertheless, I already see crime in this code. Different problem is to explain that: this is apparently upside-down "design", should be thrown out in full...
--SA
Member 471644 20-Feb-11 21:48pm    
this is what did work. sakryukov most have you have said doesnt even relate to the question at hand, maybe try staying on topic. the join and sleep have nothing to do with the question and are there for some testing reasons. this code solved my problem as dave posted. i was already looking into this just thought there might be some other options. there still maybe some others but

which i call from the main form. all threads can be abort and the application closes like it should.

if (MYControl != null)
{
e.Cancel = true;
foreach (TabPage tab in this.tabControl1.TabPages)
{
foreach (MYControl MYTabControl in tab.Controls)
{
MYTabControl.CloseAllActions();
}
}
this.Dispose();
}
else
{
e.Cancel = false;
}
Dave Kreskowiak 21-Feb-11 7:43am    
He wasn't that far off-topic. He's correct in saying that your code needs a lot of work. Aborting threads is not a good idea. You're not giving your threaded code any chance to clean up resources before they terminate. This leaves you open to resource leaks that may be difficult to pin down.

The proper way to do it would be to set a flag that the thread code checks every once in a while to tell it to shutdown gracefully.

Nothing in this question make sense as the code shows something absolutely wrong in the very beginning.

The code shows some code fragment in UI thread, not the custom threads.
Anything in UI thread should not be blocking, even all more a less long operations should be eliminated. First, ServerDetails.Join() is a blocking call, it will not return until thread ServerDetails finished. How do you know when it finishes? The call Sleep(200) make no sense at all. What are you waiting for? How do you know that after 200 milliseconds anything changes?

You should through out this code completely, explain the purpose of your application and ask for possible design suggestions. What you're trying to do goes nowhere…

—SA
 
Share this answer
 
v2
Comments
Espen Harlinn 20-Feb-11 17:24pm    
Good points, my 5
Sergey Alexandrovich Kryukov 20-Feb-11 17:34pm    
Thank you.
--SA
Maybe you could try putting the thread abort calls into the destructor of the custom control, or somehow hook the parent form's Closing event.
 
Share this answer
 
C#
if (MYControl != null)
         {
             e.Cancel = true;
             foreach (TabPage tab in this.tabControl1.TabPages)
             {
                 foreach (MYControl MYTabControl in tab.Controls)
                 {
                     MYTabControl.CloseAllActions();
                 }
             }
             this.Dispose();
         }
         else
         {
             e.Cancel = false;
         }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900