473,669 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Odd Behaviour??

I have an app that spawns a thread to copy some files.
The spawned thread then spawns threads to copy individual files and wait for
the specific file to finish copying or timeout.
The specific file copy procedure raises an event which gets handled in the
main form from where the original thread was spawned which then updates a
status box and a progress bar.
The first spawned thread raises a startcopy event which the main form
handles to display the progress bar and a finishcopy event to hide the
progress bar.
This was all working peachy until I moved the individual file copying to
individual threads. After that It was REALLY slow and the progress bar would
not display.
When I set the visibility of the progress bar to TRUE to begin with it works
fine. Even after the progress bar has been hidden by the finishcopy event
and shown again by the startcopy event if I run the routine again.

Now for the question.
Why is this so?

I apologize for the long winded explanation but I really dont understand why
this is.

TIA
JB

P.S. The main form "Freezes" when the progress bar is not visible to begin
with. It wont allow me to resize or do anything whilst the filecopying is
going on, even though it is being run in another thread.
Nov 15 '05 #1
11 1111
Maybe you have too many threads copying files at once causing your hdd to thrash. One thread might be better.

--
Michael Culley
"John Baro" <jo***@NOSPAMme sware.com.au> wrote in message news:V1******** **********@news-server.bigpond. net.au...
I have an app that spawns a thread to copy some files.
The spawned thread then spawns threads to copy individual files and wait for
the specific file to finish copying or timeout.
The specific file copy procedure raises an event which gets handled in the
main form from where the original thread was spawned which then updates a
status box and a progress bar.
The first spawned thread raises a startcopy event which the main form
handles to display the progress bar and a finishcopy event to hide the
progress bar.
This was all working peachy until I moved the individual file copying to
individual threads. After that It was REALLY slow and the progress bar would
not display.
When I set the visibility of the progress bar to TRUE to begin with it works
fine. Even after the progress bar has been hidden by the finishcopy event
and shown again by the startcopy event if I run the routine again.

Now for the question.
Why is this so?

I apologize for the long winded explanation but I really dont understand why
this is.

TIA
JB

P.S. The main form "Freezes" when the progress bar is not visible to begin
with. It wont allow me to resize or do anything whilst the filecopying is
going on, even though it is being run in another thread.

Nov 15 '05 #2
My guess: if you look at the number of drive faults you are getting, you
will see that you are thrashing.

