MinorDBDataContext mdc = new ..." />
开发者

C# Parallel Loop Problem

开发者 https://www.devze.com 2023-03-07 04:29 出处:网络
When this Codes Runs show me This error: \"Object reference not set to an instance of an object\" error occur in first line => MinorDBDataContext mdc = new ...

When this Codes Runs show me This error: "Object reference not set to an instance of an object" error occur in first line => MinorDBDataContext mdc = new ...

but also with serial for "for(int i; i < 1005; i++) { } " works fine.

what's the problem ?

Thread.CurrentThread.Priority = ThreadPriority.Highest;
        var query = from M in new MajorDBDataContext().User_Accounts select M;
        List<User_Account> Ulist = query.ToList();
        string d = DateTime.Now.ToString();
        int c = 0;
        string temp ="";
        Parallel.For(0, 1005, (i,loop) =>
        {
         开发者_如何学JAVA       try
                {
                    MinorDBDataContext mdc = new MinorDBDataContext(_Filings.OnServerRepository(Ulist[i].user_Sys_DBPath));
                    GoodJob(mdc, temp, i);
                    DA.Page page = mdc.Pages.Single();
                    temp += mdc.Minor_Users.Take(1).SingleOrDefault().Minor_User_Email;
                    temp += mdc.Minor_Users.Take(1).SingleOrDefault().Minor_User_Name;
                    temp += mdc.Minor_Users.Take(1).SingleOrDefault().Minor_User_Family + i.ToString();
                }
                catch { }
        });
        append(temp);


Your Exception may be indicative of other things. Which value is actually null? you don't say.

I can see some problems with this code.

You cannot depend on the sequence of the loop, yet you have temp += (sequentially adding a string to the an existing string then assigning the string back to the original variable) throughout the parallelised code. Temp being set up outside the parallel loop. This is very dangerous. Misuse of shared state/data/resources is one of the most common faults in multi-threaded code.

temp may have the values added out of sequence. Multiple things will be added to temp simultaneously, and thus temp may end up missing information because temp is being overwritten from multiple threads.

Where does _Filings come from? What is it?

You may want to create a new temp string inside the loop and add everything there, adding the final temp to a ConcurrentBag (which you create outside the loop) in each iteration. Afterwards, outside of the parallel loop, you can iterate over the ConcurrentBag and get build your final string in a safe environment.

Update based on comment Comment:

System.Web.HttpContext.Current.Server.MapPath used and HttpContext.Current is null in Multi Thread loop.

The reason for this is that you are now on a different thread to the current HttpContext, therefore it can't answer any questions for you. You'll either have to pass in the context to the parallel task or the individual values from it. I don't know how dangerous that is in a parallel environment.

HttpContext myContext = HttpContext.Current;

In your parallel loop use myContext rather than HttpContext.Current.

I don't recommend this as the knowledge of the HttpContext in areas (like the data layer) sets up unnecessary coupling and made the application very difficult to maintain. To recommend a better solution would require pulling your application apart much more that is viable in a forum posting. However, for the moment it should get you moving again.

0

精彩评论

暂无评论...
验证码 换一张
取 消