When calling WebMethod on a Webpage using jQuery. We define this as static.
However static methods always have one instance. What happens when multiple web requests are made.
- Does it really happen asynchronously or
- all the requests are pipelined waiting for the WebMethod to accept the requests?
I created a sample console program to simulate the scenario on static method work & found them to execute in sequential order.
class Program
{
static int count = 10;
static void Main(string[] args)
{
new Program().foobar();
Console.ReadLine();
}
public void foobar()
{
Parallel.Invoke(() => work("one"), () => work("two"), () => work("three"), ()=> work("four"));
}
static void work(string str)
{
Thread.Sleep(3000);
count++;
Console.WriteLine(str + " " + count);
}
}
Can you please put some light on this concept?
They will not execute sequentially. If you created multiple apps in a client server scenario it would be a better example since your console app inherently runs everything sequentially.
That said, with the static methods you just need to be aware of shared resources, data, etc. Local data is fine.