I Love Stackoverflow
I Love Stackoverflow

Reputation: 6868

Unable to add records to list passed as object to method

I have this class :

Public class Error
{
     public string ErrorMessage { get; set; }
}

public class Test
    {
        public int Id { get; set; }
        public Import ImportResult { get; set; }
        public List<Error> Errors { get; set; }
        public void Start(object data)
        {
            object[] obj = data as object[];
            string[] importList = { "Item1", "Item2", "Item3", "Item4" };
            foreach (var item in importList)
            {
                object[] obj1 = new object[2];
                obj1[0] = item;
                obj1[1] = Errors;
                this.ImportResult = new Import(); 
                this.ImportResult.StartLongRunningProcess(obj1);//pass each import record to this method
            }
        }
    }

public class Import
    {
        public int Id { get; set; }
        public List<Error> Errors { get; set; }
        public void StartLongRunningProcess(object data) 
        {
            object[] datas = data as object[];
            string import = datas[0].ToString();
            try
            {
              // here I have along running process
            }
           // Here I store my error result in above list in long running process
            catch (Exception ex)
            {
               datas[1]. // Here I would like to add record to error list of Test class.
               Errors.Add(new Error
                 {
                    ErrorMessage =  ex.message
                 });
            }
        }
    }

Now what I am trying to do is I want to pass Error list of class Test as a parameter to StartLongRunningProcess method and from there I would like to store all error records in Error Variable of Test class.

The reason for doing this is because I have 1 method getCurrentData which is called every 5 second to get errors record currently running import item.So at the end of all import items I would have list of errors ready for all import item.

public string getCurrentData() //called every 5 seconds to get latest data
        {
           var data = TestList.FirstOrDefault(x => x.Key == 100).Value;
           response = new
           {
              errors = data.Errors.Select
                       (er => new
                       {
                            //error properties
                       }).ToList()   
           }

Parameter of StartLongRunningProcess is object because this method is running on threadpool from my another page that is why I have kept datatype as object for parameter data.

I also cannot make list of import like below :

  public List<Import> ImportResults = new List<Import>(); 

Upvotes: 0

Views: 162

Answers (1)

pkuderov
pkuderov

Reputation: 3571

Question:

Start method of test class is running on thread pool.still Test.errors should be threadsafe??

Given your line:

this.ImportResult.StartLongRunningProcess(obj1);//pass each import record to this method

you just syncronously call method StartLongRunningProcess. In this particular case there's no need to worry about thread-safety because both Start and StartLongRunningProcess are executing in the same thread.

But then there's also no need to have StartLongRunningProcess with object parameter ;) Just let it take string importData, List<Error> errors.

But if StartLongRunningProcess is starting in another thread (than Start) and your start many of them in foreach loop then obviously you should be concerned about thread-safety. Because more than one StartLongRunningProcess running on different thread may call Errors.Add(error) of the same Test object.

...and now I just noticed your last note in the original question. Why you cannot do that? What compiler says?


First of all. There's no Errors initialization for both classes. Do it in constructor.

Second, change datas[1]. // Here i would like to add record to error list of Test class. to:

(data[1] as List<Error>).Add(new Error ...);

Does it work?

Btw, Test.Errors should be of thread-safe type against Add operation. For example, you can use ConcurrentQueue<T>.

Upvotes: 1

Related Questions