English 中文(简体)
伙伴关系中的全球变量。 NET
原标题:Global variable in ASP.NET

我愿停止一线穿透,而另一条路面仍在实施法典的范畴:

I m currently going the following: global.asax.cs

    private static bool _isProcessingNewIllustrationRequest;

    public static bool IsProcessingNewIllustrationRequest
    {
        get { return _isProcessingNewIllustrationRequest; }
        set { _isProcessingNewIllustrationRequest = value; }
    }

Then in MVC:

    public ActionResult CreateNewApplication()
    {
        if (!Global.IsProcessingNewIllustrationRequest)
        {
            Global.IsProcessingNewIllustrationRequest = true;

            // DO WORK... RUN CODE
            Global.IsProcessingNewIllustrationRequest = false;
            return View("Index", model);
        }
        else
        {
            // DISPLAY A MESSAGE THAT ANOTHER REQUEST IS IN PROCESS
        }
    }

但是,如果似乎守则是徒劳的,因为两条read(Chrome &)仍然同时执行该守则。

<>蓬贝>

private Object thisLock = new Object();     
public ActionResult CreateApplication()
        {
            ILog log = LogManager.GetLogger(typeof(Global));
            string aa = this.ControllerContext.HttpContext.Session.SessionID;
            log.Info("MY THREAD: " + aa);

            lock (thisLock)
            {
                Thread.Sleep(8000);

                DO SOME STUFF

            }
        }

即便有这一标志,第2版(Firefox 届会)仍然在法典中,而第1场(Chrome)正在24小时执行。 我失踪了什么?

最佳回答

I can see two obvious reasons why this code won t do what you want.

问题1:它并非read。 每当有人与你的服务器连接时,这一请求就会read,如果多重要求出现,那么多管线将同时运行。 几条镜子可以非常清楚地读到国旗,所有镜头都看到该国旗是假的,因此,它们都进入了<条码>>>,所有物体都做了工作,这正是你试图预防的。

这是“种族条件”(结果取决于谁赢得种族)。 Boo变数足以解决种族状况。

切实解决这一问题的办法十分艰巨。 最好的办法是消除共同国家,使每一个国家能够完全独立于其他国家,没有必要将其他国家分开;但我假定,你已经认为,这并不实际。 接下来的研究是:mutexes monitors。 监测器的使用很少,但只有两面镜都实际处于同一个顶点和同一过程的情况下,它们才会发挥作用。 这使我......

问题2:两种线索可能而不是相同。 最新版本的IIS将启动多个单独的工人进程,处理网络要求。 每个过程都有自己的地址空间,即每个过程(每件技术文件每一过程)都有自己的“全球”变量。

In fact, if you re building a really high-traffic Web site, the different worker processes may not even run on the same computer.

Worker processes are your most likely culprit. If you were facing a race condition, the problem would be incredibly rare (and even more incredibly hard to debug when it did come up). If you re seeing it every time, my money is on multiple worker processes.

Let s assume that all the worker processes will be on the same server (after all, if you had the kind of budget that required apps that can scale out to multiple Web servers, you would already know way more about multithreaded programming than I do). If everything s on the same server, you can use a named Mutex to coordinate across different appdomains and processes. Something like this:

public static Mutex _myMutex = new Mutex(false, @"GlobalSomeUniqueName");

public ActionResult CreateNewApplication()
{
    if (_myMutex.WaitOne(TimeSpan.Zero))
    {
        // DO WORK... RUN CODE
        _myMutex.ReleaseMutex();
        return View("Index", model);
    }
    else
    {
        // DISPLAY A MESSAGE THAT ANOTHER REQUEST IS IN PROCESS
    }
}

The WaitOne(TimeSpan.Zero) will return immediately if another thread owns the mutex. You could choose to pass a nonzero timespan if you expect that the other thread will usually finish quickly; then it will wait that long before giving up, which would give the user a better experience than failing instantly into a "please try again later" error message.

问题回答

ASP.NET spools up worker processes as needed, don t fight it.

Use a database queue and let ASP.NET work optimally. You will be choking the sever trying to control the threading, if you even can, on top of creating highly un-maintainable code. If you do find a way to control it, I can t imagine the type of bugs you are going to run into.

阁下 上文Edit一案,你正在制造这个新物体,每个控制员都在场,因此,它只能锁定同样read的法典,而且只有一个要求,这样就没有了。

Try to log your global class variables upon application startup so the static initializers run and you know things are setup . Then use an object that exists in this global class (it could be anything - a string) and use that as your lock variable.

The only way to accomplish this is by using a mutex. The "lock" statement is syntactic sugar for a mutex.

You should abstract your code into a static class that has a static member variable so all threads in your app-domain will have access to this class.

Now, you can encapsulate all the code in your two static member functions in a lock statement. Please ensure that you are locking the same lock variable in the two member functions.

Also, if you do not want these member functions to be static then your class doesn t have to be static. You will be fine as long as your lock variable is static.

Hope this helps.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签