Click here to Skip to main content
15,915,703 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear Friends,

Here i facing the Typical Problem in my Application iam using the Progress Bar Image that functionality iam using in another form when in Start button iam calling the functionality through Start Button

in start Button i written the code:

C#
thrd = new System.Threading.Thread(new System.Threading.ThreadStart(opendashboard1));
              thrd.IsBackground = true;
              thrd.Start();

Before executing the Functionality iam slepping this thread.
C#
if (signalsim == 1)
                                     {
                                         try
                                         {
                                             comm.Close();
                                             scrTemp.Start();

                                             Thread.Sleep(50000);
                                             scrTemp.Refresh();
                                             string str = scrTemp.Status.ToString();
                                             if (str == "StartPending")
                                             {
                                                 XtraMessageBox.Show("Gsm Modem is Not Connected");
                                                 
                                                     thrd.Abort();
                                               
                                                 Kill_UnwantedService();
                                                 this.Enabled = true;
                                                 picboxSMSService.Image = DEMIS_SERVER.Properties.Resources.new_close_30;
                                                 btnSMSService.Text = "START";
                                                 scrTemp.Refresh();
                                             }
                                             else if (str == "Running")
                                             {
                                                 XtraMessageBox.Show("SMS Service Connected Sucessfully..");
                                               
                                                
                                                     thrd.Abort();
                                                    
                                                
                                                 this.Enabled = true;
                                                 btnSMSService.Text = "STOP";
                                                 picboxSMSService.Image = DEMIS_SERVER.Properties.Resources.new_ok_30;
                                             }
                                             else if (str == "Stopped")
                                             {
                                                 XtraMessageBox.Show("Gsm Modem Device is Not Connected");
                                                
                                                     thrd.Abort();
                                                

                                                 this.Enabled = true;
                                                 btnSMSService.Text = "START";
                                                 picboxSMSService.Image = DEMIS_SERVER.Properties.Resources.new_close_30;

                                             }
                                         }
                                         catch (Exception ex)
                                         {

                                         }

                                     }

Here in thread.abort iam getting the Exception like some Unhandled exception is coming.I dont know why its coming.If any information please share with me.


Regards,

AnilKumar.D
Posted
Comments
[no name] 11-Sep-12 7:49am    
Knowing what the exception is, is really really helpful

You need to handle this error.
C#
try
{
thrd.Abort();
}
catch(Exception){}

///By the way, abort() is not a good practice to stop a thread// 
//Read msdn System.Threading documentation//
 
Share this answer
 
v2
Comments
Anil Honey 206 11-Sep-12 8:01am    
Can u give the exact method how to abort the thread like this situation.
Rock (Multithreaded) 11-Sep-12 8:04am    
Can you post your thread method [opendashboard1()].
Anil Honey 206 11-Sep-12 8:06am    
private void opendashboard1()
{
xf4 = new ProgressForm();
xf4.ShowDialog();
}
after this iam calling that form like this

thrd = new System.Threading.Thread(new System.Threading.ThreadStart(opendashboard1));
thrd.IsBackground = true;
thrd.Start();
Rock (Multithreaded) 11-Sep-12 8:08am    
Just a minute...
Rock (Multithreaded) 11-Sep-12 8:09am    
Are you using opendashboard1() method in a winForm?
Obviously, you'll get that error. because before aborting the thread you are not checking the current status of the thread whether the thread is alive or not.
Try this to abort the thread:
C#
//If the thread is alive
if(thrd.IsAlive)
{
    //Then abort the thread
    thrd.Abort();
}

View Threading Tutorial[^] for the reference.


--Amit
 
Share this answer
 
Comments
Anil Honey 206 11-Sep-12 8:12am    
Thanks i will check it in my application
//Remember, if you'r using a UI component(eg: button, window,textbox...) then, don't use directly UI component inside threading....

private void opendashboard1()
{
this.Invoke(
C#
(MethodInvoker)delegate()
           {
            xf4 = new ProgressForm();
            xf4.ShowDialog();
           });
        }


But tell me, Why you'r opening this Dialog inside a thread?
 
Share this answer
 
v2
Comments
Rock (Multithreaded) 11-Sep-12 8:32am    
If you only want to open a dialog, then don't use threading.
Anil Honey 206 12-Sep-12 0:04am    
actually i create dashboard for my application in that dashboard start button is there to run the windows service.The windows service will take time to activate mean while i blocked the dashboard because user not intract anything on the dashboard until the windows service start so i write the code this.Enabled = false; so after this i called the progress bar form and sleep the thread after the windows service starts i abort the thread.But this functionality works sometime.And some times it showing error.
Rock (Multithreaded) 12-Sep-12 1:43am    
Send me Complete dashboard window code...I will send you complete Code.
Anil Honey 206 13-Sep-12 2:52am    
thrd = new System.Threading.Thread(new System.Threading.ThreadStart(opendashboard1)); thrd.IsBackground = true; thrd.Start();
this is the code iam calling the progress bar window.
Rock (Multithreaded) 13-Sep-12 3:01am    
You need to Dispose() your ProgressForm before Abort thread.
xf4.Dispose();

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