Thread watching

G

Grant

Problem:
I would like to save a file (Using C#) but am getting the occassional write
error. I would like to do that on another thread but how would I know
whether it was a success or failure?

--------------------------
Thread WorkerThread = new Thread(new ThreadStart(updateSomething));
WorkerThread.Start();

Thread WatcherThread = new Thread(new ThreadStart(ThreadWatcher));
WatcherThread.Start();

private void ThreadWatcher(){
while ( some boolean value is false on the first thread)
{
Thread.sleep(100);
}

when it gets out the loop do something here;
}
--------------------------

Do I have to have another watcher thread that sleeps while the worker thread
is still busy - and when it is finished, check its status? How would I do
this?

Thanks,
Grant
 
G

Guest

Hi Grant,

Grant said:
Problem:
I would like to save a file (Using C#) but am getting the occassional write
error. I would like to do that on another thread but how would I know
whether it was a success or failure?

--------------------------
Thread WorkerThread = new Thread(new ThreadStart(updateSomething));
WorkerThread.Start();

Thread WatcherThread = new Thread(new ThreadStart(ThreadWatcher));
WatcherThread.Start();

private void ThreadWatcher(){
while ( some boolean value is false on the first thread)
{
Thread.sleep(100);
}

when it gets out the loop do something here;
}
--------------------------

Do I have to have another watcher thread that sleeps while the worker thread
is still busy - and when it is finished, check its status? How would I do
this?

Thanks,
Grant

The thread that writes to the file can set an event when it has finished
processing. An event would be an instance of ManualResetEvent. Another thread
can wait on the event until it is signaled.

Mutual Data:
ManualResetEvent ProcessingEvent = new ManualResetEvent( false );
bool ProcessStatus;

Thread1: Processes file
// process file
ProcessStatus = <outcome of processing file>;
ProcessingEvent.Set();

Thread2: Process results
ProcessingEvent.WaitOne();
if( ProcessStatus )
<do this>;
else
<do that>;

HTH,
TT
 
S

Steve Walker

Grant said:
Problem:
I would like to save a file (Using C#) but am getting the occassional write
error. I would like to do that on another thread but how would I know
whether it was a success or failure?

There's no particular reason I can see why this need be a problem. You
can read and write variables in scope from either thread.
 
W

Willy Denoyette [MVP]

Grant said:
Problem:
I would like to save a file (Using C#) but am getting the occassional
write error. I would like to do that on another thread but how would I
know whether it was a success or failure?

--------------------------
Thread WorkerThread = new Thread(new ThreadStart(updateSomething));
WorkerThread.Start();

Thread WatcherThread = new Thread(new ThreadStart(ThreadWatcher));
WatcherThread.Start();

private void ThreadWatcher(){
while ( some boolean value is false on the first thread)
{
Thread.sleep(100);
}

when it gets out the loop do something here;
}
--------------------------

Do I have to have another watcher thread that sleeps while the worker
thread is still busy - and when it is finished, check its status? How
would I do this?

Thanks,
Grant

What write errors, please specify the exact exception and prefarably a code
sample that illustrates your problem. You won't solve this by doing
something on another thread.

Willy.
 
G

Grant

I was going through an XML document and modifying nodes but if a node wasnt
found I was adding it in a seperate function. I was doing a save in both
functions and thats where the problem was.

The problem is fixed but I am now doing it with threads - just for the sake
of learning how to use them :)

Thanks for your help,
Grant
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top