C#多线程操作由表单触发

发布于 2025-02-06 19:52:48 字数 2712 浏览 1 评论 0原文

美好的一天,

在我的Google& stackoverflow查询(不确定我应该搜索什么),我在这里发布了一个公共问题。

我有一个主要形式(frmmainmenu)。每当加载此表格并按下此表格上的按钮时,我都在尝试更新图表。但是,由于这可能是CPU要求,因此我正在考虑调用该操作以从另一个线程中检索图表。

我经常遇到一个错误:跨线程操作无效:从其创建的线程以外的线程中访问的'labelx2'。 system.windows.forms在system.windows.forms.control.get_handle()

背后的想法也是显示此符号(被引用为CignularProgress1 Control 每当检索数据时, 操作完成时,请隐藏

在以下

[...]

private void feedDashboard()
{

Thread l_Thread;
ClsStartPgm l_Start = null;
try
{
  this.Invoke(new MethodInvoker(delegate()
  {
    l_Thread = new Thread(() =>
    {
      try
      {
        l_Start = new ClsStartPgm();
        l_Start.getDashboardInformations(
        this.labelX2, 
        this.circularProgress1, 
        this.tmr_Progress,
        this.chart1, 
        this.expandablePanel_1);
      }
    finally
    {
      // onCompleted();
    }
  });
  
  l_Thread.Start();
  }));
}
catch (Exception exc)
{
  ClsFinappErrorManager.manageException(exc);
}

finally
{
}
}
[...]

public sealed class ClsStartPgm
{

[...] 
// (Constructors, accessors and other methods not mentioned here 
// to simplify the reading of my question)
[...] 

    public void getDashboardInformations(
    LabelX pInformationText, 
    Controls.CircularProgress pCircularProgress, 
    System.Windows.Forms.Timer pTimer,
    Chart pChart1, 
    ExpandablePanel pExpandablePanel1)
    {
        List<double> l_DoubleList = null;
        List<string> l_StringList = null;

        try
        {
            pTimer.Start();
            
            this.m_Busy_Dashboard_Generation = true;

            this.m_Busy_Dashboard_Generation = false;
            
            double[] yValues = l_DoubleList.ToArray();
            string[] xValues = l_StringList.ToArray();

            pChart1.Series["MySerie"].Points.Clear();

            // Populate series data
            pChart1.Series["MySerie"].Points.DataBindXY(xValues, yValues);

            // Set Doughnut chart type
            pChart1.Series["MySerie"].ChartType = SeriesChartType.Pie;
            
            // Set title of the expendable panel
            pExpandablePanel1.TitleText = "Situation: " + DateTime.Now.ToShortDateString();

            [...]
        }
        catch (Exception exc)
        {
            ClsFinappErrorManager.manageException(exc);
        }

        finally
        {
            l_Tuple = null;
            l_Accounts = null;
        }
    }
}

它 我肯定不要求我写代码

Good day,

After being unsuccessful in my Google & Stackoverflow queries (not sure what I should search for), I'm posting here a public question.

I have a main form (frmMainMenu). Whenever this form is loaded and a button on this form is pressed, I'm trying to update a chart. However, as this could be CPU-demanding, I was considering calling the operation to retrieve the chart from another thread.

I'm constantly getting an error : Cross-thread operation not valid: Control 'labelX2' accessed from a thread other than the thread it was created on. System.Windows.Forms at System.Windows.Forms.Control.get_Handle()

The idea behind is also to display this symbol (referenced as the circularProgress1 control enter image description herewhenever data is being retrieved and to hide it whenever the operation is complete.

Here below is the code of my method in the userform :

[...]

private void feedDashboard()
{

Thread l_Thread;
ClsStartPgm l_Start = null;
try
{
  this.Invoke(new MethodInvoker(delegate()
  {
    l_Thread = new Thread(() =>
    {
      try
      {
        l_Start = new ClsStartPgm();
        l_Start.getDashboardInformations(
        this.labelX2, 
        this.circularProgress1, 
        this.tmr_Progress,
        this.chart1, 
        this.expandablePanel_1);
      }
    finally
    {
      // onCompleted();
    }
  });
  
  l_Thread.Start();
  }));
}
catch (Exception exc)
{
  ClsFinappErrorManager.manageException(exc);
}

finally
{
}
}
[...]

and if it can be helpful, here is my mentioned getDashboardInformations method of my Class ClsStartPgm

public sealed class ClsStartPgm
{

[...] 
// (Constructors, accessors and other methods not mentioned here 
// to simplify the reading of my question)
[...] 

    public void getDashboardInformations(
    LabelX pInformationText, 
    Controls.CircularProgress pCircularProgress, 
    System.Windows.Forms.Timer pTimer,
    Chart pChart1, 
    ExpandablePanel pExpandablePanel1)
    {
        List<double> l_DoubleList = null;
        List<string> l_StringList = null;

        try
        {
            pTimer.Start();
            
            this.m_Busy_Dashboard_Generation = true;

            this.m_Busy_Dashboard_Generation = false;
            
            double[] yValues = l_DoubleList.ToArray();
            string[] xValues = l_StringList.ToArray();

            pChart1.Series["MySerie"].Points.Clear();

            // Populate series data
            pChart1.Series["MySerie"].Points.DataBindXY(xValues, yValues);

            // Set Doughnut chart type
            pChart1.Series["MySerie"].ChartType = SeriesChartType.Pie;
            
            // Set title of the expendable panel
            pExpandablePanel1.TitleText = "Situation: " + DateTime.Now.ToShortDateString();

            [...]
        }
        catch (Exception exc)
        {
            ClsFinappErrorManager.manageException(exc);
        }

        finally
        {
            l_Tuple = null;
            l_Accounts = null;
        }
    }
}

Please, could anyone guide me on what's wrong in my code? I'm definitely not asking to get the code written for me. However, I would be keen on understanding what I'm doing incorrectly here in my approach.

Many thanks for your appreciated help and best wishes,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文