Results 1 to 9 of 9

Thread: Arguments to Thread

  1. #1

    Thread Starter
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Arguments to Thread

    How can I pass arguments to the function I want to run in a new thread? I'm currently working around it like this, but could cause problems if the same instance of my class runs the procedure twice in a row.
    PHP Code:
    //....

    private string sTempFile;
    private 
    Process pTempProc;

    //....

    private void KillFile()
    {
        
    //wait for the process to exit and kill the file.
        //i want to pass a process and a filename
        //to this function when i start it in its own thread.
        
    pTempProc.WaitForExit();
        
    File.Delete(sTempFile);
    }

    public 
    void Execute(string sFileNamebool WaitAndCleanup)
    {
        
    sTempFile this.Unpack(sFileName);
        
    pTempProc = new Process();

        
    pTempProc.StartInfo = new ProcessStartInfo(sTempFile);

        
    pTempProc.Start();

        if (
    WaitAndCleanup
        {
            
    Thread th = new Thread(new ThreadStartKillFile ) );
            
    th.Start();
        }
        return;

    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Which function do you want to pass the arguments to?
    Dont gain the world and lose your soul

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    What I would do is build a class that has this method, and the properties to set for the variables needed.

    Now, you create an instance of the class, set the properties, and start it's method on its own thread. Then when you need to make another thread, you just create another object and do the same.

  4. #4

    Thread Starter
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    hellswraith: I wonder why I didn't think of that :-D thanks.
    DevGrp: I would like to pass sTempFile and pTempProc to the KillFile() function in its own thread. is this possible?
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  5. #5
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Is this what you are trying to do??

    PHP Code:
    private void KillFile(Process procstring tempFile)
    {
        
    proc.WaitForExit();
        
    File.Delete(tempFile);
    }

    public 
    void Execute(string sFileNamebool WaitAndCleanup)
    {
        
    sTempFile this.Unpack(sFileName);
        
    pTempProc = new Process();

        
    pTempProc.StartInfo = new ProcessStartInfo(sTempFile);

        
    pTempProc.Start();

        if (
    WaitAndCleanup
        {
            
    Thread th = new Thread(new ThreadStartKillFile(pTempProcsTempfile) ) );
            
    th.Start();
        }
        return;

    Dont gain the world and lose your soul

  6. #6
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Is this what you are trying to do??
    I dont think that will work. You can't pass arguments to a method that is executed on a seperate thread other than the processess main thread. Though, I haven't tried the above example..

  7. #7

    Thread Starter
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    That's what I tried first. Apparently you can't do it like that.
    ("Method Name Expected.")
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  8. #8
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Yep...Your best bet is to do what hellswraith suggested by wrapping your thread functionality into a seperate class.

  9. #9
    Lively Member
    Join Date
    Aug 1999
    Location
    Amsterdam
    Posts
    117
    You could use an async delegate, that way you can pass parameters to the function.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width