Passing parameters to a thread

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dirc

    Passing parameters to a thread

    We are building a threaded application which reacts to an event and starts
    the required processing on a new thread.

    However, we are finding it difficult finding a way of getting the function
    to run using its own data. I've looked at SetData and GetData but these seem
    only to be available once the thread is running.

    The ThreadStart delegate won't allow me to use a function with parameters..
    so I can see now way that a parent thread can pass a parameter to a child
    thread (a file name in this instance) in a thread safe way.

    Does anyone out there have any ideas as to how this can be done? It seems to
    me that the SetData and GetData have limited usefulness if you cannot pass
    thread specific data to the thread.

    Thanks







  • shanthsp2002
    New Member
    • Apr 2006
    • 28

    #2
    HI I have Similar Problem

    I am developing installer in which i am using custom installer class, my client want me to show a different window while installation to show progress of installation insted of the regular progressbar, so i have to use thread here

    i have created new form and in form1.cs i have created a method like

    privare void UpdateLabel(str ing progress)
    {

    label1.label=pr ogress;

    }


    now i have to call this in my custom installer class and there i will pass
    parameters to this function


    Thread th=new thread(new threadStart(Pr. UpdateLabel))
    th.start();

    bu how to pass parameter to this method is a big qustion for me

    if u got any idea plzz inform me too

    thank u



    Originally posted by Dirc
    We are building a threaded application which reacts to an event and starts
    the required processing on a new thread.

    However, we are finding it difficult finding a way of getting the function
    to run using its own data. I've looked at SetData and GetData but these seem
    only to be available once the thread is running.

    The ThreadStart delegate won't allow me to use a function with parameters..
    so I can see now way that a parent thread can pass a parameter to a child
    thread (a file name in this instance) in a thread safe way.

    Does anyone out there have any ideas as to how this can be done? It seems to
    me that the SetData and GetData have limited usefulness if you cannot pass
    thread specific data to the thread.

    Thanks

    Comment

    • shanthsp2002
      New Member
      • Apr 2006
      • 28

      #3
      Dear friend here is an idea

      Hi here is some code u can look at , i dont know werther i will work, but it looks to be working try it, all the best


      public class UrlFetcher
      {
      string url

      public UrlFetcher (string url)
      {
      this.url = url;
      }

      public void Fetch()
      {
      // use url here
      }
      }

      [... in a different class ...]

      UrlFetcher fetcher = new UrlFetcher (myUrl);
      new Thread (new ThreadStart (fetcher.Fetch) ).Start();

      Comment

      Working...