locked
Help about using ParametrizedThreadstart class ?? RRS feed

  • Question

  • my question is , if i have this code

                ParameterizedThreadStart operation = new ParameterizedThreadStart(ListboxObject);
    
                Thread firstthread = new Thread(operation);
                firstthread.Start();
    
     static void ListboxObject(object o)
            {
    
                ListBox list = (ListBox)o;
    
                for (int i = 0; i < 100; i++)
                {
              
    // i need to add items to listbox in this thread operation
    
                }
            }
    


    i need to know how to making some operation inside thread , like add items for list , or doing anything

    i need example please help me to use it instead of object parameters

    thank you


    Hesham Hemdan Saleh
    Saturday, September 24, 2011 11:38 PM

Answers

  • my question is , if i have this code

                ParameterizedThreadStart operation = new ParameterizedThreadStart(ListboxObject);
    
                Thread firstthread = new Thread(operation);
                firstthread.Start();
    
     static void ListboxObject(object o)
            {
    
                ListBox list = (ListBox)o;
    
                for (int i = 0; i < 100; i++)
                {
              
    // i need to add items to listbox in this thread operation
    
                }
            }
    


    i need to know how to making some operation inside thread , like add items for list , or doing anything

    i need example please help me to use it instead of object parameters

    thank you


    Hesham Hemdan Saleh

    Hi,

    You should not create or access controls in background thread.

     

    In this example data are downloaded from web in background thread.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    
    namespace ConsoleApplication6
    {
        class Program
        {
            static string data;
    
            static void Main(string[] args)
            {
                Console.WriteLine("Press any key to start");
                Console.ReadKey();
    
                ParameterizedThreadStart operation = new ParameterizedThreadStart(ListboxObject);
                Thread firstthread = new Thread(operation);
                firstthread.Start();
                firstthread.Join();
    
                Console.Clear();
                Console.WriteLine("Press any key to see downloaded data");
                Console.ReadKey();
                
                Console.WriteLine(data);
                Console.ReadKey();
            }
    
            static void ListboxObject(object o)
            {
                Console.Clear();
                Console.WriteLine("Downloading data in background thread");
    
                System.Net.WebClient webClient = new System.Net.WebClient();
                data = webClient.DownloadString("http://www.bing.com/");
            }
        }
    }
    
    


    • Marked as answer by Jackie-Sun Thursday, September 29, 2011 6:26 AM
    Sunday, September 25, 2011 2:46 AM

All replies

  • Try this way:

    Thread operation = new Thread(ListboxObject);
                List<int> integers = new List<int>()
                {
                 1, 2, 3, 4
                };
                operation.Start(integers);
    
      static object _lock = new object();
            static void ListboxObject(object o)
            {
                lock (_lock)
                {
                    List<int> integers = o as List<int>;
    
                    //add more ints to the list
                    for (int i = 0; i < 100; i++)
                    {
                        integers.Add(i);                   
                    }
                }
    
            }
    Hope I helped you,
    BR kpato

    • Proposed as answer by kpato Sunday, September 25, 2011 12:30 AM
    • Unproposed as answer by kpato Sunday, September 25, 2011 12:30 AM
    Sunday, September 25, 2011 12:28 AM
  • my question is , if i have this code

                ParameterizedThreadStart operation = new ParameterizedThreadStart(ListboxObject);
    
                Thread firstthread = new Thread(operation);
                firstthread.Start();
    
     static void ListboxObject(object o)
            {
    
                ListBox list = (ListBox)o;
    
                for (int i = 0; i < 100; i++)
                {
              
    // i need to add items to listbox in this thread operation
    
                }
            }
    


    i need to know how to making some operation inside thread , like add items for list , or doing anything

    i need example please help me to use it instead of object parameters

    thank you


    Hesham Hemdan Saleh

    Hi,

    You should not create or access controls in background thread.

     

    In this example data are downloaded from web in background thread.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    
    namespace ConsoleApplication6
    {
        class Program
        {
            static string data;
    
            static void Main(string[] args)
            {
                Console.WriteLine("Press any key to start");
                Console.ReadKey();
    
                ParameterizedThreadStart operation = new ParameterizedThreadStart(ListboxObject);
                Thread firstthread = new Thread(operation);
                firstthread.Start();
                firstthread.Join();
    
                Console.Clear();
                Console.WriteLine("Press any key to see downloaded data");
                Console.ReadKey();
                
                Console.WriteLine(data);
                Console.ReadKey();
            }
    
            static void ListboxObject(object o)
            {
                Console.Clear();
                Console.WriteLine("Downloading data in background thread");
    
                System.Net.WebClient webClient = new System.Net.WebClient();
                data = webClient.DownloadString("http://www.bing.com/");
            }
        }
    }
    
    


    • Marked as answer by Jackie-Sun Thursday, September 29, 2011 6:26 AM
    Sunday, September 25, 2011 2:46 AM