如何解决我的应用程序中的线程问题 [英] How to Resolve Thread Problem in My application

查看:77
本文介绍了如何解决我的应用程序中的线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,

在这里,我使用进度条图像来面对应用程序中的典型问题,即在开始按钮中iam以另一种形式使用的功能iam通过开始按钮调用该功能

在开始按钮中,我编写了代码:

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:

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


在执行功能性iam之前,请将该线程休眠.


Before executing the Functionality iam slepping this thread.

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)
                                         {

                                         }

                                     }


在thread.abort iam中,像一些未处理的异常一样,我正在获取异常.我不知道它为什么会出现.如果有任何信息,请与我分享.


问候,

AnilKumar.D


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

推荐答案

您需要处理此错误.
You need to handle this error.
try
{
thrd.Abort();
}
catch(Exception){}

///By the way, abort() is not a good practice to stop a thread// 
//Read msdn System.Threading documentation//


很显然,您会收到该错误.因为在中止线程之前,您不会检查线程的当前状态,线程是否处于活动状态.
尝试以下方法终止线程:
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:
//If the thread is alive
if(thrd.IsAlive)
{
    //Then abort the thread
    thrd.Abort();
}


查看线程教程 [


View Threading Tutorial[^] for the reference.


--Amit


//记住,如果您正在使用UI组件(例如:按钮,窗口,文本框...),则不要在内部直接使用UI组件穿线......

私人void opendashboard1()
{
this.Invoke(
//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(
(MethodInvoker)delegate()
           {
            xf4 = new ProgressForm();
            xf4.ShowDialog();
           });
        }



但是告诉我,为什么要在线程内打开此对话框?



But tell me, Why you''r opening this Dialog inside a thread?


这篇关于如何解决我的应用程序中的线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