C# 在c中的不同线程中传递值时获取stackoverflow异常#

C# 在c中的不同线程中传递值时获取stackoverflow异常#,c#,multithreading,stack-overflow,C#,Multithreading,Stack Overflow,我正在用第三方dll创建一个opc服务器。他们给出了一个例子,其中所有函数r都运行在不同的线程上。 以下是OPCServer.cs示例: public static OpcServer CreateInstanceAsync() { Thread thread = new Thread(new ParameterizedThreadStart(InitializationThread)); OpcServer opcServer = new OpcServ

我正在用第三方dll创建一个opc服务器。他们给出了一个例子,其中所有函数r都运行在不同的线程上。 以下是OPCServer.cs示例:

public static OpcServer CreateInstanceAsync()
    {
        Thread thread = new Thread(new ParameterizedThreadStart(InitializationThread));
        OpcServer opcServer = new OpcServer();
        thread.Start(opcServer);
        thread.Join();
        return opcServer;
    }
 static void InitializationThread(object arg)
    {
        ((OpcServer)arg).Initialize();
    }

    void Initialize()
    {
         //some stuff
    }

  public void UpdateValues(string[] n)
    {
    this.BeginUpdate();
    value1 = (object[])n;
    for (int i = 0; i < tag_count; i++)
    {
       this.SetTag(tag_ids[i], value1[i], Quality.Good, FileTime.UtcNow);
    }
   this.EndUpdate(false);
    }
有一个计时器&UpdateValues()方法将在每次使用新值勾选时调用。间隔为10秒

     private void timer1_Tick(object sender, EventArgs e)
    {
        opcServer.UpdateValues(valuesInArray);
    }

程序运行平稳一段时间。但在那之后,它显示堆栈溢出异常。有时电脑被挂了。我不明白为什么?我怎样才能摆脱这个?OPCServer.cs由第三方提供。我的工作是在该特定方法中传递值。每次调用该方法时是否必须创建一个新线程?

请尝试BackgroundWorker在运行长进程时更新表单。使用ProgressChanged事件更新表单值,否则调用委托更新表单控件。

另一种方法是使用,然后使用事件和委托与表单元素交互

使用任务并行库非常简单:

foreach (DriveInfo info in DriveInfo.GetDrives())
{
    if (info.DriveType == DriveType.Fixed)
        {
             var task = Task.Factory.StartNew(() => scanFiles(findType, info.RootDirectory.Name));                        
        }
}
这是一个与表单元素交互的示例:

在我的对外课上:

/// <summary>
        /// Delegate for setting text box text
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public delegate void TextBoxEventHandler(object sender, TextEventArgs e);
        /// <summary>
        /// Event for changing tool bar text
        /// </summary>
        public event TextBoxEventHandler ChangeTextBoxText = delegate { };
        /// <summary>
        /// Function that raises set tool bar text event
        /// </summary>
        /// <param name="s"></param>
        public void SetTextBoxText(string s)
        {
            ChangeTextBoxText(this, new TextEventArgs(s));
        }

首先,为什么要在这里创建线程

public static OpcServer CreateInstanceAsync()
{
    Thread thread = new Thread(new ParameterizedThreadStart(InitializationThread));
    OpcServer opcServer = new OpcServer();
    thread.Start(opcServer);
    thread.Join();
    return opcServer;
}
因为我可能认为,在获得OpcServer对象后,您不想挂起主窗体创建,您只是在使用同一个实例在计时器中调用UpdateValues()

现在,当你在这个电话里堆积东西的时候。您正在添加的更新数量

this.SetTag(tag_ids[i], value1[i], Quality.Good, FileTime.UtcNow);
必须有某种方法来删除旧/过时的标记


查看API文档以释放对象

我不必更新表单。我必须将表单中变量的值传递给方法,该方法假定在不同的线程中以计时器间隔运行。1.)什么是OPC服务器?2.)这是什么意思。BeginUpdate()?3.)这个.SetTag(..)是什么意思?4.)这是什么。EndUpdate(false)?OPC服务器是一个充当协议转换器的软件应用程序。它创建标记&所有这些方法都是由第三方dll提供的,用于更新标记。SetTag()有4个参数。我的工作是继续将数据传递到updateTags()。它在一段时间内工作正常,然后显示错误。您可以设置断点吗“返回opcServer;“插入CreateInstanceSync方法并检查它是否调用?@mt_serg:我在返回时给出了断点,它返回的是它拥有的所有静态成员,但不是该方法。我试图理解的是为什么使用线程初始化opcServer。看起来没有任何意义,因为在启动后我们立即调用Join并等待(阻止调用线程)直到InitializationThread方法完成。因此,首先,为什么不像这样创建实例:public static OpcServer CreateInstance(){var OpcServer=new OpcServer();OpcServer.Initialize();return OpcServer;}
public static OpcServer CreateInstanceAsync()
{
    Thread thread = new Thread(new ParameterizedThreadStart(InitializationThread));
    OpcServer opcServer = new OpcServer();
    thread.Start(opcServer);
    thread.Join();
    return opcServer;
}
this.SetTag(tag_ids[i], value1[i], Quality.Good, FileTime.UtcNow);