In other words, your processor asks for a disk sector and starts reading.
Optimized files will occur with contiguous sectors on the drive (therefore,
as soon as the system is done reading the first sector, the next sector in
line is just coming into range as the head passes over the platter. In the
mean time, thread context switches to another thread, which asks for a
different sector. The first sector is read, but not the all contiguous
sectors. The hard drive seeks to the second location and starts reading
sectors. At this point, the thread context switches, and in your
application, the hard drive goes off again without finishing the optimal
task (it ALWAYS will... hard drive reads are slow, and the processor looks
for something else to do).

This is wildly inefficient. If you have one thread reading a file, it will
read all of the continguous sectors before seeking the hard drive to the
next location. If you have multiple threads reading multiple files, you
will have to seek to the same relative location many times just to complete.
Considering the fact that seeking from sector to sector is the biggest waste
of time in hard drive manipulations, you are NOT better off by
multi-threading an application that hits one hard drive.

This gets worse because you are probably writing to the same drive too,
right?

This phenomenom is called hard drive thrashing, and is similar to OS
thrashing (where so many applications are loaded at the same time that the
OS spends more time performing page faults to switch from one context to
another than it does actually doing any of the work of the applications).

Now, if you were copying files from multiple locations to multiple
destinations, I can see creating multiple threads. But one source to the
same destination is dicey enough without overwhelming it with seeks.

I hope that helps,
--- Nick Malik
Solutions Architect

"John Baro" <jo***@NOSPAMme sware.com.au> wrote in message
news:V1******** **********@news-server.bigpond. net.au...
I have an app that spawns a thread to copy some files.
The spawned thread then spawns threads to copy individual files and wait for the specific file to finish copying or timeout.
The specific file copy procedure raises an event which gets handled in the
main form from where the original thread was spawned which then updates a
status box and a progress bar.
The first spawned thread raises a startcopy event which the main form
handles to display the progress bar and a finishcopy event to hide the
progress bar.
This was all working peachy until I moved the individual file copying to
individual threads. After that It was REALLY slow and the progress bar would not display.
When I set the visibility of the progress bar to TRUE to begin with it works fine. Even after the progress bar has been hidden by the finishcopy event
and shown again by the startcopy event if I run the routine again.

Now for the question.
Why is this so?

I apologize for the long winded explanation but I really dont understand why this is.

TIA
JB

P.S. The main form "Freezes" when the progress bar is not visible to begin
with. It wont allow me to resize or do anything whilst the filecopying is
going on, even though it is being run in another thread.

Nov 15 '05 #3
Thanks for the answer.

I knew I should have written this when I was awake.

I only spawn 1 thread to copy all files which in turn spawns 1 thread to
copy 1 file at a time. This is done so that I can have fault tolerance and
cancel the copy if it is hanging on copying a file.
This occurrs when a file is damaged. I wrote this to copy files off a
damaged CD and ignore damaged files.

The most important part..
When the progress bar is set to visible before I start copying files (In the
IDE), then it works perfectly.
No disk thrashing is occurring, guaranteed.
I can email you the project if you want.
JB

"Michael Culley" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:OK******** ******@TK2MSFTN GP12.phx.gbl...
Maybe you have too many threads copying files at once causing your hdd to thrash. One thread might be better.
--
Michael Culley
"John Baro" <jo***@NOSPAMme sware.com.au> wrote in message

news:V1******** **********@news-server.bigpond. net.au...
I have an app that spawns a thread to copy some files.
The spawned thread then spawns threads to copy individual files and wait for the specific file to finish copying or timeout.
The specific file copy procedure raises an event which gets handled in the main form from where the original thread was spawned which then updates a status box and a progress bar.
The first spawned thread raises a startcopy event which the main form
handles to display the progress bar and a finishcopy event to hide the
progress bar.
This was all working peachy until I moved the individual file copying to
individual threads. After that It was REALLY slow and the progress bar would not display.
When I set the visibility of the progress bar to TRUE to begin with it works fine. Even after the progress bar has been hidden by the finishcopy event and shown again by the startcopy event if I run the routine again.

Now for the question.
Why is this so?

I apologize for the long winded explanation but I really dont understand why this is.

TIA
JB

P.S. The main form "Freezes" when the progress bar is not visible to begin with. It wont allow me to resize or do anything whilst the filecopying is going on, even though it is being run in another thread.


Nov 15 '05 #4
Thanks for the reply.
See my reply to MC above, Same scenario.
JB
Nov 15 '05 #5
Hi John,
You raise the event from the worker thread. The event handler, which is
method of the form class, is executed by this worker thread. Is it posible
that you don't call Control.Invoke and manipulate the progress bar from the
worker thread. If you do so you can exepect any strange behaviour. You know
that you can use controls only from the UI thread that has created them.

--
B\rgds
100
"John Baro" <jo***@NOSPAMme sware.com.au> wrote in message
news:o%******** **********@news-server.bigpond. net.au...
Thanks for the answer.

I knew I should have written this when I was awake.

I only spawn 1 thread to copy all files which in turn spawns 1 thread to
copy 1 file at a time. This is done so that I can have fault tolerance and
cancel the copy if it is hanging on copying a file.
This occurrs when a file is damaged. I wrote this to copy files off a
damaged CD and ignore damaged files.

The most important part..
When the progress bar is set to visible before I start copying files (In the IDE), then it works perfectly.
No disk thrashing is occurring, guaranteed.
I can email you the project if you want.
JB

"Michael Culley" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:OK******** ******@TK2MSFTN GP12.phx.gbl...
Maybe you have too many threads copying files at once causing your hdd to
thrash. One thread might be better.

--
Michael Culley
"John Baro" <jo***@NOSPAMme sware.com.au> wrote in message news:V1******** **********@news-server.bigpond. net.au... I have an app that spawns a thread to copy some files.
The spawned thread then spawns threads to copy individual files and wait for
the specific file to finish copying or timeout.
The specific file copy procedure raises an event which gets handled in the main form from where the original thread was spawned which then
updates
a status box and a progress bar.
The first spawned thread raises a startcopy event which the main form
handles to display the progress bar and a finishcopy event to hide the
progress bar.
This was all working peachy until I moved the individual file copying
to individual threads. After that It was REALLY slow and the progress bar
would not display.
When I set the visibility of the progress bar to TRUE to begin with it works fine. Even after the progress bar has been hidden by the finishcopy event and shown again by the startcopy event if I run the routine again.

Now for the question.
Why is this so?

I apologize for the long winded explanation but I really dont
understand why this is.

TIA
JB

P.S. The main form "Freezes" when the progress bar is not visible to begin with. It wont allow me to resize or do anything whilst the filecopying is going on, even though it is being run in another thread.



Nov 15 '05 #6
Hi Stoitcho

Thanks for the reply
The only references to pbStatus (the progress bar) are below.
I just checked again and it works exactly as I said.
When I have the progress bar hidden to start with, it does not draw properly
and when I tried to maximise the form it froze the app and I had to kill it.
It is so very odd.
private void fileCopy_CopyPr ogress(object sender, CopyProgressEve ntArgs e)

{

txtResults.Text = e.Message + "\r\n" + e.SourceFolder + "\\" + e.Filename +
"\r\n" + txtResults.Text ;

//really need to get or make a three segment scrolling progress bar like the
xp startup one

pbStatus.Value = pbStatus.Value == pbStatus.Maximu m ? pbStatus.Minimu m :
pbStatus.Value;

pbStatus.Value ++;

}

private void fileCopy_StartC opy(object sender, EventArgs e)

{

pbStatus.Visibl e = true;

}

private void fileCopy_Finish Copy(object sender, EventArgs e)

{

pbStatus.Visibl e = false;

}

}

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:Ov******** ******@tk2msftn gp13.phx.gbl...
Hi John,
You raise the event from the worker thread. The event handler, which is
method of the form class, is executed by this worker thread. Is it posible
that you don't call Control.Invoke and manipulate the progress bar from the worker thread. If you do so you can exepect any strange behaviour. You know that you can use controls only from the UI thread that has created them.

