Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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);
    }

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

C#
        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);
        }


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.
Posted
Updated 27-Sep-13 2:55am
v3
Comments
Sergey Alexandrovich Kryukov 27-Sep-13 9:43am    
Why would you do such things by the timer? That can be dangerous. Did you foresee the situations when the handler from works triggered by previous timer interrupt and execution is not complete, when next one happens? It may not happen for a while, but eventually your program goes slower, and it happens. The whole code design is too careless, so many bad things can happens.

And why, why then thread method is static? Big common misconception. This is a bad thing.

—SA
mahua22 28-Sep-13 1:58am    
my actual program will receive data from tcp client at 10 seconds interval.whenever data will receive,updateValues() will be called. I am creating its trial project using timer.the whole code is provided by the vendor. can you suggest me how do I edit it so that it runs smoothly?
Sergey Alexandrovich Kryukov 28-Sep-13 2:35am    
This is not how it is done. The event of getting data from the client is driven by the client, so you just need to read permanently in a separate thread.
—SA
mahua22 28-Sep-13 3:42am    
there is no problem with tcp part.it is running in a different thread. i am getting data from it & updating UI successfully. I have a variable which store data from tcp client. the problem is occurring in passing this data to that method which runs in a different thread.
Sergey Alexandrovich Kryukov 28-Sep-13 10:29am    
How do you know? Is some TCP communications works for a while, it does not mean there is no problem. I don't say the problem is TCP itself, the problem is race condition. Yes, passing data, but why it is a problem? I tried to explain that your approach is wrong in principle and why it shows some operation, for a while...
—SA

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900