Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. threading in a Custom Collection

threading in a Custom Collection

Scheduled Pinned Locked Moved C#
cssdesigntutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Juvil John
    wrote on last edited by
    #1

    Hi All, I'd like to make a separate thread in inserting of data into the Custom Collection I have and fires an event when its done so it would be bound to a grid. Is it possible? here is my example code.

    public class Details : List<Details.Detail>
    {
        public delegate void \_WorkComplete();
        public event \_WorkComplete WorkComplete;
    
        public void AddDataSource(object value)
        {
            Thread worker = new Thread(new ParameterizedThreadStart(dowork));
            worker.Start(value);
        }
    
        private void dowork(object value)
        {
            ArrayList list = (ArrayList)value;
            foreach (Item item in list)
            {
                this.Add(new Detail { FIELD1 = item.Code1, FIELD2 = item.Code2 });
            }
            if (WorkComplete != null) WorkComplete();
        }
    
        public class Detail
        {
            private string field1;
            private string field2;
    
            public Detail()
            {
                field1 = string.Empty;
                field2 = string.Empty;
            }
    
            public string FIELD1
            {
                get { return field1; }
                set { field1 = value; }
            }
    
            public string FIELD2
            {
                get { return field2; }
                set { field2 = value; }
            }
        }
    }
    

    so in the UI side i could do

    private Details details = new Details();

    protected void Page_Load(object sender, EventArgs e)
    {
    details.OnWorkComplete += new Details._WorkComplete(details_AddComplete);
    details.AddDataSource(source1);
    details.AddDataSource(source2);
    }

    void details_AddComplete()
    {
    grid1.DataSource = details;
    grid1.DataBind();
    }

    L N 2 Replies Last reply
    0
    • J Juvil John

      Hi All, I'd like to make a separate thread in inserting of data into the Custom Collection I have and fires an event when its done so it would be bound to a grid. Is it possible? here is my example code.

      public class Details : List<Details.Detail>
      {
          public delegate void \_WorkComplete();
          public event \_WorkComplete WorkComplete;
      
          public void AddDataSource(object value)
          {
              Thread worker = new Thread(new ParameterizedThreadStart(dowork));
              worker.Start(value);
          }
      
          private void dowork(object value)
          {
              ArrayList list = (ArrayList)value;
              foreach (Item item in list)
              {
                  this.Add(new Detail { FIELD1 = item.Code1, FIELD2 = item.Code2 });
              }
              if (WorkComplete != null) WorkComplete();
          }
      
          public class Detail
          {
              private string field1;
              private string field2;
      
              public Detail()
              {
                  field1 = string.Empty;
                  field2 = string.Empty;
              }
      
              public string FIELD1
              {
                  get { return field1; }
                  set { field1 = value; }
              }
      
              public string FIELD2
              {
                  get { return field2; }
                  set { field2 = value; }
              }
          }
      }
      

      so in the UI side i could do

      private Details details = new Details();

      protected void Page_Load(object sender, EventArgs e)
      {
      details.OnWorkComplete += new Details._WorkComplete(details_AddComplete);
      details.AddDataSource(source1);
      details.AddDataSource(source2);
      }

      void details_AddComplete()
      {
      grid1.DataSource = details;
      grid1.DataBind();
      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Search in google for cross-thread-exception. You cannot change something in control from another thread. "grid1.DataSource = details;" -> Cross Thread Exception

      void details_AddComplete()
      {
      Invoke(new MethodInvoker(AddComplete));
      }

      void AddComplete()
      {
      grid1.DataSource = details;
      grid1.DataBind();
      }

      J 1 Reply Last reply
      0
      • L Lost User

        Search in google for cross-thread-exception. You cannot change something in control from another thread. "grid1.DataSource = details;" -> Cross Thread Exception

        void details_AddComplete()
        {
        Invoke(new MethodInvoker(AddComplete));
        }

        void AddComplete()
        {
        grid1.DataSource = details;
        grid1.DataBind();
        }

        J Offline
        J Offline
        Juvil John
        wrote on last edited by
        #3

        that does not fire an error the "details" is created on the same thread. as the "grid1". not the comment I expected but thanks for the reply.

        modified on Wednesday, August 12, 2009 5:07 AM

        1 Reply Last reply
        0
        • J Juvil John

          Hi All, I'd like to make a separate thread in inserting of data into the Custom Collection I have and fires an event when its done so it would be bound to a grid. Is it possible? here is my example code.

          public class Details : List<Details.Detail>
          {
              public delegate void \_WorkComplete();
              public event \_WorkComplete WorkComplete;
          
              public void AddDataSource(object value)
              {
                  Thread worker = new Thread(new ParameterizedThreadStart(dowork));
                  worker.Start(value);
              }
          
              private void dowork(object value)
              {
                  ArrayList list = (ArrayList)value;
                  foreach (Item item in list)
                  {
                      this.Add(new Detail { FIELD1 = item.Code1, FIELD2 = item.Code2 });
                  }
                  if (WorkComplete != null) WorkComplete();
              }
          
              public class Detail
              {
                  private string field1;
                  private string field2;
          
                  public Detail()
                  {
                      field1 = string.Empty;
                      field2 = string.Empty;
                  }
          
                  public string FIELD1
                  {
                      get { return field1; }
                      set { field1 = value; }
                  }
          
                  public string FIELD2
                  {
                      get { return field2; }
                      set { field2 = value; }
                  }
              }
          }
          

          so in the UI side i could do

          private Details details = new Details();

          protected void Page_Load(object sender, EventArgs e)
          {
          details.OnWorkComplete += new Details._WorkComplete(details_AddComplete);
          details.AddDataSource(source1);
          details.AddDataSource(source2);
          }

          void details_AddComplete()
          {
          grid1.DataSource = details;
          grid1.DataBind();
          }

          N Offline
          N Offline
          Nicholas Butler
          wrote on last edited by
          #4

          Assuming you add all your sources in Page_Load, you could do this: In AddDataSource, just add the source to a collection inside Details. Add a method Details.StartProcessing() which starts the background work. So Page_Load would look like:

          details.AddDataSource( source1 );
          details.AddDataSource( source2 );
          details.StartProcessing();
          }

          As Stan said, your event will be fired on the background thread. Either use a BackgroundWorker instead of a Thread, or use Invoke before you update the grid. Nick

          ---------------------------------- Be excellent to each other :)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups