并行调用方法的未引用对象错误

时间:2021-09-24 13:54:12

I have a problem not referenced object if called with parallel.invoke , the problem is that if you call them one by one the methods , they work .

如果使用parallel.invoke调用,我有一个问题没有引用对象,问题是如果你逐个调用它们的方法,它们就可以了。

try
        {
            Task t1 = Task.Run(async () =>
            {
                log.Info("call GetRecentHomeChanges");
                GetRecentHomeChanges resultHomeChanges = await apManager.GetRecentHomeChanges(ApplicationContext.Instance.LoggedUser.UserName, ApplicationContext.Instance.LoggedUser.HashedPwd);
                ApplicationContext.Instance.pastmeetingList = resultHomeChanges.PastMeetings;
                ApplicationContext.Instance.documentsHomePageList = resultHomeChanges.Attachments;
            });
            Task t2 = Task.Run(() =>
            {
                //STORE PROCEDURE CALL - INSERISCO I PAST MEETINGS
                storeProcedure.insertPastMeetings(ApplicationContext.Instance.LoggedUser.PID, ApplicationContext.Instance.pastmeetingList);
                List<PastMeetings> pastMeetingsDB = storeProcedure.selectPastMeetings(ApplicationContext.Instance.LoggedUser.PID);
                ApplicationContext.Instance.pastmeetingList = pastMeetingsDB;
            });

            await Task.WhenAll(t1, t2);
        }
        catch(Exception ex)
        {
            throw ex;
        }

if I call them so they work regularly :

如果我给他们打电话,他们经常工作:

        log.Info("call GetRecentHomeChanges");
        GetRecentHomeChanges resultHomeChanges = await apManager.GetRecentHomeChanges(ApplicationContext.Instance.LoggedUser.UserName, ApplicationContext.Instance.LoggedUser.HashedPwd);
        ApplicationContext.Instance.pastmeetingList = resultHomeChanges.PastMeetings;
        ApplicationContext.Instance.documentsHomePageList = resultHomeChanges.Attachments;

        //STORE PROCEDURE CALL - INSERISCO I PAST MEETINGS
        storeProcedure.insertPastMeetings(ApplicationContext.Instance.LoggedUser.PID, ApplicationContext.Instance.pastmeetingList);
        List<PastMeetings> pastMeetingsDB = storeProcedure.selectPastMeetings(ApplicationContext.Instance.LoggedUser.PID);
        ApplicationContext.Instance.pastmeetingList = pastMeetingsDB;

So my problem occurs only when calling in parallel , and the printed error code is : { " Object reference not set to an instance of an object . " }

所以我的问题只发生在并行调用时,并且打印的错误代码是:{“对象引用未设置为对象的实例。”}

how can i solve ?

我怎么解决?

1 个解决方案

#1


0  

You're setting ApplicationContext.Instance.pastmeetingList in the first threadpool task, and reading ApplicationContext.Instance.pastmeetingList in the second threadpool task. By executing both of these in parallel, you're telling your code to read and write the variable at the same time. Hopefully it's obvious that this won't work if the read takes place before the write.

您将在第一个线程池任务中设置ApplicationContext.Instance.pastmeetingList,并在第二个线程池任务中读取ApplicationContext.Instance.pastmeetingList。通过并行执行这两个操作,您可以告诉代码同时读取和写入变量。希望很明显,如果在写入之前进行读取,这将不起作用。

So, this code can't be parallelized.

因此,此代码无法并行化。

#1


0  

You're setting ApplicationContext.Instance.pastmeetingList in the first threadpool task, and reading ApplicationContext.Instance.pastmeetingList in the second threadpool task. By executing both of these in parallel, you're telling your code to read and write the variable at the same time. Hopefully it's obvious that this won't work if the read takes place before the write.

您将在第一个线程池任务中设置ApplicationContext.Instance.pastmeetingList,并在第二个线程池任务中读取ApplicationContext.Instance.pastmeetingList。通过并行执行这两个操作,您可以告诉代码同时读取和写入变量。希望很明显,如果在写入之前进行读取,这将不起作用。

So, this code can't be parallelized.

因此,此代码无法并行化。