Threading Newbie

  • Thread starter Thread starter Harry Whitehouse
  • Start date Start date
H

Harry Whitehouse

I have button-press response function which gathers some data, and spawns
another application. The spawned application may or may not create a
predetermined fine name (an answer file) which my C# application will be
looking for.

I start a thread to look for the file as shown below. Actually, this works
somewhat well, but I have two residual problems/questions:

a) It is possible that the spawned application might not create the file
being searched for (the user aborts), and then the user returns to my C# app
and start the process again. If this happens, I could end up with two
threads in action. I really only need/want one thread looking for the
answer file. Have I handled this properly in my code below? I'm tempted to
completely stop the thread and restart a new one each time DoLink() is
invoked, but perhaps it's OK to start it on the first DoLink() invocation
and keep the thread running permanently.

b) When I find the file, if I just display a MessageBox in the
MonitorForFile() thread member. But if I attempt to read the file (see the
ReadFile() function), the subsequent MessageBox never appears. I'm
beginning to wonder if the thread member function can't be used for certain
purposes. How can I best invoke a more complex process from my thread
member function?

TIA

Harry

private bool DoLink(){

// do some stuff...

// start the file monitoring thread only once, DoLink() may be invoked many
times

if(tMonitorForFile==null)

{

tMonitorForFile = new Thread(new ThreadStart(MonitorForFile));

tMonitorForFile.IsBackground=true;

tMonitorForFile.Start();

bLookingForFile=true;

}

}

public void MonitorForFile()

{

bool filefound=false;

int i=0;

do

{

label9.Text= "File Monitor round " + i.ToString();

Thread.Sleep(100);

if(File.Exists(XMLOutputFileName))

{

filefound=true;

bLookingForFile=false;

break;

}

i++;

} while(true);




label9.Text="File found!";

if(filefound)

{

// ReadFile(); // If I attempt to invoke this, the MessageBox
never below appears!

System.Windows.Forms.MessageBox.Show("File
Found","Test",MessageBoxButtons.OK);



}



}
 
Back
Top