--
B\rgds
100
"John Baro" <jo***@NOSPAMme sware.com.au> wrote in message
news:o%******** **********@news-server.bigpond. net.au...
Thanks for the answer.

I knew I should have written this when I was awake.

I only spawn 1 thread to copy all files which in turn spawns 1 thread to
copy 1 file at a time. This is done so that I can have fault tolerance and
cancel the copy if it is hanging on copying a file.
This occurrs when a file is damaged. I wrote this to copy files off a
damaged CD and ignore damaged files.

The most important part..
When the progress bar is set to visible before I start copying files (In the
IDE), then it works perfectly.
No disk thrashing is occurring, guaranteed.
I can email you the project if you want.
JB

"Michael Culley" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:OK******** ******@TK2MSFTN GP12.phx.gbl...
Maybe you have too many threads copying files at once causing your hdd to
thrash. One thread might be better.

--
Michael Culley
"John Baro" <jo***@NOSPAMme sware.com.au> wrote in message

news:V1******** **********@news-server.bigpond. net.au...
> I have an app that spawns a thread to copy some files.
> The spawned thread then spawns threads to copy individual files and

wait
for
> the specific file to finish copying or timeout.
> The specific file copy procedure raises an event which gets handled in the
> main form from where the original thread was spawned which then updates
a
> status box and a progress bar.
> The first spawned thread raises a startcopy event which the main

form > handles to display the progress bar and a finishcopy event to hide the > progress bar.
> This was all working peachy until I moved the individual file

copying to > individual threads. After that It was REALLY slow and the progress

bar would
> not display.
> When I set the visibility of the progress bar to TRUE to begin with
it works
> fine. Even after the progress bar has been hidden by the finishcopy

event
> and shown again by the startcopy event if I run the routine again.
>
> Now for the question.
> Why is this so?
>
> I apologize for the long winded explanation but I really dont understand
why
> this is.
>
> TIA
> JB
>
> P.S. The main form "Freezes" when the progress bar is not visible to

begin
> with. It wont allow me to resize or do anything whilst the

