|
-
Oct 27th, 2002, 10:19 AM
#1
Thread Starter
PowerPoster
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 sFileName, bool WaitAndCleanup)
{
sTempFile = this.Unpack(sFileName);
pTempProc = new Process();
pTempProc.StartInfo = new ProcessStartInfo(sTempFile);
pTempProc.Start();
if (WaitAndCleanup)
{
Thread th = new Thread(new ThreadStart( KillFile ) );
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.
-
Oct 27th, 2002, 10:59 AM
#2
Frenzied Member
Which function do you want to pass the arguments to?
Dont gain the world and lose your soul
-
Oct 27th, 2002, 11:38 AM
#3
PowerPoster
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.
-
Oct 28th, 2002, 04:25 PM
#4
Thread Starter
PowerPoster
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.
-
Oct 28th, 2002, 04:58 PM
#5
Frenzied Member
Is this what you are trying to do??
PHP Code:
private void KillFile(Process proc, string tempFile)
{
proc.WaitForExit();
File.Delete(tempFile);
}
public void Execute(string sFileName, bool WaitAndCleanup)
{
sTempFile = this.Unpack(sFileName);
pTempProc = new Process();
pTempProc.StartInfo = new ProcessStartInfo(sTempFile);
pTempProc.Start();
if (WaitAndCleanup)
{
Thread th = new Thread(new ThreadStart( KillFile(pTempProc, sTempfile) ) );
th.Start();
}
return;
}
Dont gain the world and lose your soul
-
Oct 28th, 2002, 06:01 PM
#6
PowerPoster
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..
-
Oct 28th, 2002, 06:07 PM
#7
Thread Starter
PowerPoster
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.
-
Oct 28th, 2002, 06:16 PM
#8
PowerPoster
Yep...Your best bet is to do what hellswraith suggested by wrapping your thread functionality into a seperate class.
-
Nov 6th, 2002, 05:39 PM
#9
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|