Bart88
Bart88

Reputation: 163

asp.net and multi threading and C# object

I'm new to ASP.net programming and I don't really understand the thing about multi threading . When I debug in my ASP.net app/website it shows a tab that I'm not familiar with: Parallel watch, it's something about multi threading. When I search about multi threading I only find examples about how to use multi-threading etc. The thing is, I'm not using any class for threading.

What I got in my code:

is my main page: Index.aspx.cs:

 protected void Button1_Click(object sender, EventArgs e)
{
    string Message = PLC1.Connect();

    lblClock.Text = Message;    
}

and

protected void Timer1_Tick(object sender, EventArgs e)
{
    if(PLC1.x_ConnectionEstablished)
    {
        string Message;

        Message = PLC1.ReadDB(1, 0, 17);

        if (Message == "OK")
        {
            lblClock.Text = "Connected and retrieved value!!";
        }
        else
        {
            lblClock.Text = Message;
        }
    }
    else
    {
        lblClock.Text = "No connection! Press the button!";
    }      
}

my class: PLC.cs:

public string Connect()
{
    int Result;
    string Message;

    Result = s7_S7Client.ConnectTo(s_IP, i_Rack, i_Slot);
    Message = s7_S7Client.ErrorText(Result);

    x_ConnectionEstablished = s7_S7Client.Connected();

    return Message;
}

public string ReadDB(int a_DBnr, int a_startPos, int a_size)
{
    int Result;
    string Message = "";

    try
    {
        Result = s7_S7Client.DBRead(a_DBnr, a_startPos, a_size, bArray_Buffer);
        Message = s7_S7Client.ErrorText(Result);
    }
    catch (Exception E)
    {
        Message = E.Message;
    }

    return Message;
}

When I press the button (while debugging) it jumps to the Connect() of my PLC class. And it actually gives true on the x_ConnectionEstablished. But when my Timer triggers it doesn't go into my if-statement. Since it's saying: x_ConnectionEstablished = false.

I can add the code to the timer, but I don't want to execute the connection method each time I get in the timer(that's what I'm using this x_connectionEstablished for.

So my question is how does this threading work and how can I get my website running decently with or without the threading?

Upvotes: 0

Views: 40

Answers (1)

CodeCaster
CodeCaster

Reputation: 151720

Your PLC1 and s7_S7Client appear to create some kind of persistent connection, for example through TCP sockets or a serial connection.

You don't want to create connections like this from an HTTP back-end, because HTTP is (or at least supposed to be) stateless: each request starts with a clean slate, all your variables (and thus connections) from the previous request are gone.

So I would advise wrapping this logic into a Windows Service that manages connecting to the PLC, and exposing the logic from this service through WCF.

Then your web application can issue requests, through WCF, to the service, and the service in turn talks to the PLC and maintains the connection.

Upvotes: 1

Related Questions