473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

class-method in a thread

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...
Nov 15 '05 #1
4 1185
Please don't have a nervous breakdown and please show some code as an
example of what you're doing.

"Kevin Otte" <ja*******@gmx.de> wrote in message
news:d8*************************@posting.google.co m...
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...

Nov 15 '05 #2
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?
Nov 15 '05 #3
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.
Nov 15 '05 #4
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
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gabriel Genellina | last post by:
Hi In the following code sample, I have: - a Worker class, which could have a lot of methods and attributes. In particular, it has a 'bar' attribute. This class can be modified as needed. - a...
13
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes....
9
by: mead | last post by:
What kind of classes is qualified as "concrete classes"? When should a member function in a class defined as "pure virtual" and when as "virtual"? Thanks!
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
38
by: looping | last post by:
For Python developers around. >From Python 2.5 doc: The list of base classes in a class definition can now be empty. As an example, this is now legal: class C(): pass nice but why this...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
2
by: Cari Elf | last post by:
I wrote a template class that inherits from the PUG xml_tree_walker class so that I can load data from any XML file without having to write a parser for each one. template <class T> class...
19
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
8
by: Mayur H Chauhan | last post by:
All, For my knowledge, if I declare Class as follow, then it thows compilation error. Protected Class Book End Class Even same for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.