0

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>(); 
halfer
  • 19,824
  • 17
  • 99
  • 186
I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216

1 Answers1

1

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>.

pkuderov
  • 3,501
  • 2
  • 28
  • 46
  • What do you mean by supposed types?Real problem is i want to get progress of each import item that is there is any error while executing any import item.if yes then add those errors to list and display it – I Love Stackoverflow Feb 03 '17 at 15:48
  • I have told the reason that why datatype of parameter is object because StartLongRunningProcess method is used on another page and StartLongRunningProcess is running on threadpool and with threadpool you can pass parameter as object only :) – I Love Stackoverflow Feb 03 '17 at 15:56
  • Upvoted for your kind efforts towards helping me.Can you explain me your last point : Test.Errors should be of thread-safe type against Add operation. For example, you can use ConcurrentQueue?? – I Love Stackoverflow Feb 03 '17 at 16:11
  • Thank you) Because you pass the same `Errors` object of `Test` class to `StartLongRunningProcess`es running probably the same time in different threads. For more look at http://stackoverflow.com/questions/5589315/list-add-thread-safety – pkuderov Feb 03 '17 at 16:16
  • Start method of test class is running on thread pool.still Test.errors should be threadsafe?? – I Love Stackoverflow Feb 03 '17 at 16:36
  • Updated my answer, btw. Forgot to write a comment about it to generate a notification for you – pkuderov Feb 06 '17 at 13:30