【问题标题】:Error accessing ThreadStart object inside a function访问函数内的 ThreadStart 对象时出错
【发布时间】:2016-06-30 12:48:36
【问题描述】:

我遇到了一个特殊的问题,需要一些帮助来解决这个问题。

我有下面的代码

    public partial class FrmSoftJobProcess : Form
        {
            private bool InstanceFieldsInitialized = false;
            public FrmSoftJobProcess()
            {
                InitializeComponent();
                Load += FrmJobProcess_Load;
                FormClosing += FrmJobProcess_FormClosing;
                if (!InstanceFieldsInitialized)
                {
                    InitializeInstanceFields();
                    InstanceFieldsInitialized = true;
                }

            }
            private void InitializeInstanceFields()
            {
                PlotLensProfileThreadDelegate = new ThreadStart(PlotLensProfileThreadFunction);
                PlotLensProfileThread = new Thread(PlotLensProfileThreadDelegate);
                PlotLensPlanThreadDelegate = new ThreadStart(PlotLensPlanThreadFunction);
                PlotLensPlanThread = new Thread(PlotLensPlanThreadDelegate);
            }
            private ThreadStart PlotLensProfileThreadDelegate;
            private Thread PlotLensProfileThread;
            private ThreadStart PlotLensPlanThreadDelegate;
            private Thread PlotLensPlanThread;
-------------------
-----------------------
}

上面的类有如下功能

 private   void PlotLensProfileThreadFunction()
        {
            Mold_Power_Suite.Model.FrontEndStructures.ErrorFlagType ErrorFlag =FrontEndStructures. InitErrorFlag();
            Graphics InMedia = this.PicLensPlot.CreateGraphics();

           PlotLensProfileThread PlotRef = new PlotLensProfileThread(); // how can I access this? I cant create object as its a field. 
           // var PlotRef = PlotLensProfileThread;
            try
            {
                PlotRef.ThreadInGrphRef = InMedia;
                PlotRef.ThreadInConcavePaths = ConcavePaths;
                PlotRef.ThreadInConvexPaths = ConvexPaths;
                PlotRef.ThreadPlotOptions = PlotOptions.ProfileView;
                PlotRef.ThreadStepSixData = JobData.StepSixData;
                PlotRef.ThreadStepFiveData = JobData.StepFiveData;
                PlotRef.ThreadStepFourData = JobData.StepFourData;
                PlotRef.ThreadStepThreeData = JobData.StepThreeData;
                PlotRef.ThreadStepTwoData = JobData.StepTwoData;
                PlotRef.ErrorFlag = ErrorFlag;
                PlotRef.MeridianConcave = MeridianConcave;
                PlotRef.MeridianEdge = MeridianEdge;
                PlotRef.MeridianConvex = MeridianConvex;
                ZedGraphControl1.GraphPane.CurveList.Clear();
                PlotRef.zedGraphType = ZedGraphControl1;
                PlotRef.PlotLensProfile();
            }
            catch (System.Threading.ThreadAbortException e)
            {
                System.Threading.Thread.ResetAbort();
            }
        }

整个源代码在 VB 中,并已使用 Telerik Online Tool 转换为 C#。

我在VB中添加原始方法以便更好地理解

Private Sub PlotLensPlanThreadFunction()
        Dim ErrorFlag As ErrorFlagType = InitErrorFlag()
        Dim InMedia As Graphics = Me.PicLensPlot.CreateGraphics
        Dim PlotRef As New PlotLensPlanThread()
        Try
            PlotRef.ThreadInGrphRef = InMedia
            PlotRef.ThreadInConcavePaths = ConcavePaths
            PlotRef.ThreadInConvexPaths = ConvexPaths
            PlotRef.ThreadPlotOptions = PlotOptions.PlanView
            PlotRef.ThreadStepSixData = JobData.StepSixData
            PlotRef.ThreadStepFourData = JobData.StepFourData
            PlotRef.ErrorFlag = ErrorFlag
            PlotRef.MeridianConcave = MeridianConcave
            PlotRef.MeridianEdge = MeridianEdge
            PlotRef.MeridianConvex = MeridianConvex
            PlotRef.PlotLensPlan()
        Catch ex As Exception
            System.Threading.Thread.ResetAbort()
        End Try
    End Sub

我在 C# 语句中遇到错误 PlotLensProfileThread PlotRef = new PlotLensProfileThread(); 错误是 PlotLensProfileThread' 是一个“字段”,但用作“类型”

谁能帮帮我?

谢谢

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    您在上述行 PlotLensProfileThread PlotRef = new PlotLensProfileThread(); 中遇到错误,因为在您的类中您已经定义了一个同名的线程实例,如下所示

    Thread PlotLensProfileThread = new Thread(new FrmSoftJobProcess(), ....
    

    在您的方法中,PlotLensProfileThread 是自定义类还是什么?无论如何,您应该为您的线程或您的类型使用不同的名称。

    【讨论】:

    • PlotLensProfileThread 是用于创建 ThreadClass 对象的变量,请看代码
    • @Apoorv,是的,看到了,所以回答了。问题是您已经使用该名称定义了一个线程实例并尝试在您的方法中使用相同的名称
    • 如何使用 PlotRef?该函数使用 PlotRef 并且我知道 PlotLensProfileThread 是一个变量。无法创建变量的对象。
    • @Apoorv,除非你告诉我PlotLensProfileThread 实际上是什么,否则不能说?您是否尝试访问您在方法主体(或)外部声明的线程实例,它是您定义的自定义类吗?
    猜你喜欢
    • 2018-01-09
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多