filecopying is
> going on, even though it is being run in another thread.
>
>



Nov 15 '05 #7

"John Baro" <jo***@NOSPAMme sware.com.au> wrote in message
news:wR******** **********@news-server.bigpond. net.au...
Hi Stoitcho

Thanks for the reply
The only references to pbStatus (the progress bar) are below.
I just checked again and it works exactly as I said.
When I have the progress bar hidden to start with, it does not draw properly and when I tried to maximise the form it froze the app and I had to kill it. It is so very odd.


Odd things are bound to happen if you access controls from threads other
than the UI thread. As Stoitcho already suggested, use Control.Invoke to
access methods or properties of a control from other threads. Otherwise it
will never work reliably.

Sami
www.capehill.net
Nov 15 '05 #8
Hi John,
Obviously you don't use Control.Invoke and you try to manipulate the
progress bar from the worker thread. This is your problem I believe. The
following is what you might wanna do (see my suggestions inlined in your
code).

//Probably you have this already defined in your application
delegate void CopyProgressEve ntHandler(objec t sender, CopyProgressEve ntArgs
e);

private void fileCopy_CopyPr ogress(object sender, CopyProgressEve ntArgs e)

{
//You don't have to do this for all controls you want to change. In fact
you can use this.InvokeRequ ired and this.Invoke if the event handler is form
class method
//Since this event handler might be frequently called you may create a
CopyProgressEve ntHandler delegate
in the constructor of the form and reuse it in order not to pollute the
heap with objects each time the event is fired
if(pbStatus.Inv okeRequired)
{
pbStatus.Invoke (new CopyProgressEve ntHandler(fileC opy_CopyProgres s),
new object[]{sender, e});
return;
}
txtResults.Text = e.Message + "\r\n" + e.SourceFolder + "\\" + e.Filename + "\r\n" + txtResults.Text ;

//really need to get or make a three segment scrolling progress bar like the xp startup one

pbStatus.Value = pbStatus.Value == pbStatus.Maximu m ? pbStatus.Minimu m :
pbStatus.Value;

pbStatus.Value ++;

}

private void fileCopy_StartC opy(object sender, EventArgs e)

{ if(pbStatus.Inv okeRequired)
{
pbStatus.Invoke (new EventHandler(fi leCopy_StartCop y), new
object[]{sender, e});
return;
}
pbStatus.Visibl e = true;

}

private void fileCopy_Finish Copy(object sender, EventArgs e)

{ if(pbStatus.Inv okeRequired)
{
pbStatus.Invoke (new EventHandler(fi leCopy_FinishCo py), new
object[]{sender, e});
return;
}
pbStatus.Visibl e = false;

}

}
--
HTH
B\rgds
100
"John Baro" <jo***@NOSPAMme sware.com.au> wrote in message
news:wR******** **********@news-server.bigpond. net.au... Hi Stoitcho

Thanks for the reply
The only references to pbStatus (the progress bar) are below.
I just checked again and it works exactly as I said.
When I have the progress bar hidden to start with, it does not draw properly and when I tried to maximise the form it froze the app and I had to kill it. It is so very odd.
private void fileCopy_CopyPr ogress(object sender, CopyProgressEve ntArgs e)

{

txtResults.Text = e.Message + "\r\n" + e.SourceFolder + "\\" + e.Filename + "\r\n" + txtResults.Text ;

//really need to get or make a three segment scrolling progress bar like the xp startup one

pbStatus.Value = pbStatus.Value == pbStatus.Maximu m ? pbStatus.Minimu m :
pbStatus.Value;

pbStatus.Value ++;

}

private void fileCopy_StartC opy(object sender, EventArgs e)

{

pbStatus.Visibl e = true;

}

private void fileCopy_Finish Copy(object sender, EventArgs e)

{

pbStatus.Visibl e = false;

}

}

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:Ov******** ******@tk2msftn gp13.phx.gbl...
Hi John,
You raise the event from the worker thread. The event handler, which is
method of the form class, is executed by this worker thread. Is it posible
that you don't call Control.Invoke and manipulate the progress bar from the
worker thread. If you do so you can exepect any strange behaviour. You

know
that you can use controls only from the UI thread that has created them.

