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?
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.
Related
I want to show popup window when a connection is lost or back for that I am us below code
using System.Net.NetworkInformation;
using Tulpep.NotificationWindow;
public TaskList()
{
InitializeComponent();
NetworkChange.NetworkAvailabilityChanged += AvailabilityChanged;
load();
}
private void AvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
PopupNotifier popup = new PopupNotifier();
if (e.IsAvailable)
{
popup.ContentText = "Network connected!";
popup.Popup();
}
else
{
popup.ContentText = "Network disconnected!";
popup.Popup();
}
}
But it cannot show a notification. I also apply to debug but it is not hit the debugger what is wrong here I don't know please help me in my code
One option would be to use the Ping method periodically. That call will return a result, and when it fails that might indicate you've disconnected. However, this does rely on you having a reliable IP (or url) to use.
The basic test goes like this:
var reliableIP = "127.0.0.1";
var stillConnected = new System.Net.NetworkInformation.Ping().Send(reliableIP, 500).Status == System.Net.NetworkInformation.IPStatus.Success;
How you use that will depend upon the kind of monitoring you want.
I have a very simple WCF service:
[ServiceContract]
public interface IEngine
{
#region Test code
// test - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[OperationContract]
string Test();
}
implemented as follows:
public partial class Engine : IEngine
{
private static int nTestCount = 0;
string IEngine.Test()
{
try
{
nTestCount++;
}
catch (Exception)
{
}
return "Service OK " + nTestCount.ToString();
}
}
When I call the test service method 10 times, in about 10 seconds... I get this error:
The request channel timed out while waiting for a reply after 00:01:00. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout.
( I tried to put in my web.config file but it doesn't show up here )
( help on posting the contents would be appreciated )
Update 1:
Here is the client code that invokes the service:
private void btn_Test_Click(object sender, EventArgs e)
{
ServiceReference1.EngineClient eng = new EngineClient();
textBox1.Text = eng.Test();
}
You are not closing your connections when you are done with them, this is causing the server to tie up a connection until the garbage collector collects your object. The default max open sessions a server can handle is 10 for .net 3.5 and older (it was raised to 100 * ProcessorCount in .NET 4).
Dispose of the engine and it should work fine.
private void btn_Test_Click(object sender, EventArgs e)
{
using(ServiceReference1.EngineClient eng = new EngineClient())
{
textBox1.Text = eng.Test();
}
}
You aren't closing your client connections after each use which after a certain period causes the service to run out of available connections. Once the service reaches its max open connections, new connections will timeout waiting for a connect to clear up.
You need to close your connections when you're done with them by calling their Dispose() or wrapping them in using blocks
private void btn_Test_Click(object sender, EventArgs e)
{
using (ServiceReference1.EngineClient eng = new EngineClient())
{
textBox1.Text = eng.Test();
}
}
I am working on a window services application and my window service will call one of the web services in certain intervals (for example 3 min). From the web service I will get data from a database and using that data I will send an email.
If I am having huge sets of rows in my db table it will take some time to send the mail. Here I have the problem: The window services send the first request and it will handle some set of records. So, while processing it by the web service, the window service sends another request to the web service before it has completed the first request.
Due to this, the web service gets the same records from db again and again whenever it receives a new request from the windows service.
Can any one suggest me how to lock the previous request until it completes its work or some other way to handle this situation?
Web Service call:
protected override void OnStart(string[] args)
{
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = 180000;
timer.AutoReset = false;
timer.Enabled = true;
}
Inside Method
using (MailWebService call = new MailWebService())
{
try
{
call.ServiceUrl = GetWebServiceUrl();
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
call.CheckMailQueue();
}
catch (Exception ex)
{
LogHelper.LogWriter(ex);
}
finally
{
}
}
The Monitor class works great for this scenario. Here is an example of how to use it:
// This is the object that we lock to control access
private static object _intervalSync = new object();
private void OnElapsedTime(object sender, ElapsedEventArgs e)
{
if (System.Threading.Monitor.TryEnter(_intervalSync))
{
try
{
// Your code here
}
finally
{
// Make sure Exit is always called
System.Threading.Monitor.Exit(_intervalSync);
}
}
else
{
//Previous interval is still in progress.
}
}
There is also an overload for TryEnter that allows you to specify timeout for entering the section.
I'm trying to set up a specific scenario but, obviously, I'm having problems. My server is a site that primarily hosts a WCF service but I want to add an XSockets host there as well. I have the standard code in the bootstrap code file as per the instructions in the readme.txt. Upon a client connection, I am starting a worker thread which is basically a heartbeat that the client will monitor. The relevant code from the controller is as follows:
public class HeartbeatController : XSocketController
{
public void AddMessage(string message)
{
this.SendToAll(message, "addMessage");
}
}
Within my worker thread I am calling this:
string message = String.Format("pump", Math.Round(cpuCounter.NextValue());
ClientPool connection = ClientPool.GetInstance("ws://mywebsite:4502/HeartbeatController", "*");
connection.Send(message, "addMessage");
Currently I'm testing this with a console client which looks like this:
class Program
{
static XSocketClient socketClient;
static void Main(string[] args)
{
Console.WriteLine("Starting client...");
string url = "ws://mywebsite:4502/HeartbeatController";
socketClient = new XSocketClient(url, "*");
socketClient.OnOpen += socketClient_OnOpen;
socketClient.Open();
while (true)
{
// let it sit and display the "pump" messages
string input = Console.ReadLine();
if (input.Equals("Q", StringComparison.Ordinal))
{
break;
}
}
}
static void socketClient_OnOpen(object sender, EventArgs e)
{
Console.WriteLine("socketClient Opened");
socketClient.Bind("addMessage", OnAddMessage);
}
private static void OnAddMessage(ITextArgs textArgs)
{
Console.WriteLine("AddMessage :: {0}", textArgs.data);
}
}
On the client, if I put a breakpoint in the socketClient_OnOpen method it gets hit so I think it is connecting. But the pump message never makes it to the client.
Two Questions:
Is there anything obvious that I'm missing?
(Unrelated) Since many enterprises really don't like punching holes in their firewalls, is there any way to use port 80 with this setup (so that the client connection would look like "ws://mywebsite/HeartbeatController")?
Thanks for any help!
So to see what your pump actually was sending in to the server I added a custom pipeline.
public class MyPipeline : XSocketPipeline
{
//Incomming textmessage
public override void OnMessage(IXSocketController controller, ITextArgs e)
{
Console.WriteLine("IN " + e.data);
//Let the message continue into the server
base.OnMessage(controller, e);
}
//Outgoing textmessage
public override ITextArgs OnSend(IXSocketProtocol protocol, ITextArgs e)
{
Console.WriteLine("OUT " + e.data);
return base.OnSend(protocol, e);
}
}
Since I then saw that you was sending in a string that actually did not have a property named "message". The actionmethod "AddMessage" expects you to pass in a property message of type string. So you can solve this in two ways, both of them are simple.
Just replace the string parameter in the AddMessage with ITextArgs
public void AddMessage(ITextArgs message)
or...
Pass in a object from your worker thread instead of a string like this
connection.Send(new {message}, "addMessage");
So all you need to do to get it to work is to change this row
connection.Send(message, "addMessage");
with this row
connection.Send(new {message}, "addMessage");
EDIT: Btw, 4.0 is on the way and the client will be very much improved as well as the serverside stuff.
I'm trying to send confirmation mails to users periodically in ASP.NET.
To do this I polulate a queue with mails and check it every 30 seconds. Any confirmation emails in the queue at this time are sent and then cleared from the queue.
Does anyone know how to do this?
Here is my sending mail code
public static bool SendMail(string AdminMail,string AdminPassword,string subject,string toAddress, string content,DateTime SendTime)
{
toAddressListProperty.Enqueue(toAddress);
if(date==null)
{
date = DateTime.Now.Second;
}
if (date-SendTime.Second > 120)
{
var message = new MailMessage
{
From = new MailAddress(AdminMail)
};
foreach (var toAddressl in toAddressListProperty)
{
message.To.Add(new MailAddress(toAddressl));
}
message.Subject = subject;
message.Body = content;
message.IsBodyHtml = true;
var smtp = new SmtpClient
{
Credentials = new System.Net.NetworkCredential(AdminMail, AdminPassword),
Port = 587,
Host = "smtp.gmail.com",
EnableSsl = true
};
smtp.Send(message);
//date = SendTime;
return true;
}
return false;
}
I have done this using a background thread. I did a little research, and I believe this is an ok approach. There are a few dangers, which this blog details.
The main thing is to ensure you never throw an exception from a background thread, as I believe that will cause the web process to restart. Also, incase the thread dies, I ensure it is running on every call.
I have been using this approach for a few months, and so far no issues.
Also I run it every 1 second, this minamizes the amount of time you might loose emails due to an app shutdown.
public class BackgroundSmtpService
{
private ILog _log = LogManager.GetLogger(typeof(BackgroundSmtpService));
private readonly SmtpService SmtpService;
private static Thread _watchThread;
private static List<Email> _emailToSend = new List<Email>();
public BackgroundSmtpService(SmtpService smtpService)
{
SmtpService = smtpService;
}
public void Send(Email email)
{
lock (_emailToSend)
{
_emailToSend.Add(email);
}
EnsureRunning();
}
private void EnsureRunning()
{
if (_watchThread == null || !_watchThread.IsAlive)
{
lock (SmtpService)
{
if (_watchThread == null || !_watchThread.IsAlive)
{
_watchThread = new Thread(ThreadStart);
_watchThread.Start();
}
}
}
}
private void ThreadStart()
{
try
{
while (true)
{
Thread.Sleep(1000);
try
{
lock (_emailToSend)
{
var emails = _emailToSend;
_emailToSend = new List<Email>();
emails.AsParallel().ForAll(a=>SmtpService.Send(a));
}
}
catch (Exception e)
{
_log.Error("Error during running send emails", e);
}
}
}
catch (Exception e)
{
_log.Error("Error during running send emails, outer", e);
}
}
}
You might want to consider using Quartz.net library. It have decent documentation and it's fairly easy to use.
The biggest challenge you'll have with this is that any time your application pool recycles it will take a new request to kick stats your "timer". If you had an HTTP monitor application such as Pingdom to poll your server it shouldn't be a problem, but then again you could also just use a third party monitor tool to hit your a page on your site every N seconds that would send out the mail and issue a response.
I myself would use a Windows service to pull a queue from a database and send out messages that way.
Easiest way is to create a VBScript that sends an HTTP GET request to http://localhost/SendConfirmationEmails.aspx
You'd start the VBScript in your global.asax Application_Start method.
The SendConfirmationEmails.aspx would act as a simple web service (you could use an ashx, or actual web service asmx if you wanted). It would only be accessible on the localhost so remote users wouldn't be able to spam it.
Using a windows service is probably the best practice method, but a simple VBScript will get the job done.
surl="http://localhost/SendConfirmationEmails.aspx"
set oxmlhttp=createobject("msxml2.xmlhttp")
with oxmlhttp
.open "GET",surl,false
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.send srequest
end with
You'd put the code above in a while wend loop with a Sleep to delay every 30 seconds...