class-method in a thread

  • Thread starter Thread starter Kevin Otte
  • Start date Start date
K

Kevin Otte

Hi,

I'm currently working on a little tool to copy files over the network
using FileStreams. I've written a class that takes care of that.
I create an instance of the class within the main program, specify
some properties and start a new thread which calls the StartCopy
method.
My idea was to have a method called 'Stop' which switches a bool to
true and the while-loop within the StartCopy stuff would check that
bool continously to know when to stop.

Now the problem is that I don't seem to be able to do that because
everytime i call 'Stop', the 'StartCopy' method seems to stop working
without even checking the bool.

I'm quite close to a nervous breakdown...
 
Please don't have a nervous breakdown and please show some code as an
example of what you're doing.
 
Please do boil down some sample code and post it, so we can understand what
you're [not] doing. Is the main thread even bothering to wait on, or join,
the worker thread?
 
The following is basically the code from the class to copy a file:
It works fine. The problem is that I have to run the StartCopy method
in seperate thread.



public void StartCopy()
{
while(streamSrc.Position < m_lFileSize)
{
streamSrc.Read(bytChunk, 0, m_iChunkSize);
streamDest.Write(bytChunk, 0, m_iChunkSize);
streamDest.Flush();

m_lBytesCopied += m_iChunkSize;
ChunkCopied(m_lBytesCopied, m_iJobID);

if(m_bPaused == true)
{
m_bPauseSuccessfull = true;
return;
}
}
}


public bool IsPaused
{
get
{
return m_bPaused;
}

set
{
m_bPaused = value
}
}


code in my main class:

CopyJob = new CFileCopy(...);


Thread thrCopyFile = new Thread(newThreadStart(ThreadCopyFile));
thrCopyFile.Start();

private void ThreadCopyFile()
{
CopyJob.StartCopy();
}

private void Botton123_Click(object sender, System.EventArgs e)
{
CopyJob.IsPaused = true;
}


When I click Button123 the code in the class's IsPaused property is
called, but the StartCopy method does not seem to notice.
 
Kevin,

The StartCopy() method has probably already finished copying the file! I
slowed it down by calling Thread.Sleep(100) on every iteration, and I didn't
see this problem. I also inserted a return statement before the method's
final curly brace, just so that I could set a breakpoint there; without the
call to Sleep(), a 4Mb file would be copied before I could even click a
second button. By the way, your main thread should call Join() on the thread
it spawned.

I can send you the source, if you like.

Tony
 
Back
Top