在c#中的不同线程中传递值时获取stackoverflow异常 [英] getting stackoverflow exception while passing value in different thread in c#

查看:101
本文介绍了在c#中的不同线程中传递值时获取stackoverflow异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个带有第三方dll的opc服务器。他们给出了一个例子,其中所有函数都运行在不同的线程上。

这里是示例,OPCServer.cs:

I am creating an opc server with third party dll.they had given an example in which all functions r running on a different thread.
here is the example, 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()中遇到问题;

在主窗体中:




I am getting problem in the method UpdateValues();
in the main form:

        public Form1()
        {
            InitializeComponent();
            opcServer = OpcServer.CreateInstanceAsync();
            opcServer.UpdateValues(valuesInArray);
        }
there is a timer & the UpdateValues() method will call at every time tick with a new value. interval is 10 secs.

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





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



EDIT(V.Lorz) - 更改了代码块格式以便更好地阅读。



The program is running smoothly for some time. but after that it showing stack overflow exception.,sometimes pc got hanged.i do not understand why? how do i get rid from this? the OPCServer.cs is given by the 3rd party.my work is to passing value in that particular method.will i have to create a new thread each time i will call that method?

EDIT(V.Lorz)-changed the code block format for better reading.

推荐答案

请参阅我对该问题的评论。你试图做的是危险的,不能可靠地工作。我甚至不想讨论你的民意调查应该多么浪费,但你也遇到了所谓的竞争条件

http://en.wikipedia.org/wiki/Race_condition [ ^ ]。



以下是一般情况:连接的客户端尝试在其中发送数据块自己的步伐,你也尝试在关闭期间的计时器事件的处理程序中做一些事情。这两个事件序列以随机顺序一起跳动。即使客户只是一个...我不想分析可能的情况,显然它不能可靠地工作。事实上,一些应用程序在随机时间崩溃本身就是可能的竞争条件的良好迹象。



那么,该怎么办?首先,您需要在单独的线程中使用TCP通信。在一般情况下,服务器端还需要至少一个通信线程,即侦听新连接的通信线程,其必须与发送/接收数据并行完成。现在,发送或接收数据的 Socket TcpListener / TcpClient 方法是阻止;例如,读取方法正在等待数据传递。等待线程进入等待状态,浪费没有CPU时间,直到数据到达时系统唤醒。如果在您的场景中,某个客户端仅以自己的速度向侦听器发送数据,则侦听器部分应该只读取无限循环中的那些块。然后实际传输数据的实际时刻将由客户端部分决定。



当然,您将需要使用线程同步原语(在最简单的情况下) ,锁定语句)在这些线程和其他应用程序线程(UI,数据层等)之间进行通信。



请查看我的过去答案更详细:

插座编程中的业余问题 [< a href =http://www.codeproject.com/Answers/536542/anplusamateurplusquestionplusinplussocketplusprogr#answer2target =_ blanktitle =New Window> ^ ],

< a href =http://www.codeproject.com/Answers/165297/Multple-clients-from-same-port-Number#answer1>来自同一端口号码的多个客户端 [ ^ ]。



-SA
Please see my comments to the question. What you try to do is dangerous and cannot reliably work. I don't even want to discuss how wasteful your polling should be, but you also run into what is called race condition:
http://en.wikipedia.org/wiki/Race_condition[^].

Here is what generally happens: the connected client tries to send chunks of data in its own pace, and you also try to do something in a handler of the timer event with close period. These two sequences of events beats together in random order. Even if the client is only one… I don't want to analyze possible scenarios, it should be apparent that it cannot reliably work. In fact, crashing of some application at random time is itself a good indication of possible race condition.

So, what to do? First of all, you need to work with TCP communications in a separate threads. In general case, the server side also needs at least one more communication thread, the one which listens for new connection, which has to be done in parallel with sending/reveiving data. Now, the Socket or TcpListener/TcpClient methods sending or receiving data are blocking; for example, reading method is waiting until data is delivered. The waiting thread is put to the wait state wasting no CPU time until awaken by the system when data arrives. If, in your scenario, some client only sends data to the listener in its own pace, the listener part should just read those chunks in "infinite" loop. Then the actual moments of time when data is actually transferred will be dictated by the client part.

Of course, you will need to use thread synchronization primitives (in simplest case, lock statements) to communicate between those threads and other application threads (UI, data layer, etc.).

Please see my past answers for some more detail:
an amateur question in socket programming[^],
Multple clients from same port Number[^].

—SA


这篇关于在c#中的不同线程中传递值时获取stackoverflow异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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