--
B\rgds
100
"John Baro" <jo***@NOSPAMme sware.com.au> wrote in message
news:o%******** **********@news-server.bigpond. net.au...
Thanks for the answer.

I knew I should have written this when I was awake.

I only spawn 1 thread to copy all files which in turn spawns 1 thread to copy 1 file at a time. This is done so that I can have fault tolerance and cancel the copy if it is hanging on copying a file.
This occurrs when a file is damaged. I wrote this to copy files off a
damaged CD and ignore damaged files.

The most important part..
When the progress bar is set to visible before I start copying files (In the
IDE), then it works perfectly.
No disk thrashing is occurring, guaranteed.
I can email you the project if you want.
JB

"Michael Culley" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:OK******** ******@TK2MSFTN GP12.phx.gbl...
> Maybe you have too many threads copying files at once causing your
hdd
to
thrash. One thread might be better.
>
> --
> Michael Culley
>
>
> "John Baro" <jo***@NOSPAMme sware.com.au> wrote in message
news:V1******** **********@news-server.bigpond. net.au...
> > I have an app that spawns a thread to copy some files.
> > The spawned thread then spawns threads to copy individual files
and wait
for
> > the specific file to finish copying or timeout.
> > The specific file copy procedure raises an event which gets

handled in the
> > main form from where the original thread was spawned which then updates
a
> > status box and a progress bar.
> > The first spawned thread raises a startcopy event which the main form > > handles to display the progress bar and a finishcopy event to hide the > > progress bar.
> > This was all working peachy until I moved the individual file copying
to
> > individual threads. After that It was REALLY slow and the progress

bar would
> > not display.
> > When I set the visibility of the progress bar to TRUE to begin
with it works
> > fine. Even after the progress bar has been hidden by the
finishcopy event
> > and shown again by the startcopy event if I run the routine again.
> >
> > Now for the question.
> > Why is this so?
> >
> > I apologize for the long winded explanation but I really dont

understand
why
> > this is.
> >
> > TIA
> > JB
> >
> > P.S. The main form "Freezes" when the progress bar is not visible to begin
> > with. It wont allow me to resize or do anything whilst the

filecopying is
> > going on, even though it is being run in another thread.
> >
> >
>
>



Nov 15 '05 #9
Thanks Sami
"Sami Vaaraniemi" <sa************ ***@jippii.fi> wrote in message
news:c0******** **@phys-news1.kolumbus. fi...

"John Baro" <jo***@NOSPAMme sware.com.au> wrote in message
news:wR******** **********@news-server.bigpond. net.au...
Hi Stoitcho

Thanks for the reply
The only references to pbStatus (the progress bar) are below.
I just checked again and it works exactly as I said.
When I have the progress bar hidden to start with, it does not draw

properly
and when I tried to maximise the form it froze the app and I had to kill

it.
It is so very odd.


Odd things are bound to happen if you access controls from threads other
than the UI thread. As Stoitcho already suggested, use Control.Invoke to
access methods or properties of a control from other threads. Otherwise it
will never work reliably.

Sami
www.capehill.net

Nov 15 '05 #10

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

Similar topics

2
2920
by: Joona I Palaste | last post by:
AFAIK the C standard divides behaviour of different things into four classes. 1) Defined behaviour. The implementation must do exactly what the standard says. 2) Implementation-defined behaviour. The implementation must choose some behaviour, document it, and be consistent about it. 3) Unspecified behaviour. The implementation must pick one of several possible behaviours, but does not need to be consistent about it. 4) Undefined...
25
3088
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I don't have a copy of ANSI C89 standard,therefore i had to post this question: What is the difference between "unspecified" behaviour & "undefined" behaviour of some C Code ??
31
2611
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
26
2177
by: Frederick Gotham | last post by:
I have a general idea of the different kinds of behaviour described by the C Standard, such as: (1) Well-defined behaviour: int a = 2, b = 3; int c = a + b; (Jist: The code will work perfectly.)
285
8802
by: Sheth Raxit | last post by:
Machine 1 : bash-3.00$ uname -a SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R bash-3.00$ gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/ specs gcc version 2.95.3 20010315 (release)
0
8465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8587
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2792
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2029
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1787
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.