Emuforums.com

Go Back   Emuforums.com > General Discussion > Web development / Programming
About Us Register FAQ Members List Calendar Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old July 17th, 2008   #1 (permalink)
UAC
 
S.T.A.R.S.'s Avatar
 
Join Date: Jan 2006
Location: Planet Earth...
Posts: 362
Unhappy C# code BLOCKS the program...

Hey I was just developing the example application.What I wanted to do is when the button is clicked,the file is copied from one location to another and that there is also the text box and progress bar which will be showing the COPYING progress.Now...the copying function is working just fine,but the weird thing is that the progress bar and text box control start to work only WHEN copying is finished.LOL!!!I noticed that the line of code which is copying the file blocks the rest of the code until copying is finished.I want to say if you have for example written 4 lines of code,if the first line of code is for that COPYING function,all other 3 lines are executed ONLY when COPYING is complete.So not all 4 lines are executed immediately.The line for COPYING is executing all the time until copying finishes.And THEN all the rest executes.Now this is representing me a big problem because it's silly that the progress bar starts to work WHEN copying is finished...ROFL!!!
Here is the code that I was using for that COPY button.I was also using a timer for progress bar:

privatevoid button1_Click(object sender,EventArgs e)
{
System.IO.
File.Copy("C:\\Documents and Settings\\S.T.A.R.S.---UAC\\Desktop\\Video.avi","C:\\Video.avi",true);
progressBar1.Minimum=0;
progressBar1.Step=10;
progressBar1.Maximum=100;
progressBar1.Value=0;
timer1.Start();
}
privatevoid timer1_Elapsed(object sender,System.Timers.ElapsedEventArgs e)
{
progressBar1.PerformStep();
if(progressBar1.Value==100)
{
timer1.Stop();
textBox1.Text=
"Copying finished!";
}
if(progressBar1.Value!=100)
{
textBox1.Text=
"Copying...";
}
}


PLEASE IF ANYONE KNOWS HOW TO SOLVE THIS ISSUE,I WOULD BE GRATEFUL IF YOU TELL ME...

Thanks in advance!

__________________
Standard programmer in Microsoft Visual Studio 2005 C# Express Edition...
(GO PCSX2 TEAM!!!GO AND MAKE THE BEST PLAYSTATION 2 EMULATOR!!!YOU CAN DO IT!!!)
S.T.A.R.S. is offline   Reply With Quote
Old July 17th, 2008   #2 (permalink)
Emulation to the max!
 
Coolsvilleman's Avatar
 
Join Date: Jan 2004
Location: Canada
Posts: 2,413
Sounds like a threading issue to me. It most likely waits for that System.IO.File.Copy operation to finish before it continues on with the reset of the code. You need to use some kind of design pattern like MVC or something. Basically have two independent threads. One which updates the GUI every second or so (The view 'V') and then the actual copying (The controller 'C'). The view can pole the controller on its status every few seconds. The Model 'M' part probably doesn't do much in this scenario.

Edit:
Another thing to take into account. If you use the system copy function your in good hands because 9 out of 10 times it uses DMA which is much faster than PIO (Programmed Input/Ouput or something along those lines). BUT! if you decided to write your own function there is a good chance you will be running it through the processor turning it into PIO mode. Something to keep in mind if you decide to write your own copy function.
__________________
Q6600 Core 2 Quad || Soundblaster X-Fi Extreme HD || e-VGA 8800GTX || 4096mb 4-4-4-12-2T Dual-Channel RAM || MSI P6N Diamond || 2x 320GB Hardrive || 5.1 Speakers || Remote control || 8x USB 2.0 || DVD/CD ±RW Drive || 1x IEEE1394

www.coolsvilleman.homedns.org Normally down. E-mail me for a DVD @ coolsvilleman@gmail.com
Coolsvilleman is offline   Reply With Quote
Old July 17th, 2008   #3 (permalink)
UAC
 
S.T.A.R.S.'s Avatar
 
Join Date: Jan 2006
Location: Planet Earth...
Posts: 362
Man that sucks lol.C# code for copying file pauses the rest execution...
Can you give me any example for resolving this? If it's possible lol...
__________________
Standard programmer in Microsoft Visual Studio 2005 C# Express Edition...
(GO PCSX2 TEAM!!!GO AND MAKE THE BEST PLAYSTATION 2 EMULATOR!!!YOU CAN DO IT!!!)
S.T.A.R.S. is offline   Reply With Quote
Old July 17th, 2008   #4 (permalink)
Emulation to the max!
 
Coolsvilleman's Avatar
 
Join Date: Jan 2004
Location: Canada
Posts: 2,413
Quote:
Originally Posted by S.T.A.R.S. View Post
Man that sucks lol.C# code for copying file pauses the rest execution...
Can you give me any example for resolving this? If it's possible lol...
Well, first I would double check my "Theory". Use a debugger to trace through how the code gets executed. It could always be another problem. I will try to get an example for you in a bit.
__________________
Q6600 Core 2 Quad || Soundblaster X-Fi Extreme HD || e-VGA 8800GTX || 4096mb 4-4-4-12-2T Dual-Channel RAM || MSI P6N Diamond || 2x 320GB Hardrive || 5.1 Speakers || Remote control || 8x USB 2.0 || DVD/CD ±RW Drive || 1x IEEE1394

www.coolsvilleman.homedns.org Normally down. E-mail me for a DVD @ coolsvilleman@gmail.com
Coolsvilleman is offline   Reply With Quote
Old July 17th, 2008   #5 (permalink)
Moderator
 
Shendo's Avatar
 
Join Date: Feb 2006
Location: Croatia
Posts: 3,897
Hmm, I believe Application.DoEvents(); might do the trick.
Also, just a few advices: use tab to make your code cleaner and use else instead of two ifs.
__________________
Shendo is offline   Reply With Quote
Old July 17th, 2008   #6 (permalink)
UAC
 
S.T.A.R.S.'s Avatar
 
Join Date: Jan 2006
Location: Planet Earth...
Posts: 362
Quote:
Originally Posted by Shendo View Post
Hmm, I believe Application.DoEvents(); might do the trick.
Also, just a few advices: use tab to make your code cleaner and use else instead of two ifs.
I tryed "Application.DoEvents();" yesterday...still nothing.
__________________
Standard programmer in Microsoft Visual Studio 2005 C# Express Edition...
(GO PCSX2 TEAM!!!GO AND MAKE THE BEST PLAYSTATION 2 EMULATOR!!!YOU CAN DO IT!!!)
S.T.A.R.S. is offline   Reply With Quote
Old July 17th, 2008   #7 (permalink)
Emu author
 
@ruantec's Avatar
 
Join Date: Nov 2002
Location: Austria (originally from the most beautiful island of the caribbean "Dominican Republic")
Posts: 1,428
Setting the progressbar.max value to a fixed number such as 100 isn´t a good idea... you have to set the maximum value to the file lengh for example in order to read the bytes written in the other location and set the current status in the progressbar.

while using .doevents() will probably do the trick but you could maybe have some flashy problems.. i recommend you to use a thread() and create a multi-threaded code that will do the trick as it should.

UPDATE:
System.IO.File.Copy will just copy the file to another location but as you probably guessed the operation will be done and after that the rest of the code will be executed.. i recommend you to do it yourself by using the io.stream methods..

open the file to be copied as a stream and save it to another location manually in that way you will get control over the bytes written in the other location.. by setting the progressbar to the file lengh you will be able to set the progressbar value or the current byte written anytime... and using .doevents() or a threading method you will be able to see the bar moving

btw if you mark the code before it starts you can go through the code by pressing F10 and you can see what´s beeing executed. it´s very helpfull if you want to see or debug your code.

sadly i have to go sleep now otherwise i would code that for you
__________________

Current development tools:

Visual C++.net, Visual C#.net
Visual VB.net, Visual Webdeveloper.net
Bloodshed Dev C++, Borland C++
Visual Basic 6

Last edited by @ruantec; July 17th, 2008 at 22:53.
@ruantec is offline   Reply With Quote
Old July 17th, 2008   #8 (permalink)
Emulation to the max!
 
Coolsvilleman's Avatar
 
Join Date: Jan 2004
Location: Canada
Posts: 2,413
Quote:
Originally Posted by @ruantec View Post
UPDATE:
System.IO.File.Copy will just copy the file to another location but as you probably guessed the operation will be done and after that the rest of the code will be executed.. i recommend you to do it yourself by using the io.stream methods..
Wont that use PIO mode then.

Update: Almost done example.
__________________
Q6600 Core 2 Quad || Soundblaster X-Fi Extreme HD || e-VGA 8800GTX || 4096mb 4-4-4-12-2T Dual-Channel RAM || MSI P6N Diamond || 2x 320GB Hardrive || 5.1 Speakers || Remote control || 8x USB 2.0 || DVD/CD ±RW Drive || 1x IEEE1394

www.coolsvilleman.homedns.org Normally down. E-mail me for a DVD @ coolsvilleman@gmail.com
Coolsvilleman is offline   Reply With Quote
Old July 17th, 2008   #9 (permalink)
Emu author
 
@ruantec's Avatar
 
Join Date: Nov 2002
Location: Austria (originally from the most beautiful island of the caribbean "Dominican Republic")
Posts: 1,428
Quote:
Originally Posted by Coolsvilleman View Post
Wont that use PIO mode then.

Update: Almost done example.
PIO mode???.... btw a simple binary reader and writter should do the job

anyways am going to bed now hope my info helped him a bit
__________________

Current development tools:

Visual C++.net, Visual C#.net
Visual VB.net, Visual Webdeveloper.net
Bloodshed Dev C++, Borland C++
Visual Basic 6
@ruantec is offline   Reply With Quote
Old July 17th, 2008   #10 (permalink)
Emulation to the max!
 
Coolsvilleman's Avatar
 
Join Date: Jan 2004
Location: Canada
Posts: 2,413
Quote:
Originally Posted by @ruantec View Post
PIO mode???.... btw a simple binary reader and writter should do the job

anyways am going to bed now hope my info helped him a bit
Best look up PIO and DMA on wikipedia people.
__________________
Q6600 Core 2 Quad || Soundblaster X-Fi Extreme HD || e-VGA 8800GTX || 4096mb 4-4-4-12-2T Dual-Channel RAM || MSI P6N Diamond || 2x 320GB Hardrive || 5.1 Speakers || Remote control || 8x USB 2.0 || DVD/CD ±RW Drive || 1x IEEE1394

www.coolsvilleman.homedns.org Normally down. E-mail me for a DVD @ coolsvilleman@gmail.com
Coolsvilleman is offline   Reply With Quote
Old July 17th, 2008   #11 (permalink)
Emu author
 
@ruantec's Avatar
 
Join Date: Nov 2002
Location: Austria (originally from the most beautiful island of the caribbean "Dominican Republic")
Posts: 1,428
Quote:
Originally Posted by Coolsvilleman View Post
Best look up PIO and DMA on wikipedia people.
i know but i just don´t get the point of what you mean???
__________________

Current development tools:

Visual C++.net, Visual C#.net
Visual VB.net, Visual Webdeveloper.net
Bloodshed Dev C++, Borland C++
Visual Basic 6
@ruantec is offline   Reply With Quote
Old July 17th, 2008   #12 (permalink)
Emulation to the max!
 
Coolsvilleman's Avatar
 
Join Date: Jan 2004
Location: Canada
Posts: 2,413
Ok I was adapting some code from here:

C# Backgroundworker Filecopy with progressbar feedback - MSDN Forums

Its not 100% done yet (ok ya the progress bar dosent work yet) but it should give you a better idea of what to do.
Attached Files
File Type: zip AwesomeTestApp.zip (39.6 KB, 1 views)
__________________
Q6600 Core 2 Quad || Soundblaster X-Fi Extreme HD || e-VGA 8800GTX || 4096mb 4-4-4-12-2T Dual-Channel RAM || MSI P6N Diamond || 2x 320GB Hardrive || 5.1 Speakers || Remote control || 8x USB 2.0 || DVD/CD ±RW Drive || 1x IEEE1394

www.coolsvilleman.homedns.org Normally down. E-mail me for a DVD @ coolsvilleman@gmail.com
Coolsvilleman is offline   Reply With Quote
Old July 17th, 2008   #13 (permalink)
Emulation to the max!
 
Coolsvilleman's Avatar
 
Join Date: Jan 2004
Location: Canada
Posts: 2,413
Quote:
Originally Posted by @ruantec View Post
i know but i just don´t get the point of what you mean???
What I mean is if you run it through a stream and then I am assuming you want to write the stream to a new file. Thats PIO mode is it not. Since all that data is going through the CPU and not directly from the hard-disk to memory or hard-disk to memory to hard-disk in our case.

And what happened to auto-merging?
__________________
Q6600 Core 2 Quad || Soundblaster X-Fi Extreme HD || e-VGA 8800GTX || 4096mb 4-4-4-12-2T Dual-Channel RAM || MSI P6N Diamond || 2x 320GB Hardrive || 5.1 Speakers || Remote control || 8x USB 2.0 || DVD/CD ±RW Drive || 1x IEEE1394

www.coolsvilleman.homedns.org Normally down. E-mail me for a DVD @ coolsvilleman@gmail.com

Last edited by Coolsvilleman; July 18th, 2008 at 00:10.
Coolsvilleman is offline   Reply With Quote
Old July 18th, 2008   #14 (permalink)
Emu author
 
@ruantec's Avatar
 
Join Date: Nov 2002
Location: Austria (originally from the most beautiful island of the caribbean "Dominican Republic")
Posts: 1,428
the idea i was talking about is to open both streams input and ouput and while reading the file you can store those bytes in the output file..

anyways i got your point now... sometimes(if not always) my english is pretty bad
__________________

Current development tools:

Visual C++.net, Visual C#.net
Visual VB.net, Visual Webdeveloper.net
Bloodshed Dev C++, Borland C++
Visual Basic 6
@ruantec is offline   Reply With Quote
Old July 18th, 2008   #15 (permalink)
UAC
 
S.T.A.R.S.'s Avatar
 
Join Date: Jan 2006
Location: Planet Earth...
Posts: 362
I have sent a message to www.learnvisualstudio.net few hours ago and asked them if they can make a video tutorial for this problem.I hope they will answer...

Ok I have just used the backgroundWorker control and the code for COPYING is not blocking the application while copying Progress bar also works immediately,but he already completes,while the copying is still in progress lol.I used the "backgroundWorker1.RunWorkerAsync();" and the code for COPYING I put in "backgroundWorker1_DoWork" event.I will try to put the progress bar code in the events of that background worker and see what happenes.Wish me luck lol


Quote:
Originally Posted by Coolsvilleman View Post
Ok I was adapting some code from here:

C# Backgroundworker Filecopy with progressbar feedback - MSDN Forums

Its not 100% done yet (ok ya the progress bar dosent work yet) but it should give you a better idea of what to do.
I FINALLY MADE IT!!! This code example you gave me didn't really help.But I used the background worker to run the COPYING process in the background and I had no problems with the progress bar anymore.I made that the timer control repeats the progress bar process all until copying is finished.Here is the code I wrote and it really works!!!!:

privatevoid button1_Click(object sender,EventArgs e)
{
textBox1.Text=
"Copying file...";
progressBar1.Minimum=0;
progressBar1.Step=10;
progressBar1.Maximum=100;
progressBar1.Value=0;
timer1.Start();
backgroundWorker1.RunWorkerAsync();
}
privatevoid timer1_Elapsed(object sender,System.Timers.ElapsedEventArgs e)
{
if(progressBar1.Value!=100)
{
progressBar1.PerformStep();
}
if(progressBar1.Value==100)
{
progressBar1.Value=0;
progressBar1.PerformStep();
}
}
privatevoid backgroundWorker1_DoWork(object sender,DoWorkEventArgs e)
{
System.IO.
File.Copy("C:\\Documents and Settings\\S.T.A.R.S.---UAC\\Desktop\\Video.avi","C:\\Video.avi",true);
}
privatevoid backgroundWorker1_ProgressChanged(object sender,ProgressChangedEventArgs e)
{
//...
}
privatevoid backgroundWorker1_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
timer1.Stop();
textBox1.Text=
"Copying finished!";
progressBar1.Value=100;
}

I hope this will help to others also! Thanks for the help guys
__________________
Standard programmer in Microsoft Visual Studio 2005 C# Express Edition...
(GO PCSX2 TEAM!!!GO AND MAKE THE BEST PLAYSTATION 2 EMULATOR!!!YOU CAN DO IT!!!)

Last edited by S.T.A.R.S.; July 18th, 2008 at 16:28. Reason: Automerged Doublepost
S.T.A.R.S. is offline   Reply With Quote
Old July 19th, 2008   #16 (permalink)
Emulation to the max!
 
Coolsvilleman's Avatar
 
Join Date: Jan 2004
Location: Canada
Posts: 2,413
Quote:
Originally Posted by S.T.A.R.S. View Post
I have sent a message to www.learnvisualstudio.net few hours ago and asked them if they can make a video tutorial for this problem.I hope they will answer...

Ok I have just used the backgroundWorker control and the code for COPYING is not blocking the application while copying Progress bar also works immediately,but he already completes,while the copying is still in progress lol.I used the "backgroundWorker1.RunWorkerAsync();" and the code for COPYING I put in "backgroundWorker1_DoWork" event.I will try to put the progress bar code in the events of that background worker and see what happenes.Wish me luck lol


I FINALLY MADE IT!!! This code example you gave me didn't really help.But I used the background worker to run the COPYING process in the background and I had no problems with the progress bar anymore.I made that the timer control repeats the progress bar process all until copying is finished.Here is the code I wrote and it really works!!!!:

privatevoid button1_Click(object sender,EventArgs e)
{
textBox1.Text=
"Copying file...";
progressBar1.Minimum=0;
progressBar1.Step=10;
progressBar1.Maximum=100;
progressBar1.Value=0;
timer1.Start();
backgroundWorker1.RunWorkerAsync();
}
privatevoid timer1_Elapsed(object sender,System.Timers.ElapsedEventArgs e)
{
if(progressBar1.Value!=100)
{
progressBar1.PerformStep();
}
if(progressBar1.Value==100)
{
progressBar1.Value=0;
progressBar1.PerformStep();
}
}
privatevoid backgroundWorker1_DoWork(object sender,DoWorkEventArgs e)
{
System.IO.
File.Copy("C:\\Documents and Settings\\S.T.A.R.S.---UAC\\Desktop\\Video.avi","C:\\Video.avi",true);
}
privatevoid backgroundWorker1_ProgressChanged(object sender,ProgressChangedEventArgs e)
{
//...
}
privatevoid backgroundWorker1_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
timer1.Stop();
textBox1.Text=
"Copying finished!";
progressBar1.Value=100;
}

I hope this will help to others also! Thanks for the help guys

Nice. Sorry my code was a bit crappy .
__________________
Q6600 Core 2 Quad || Soundblaster X-Fi Extreme HD || e-VGA 8800GTX || 4096mb 4-4-4-12-2T Dual-Channel RAM || MSI P6N Diamond || 2x 320GB Hardrive || 5.1 Speakers || Remote control || 8x USB 2.0 || DVD/CD ±RW Drive || 1x IEEE1394

www.coolsvilleman.homedns.org Normally down. E-mail me for a DVD @ coolsvilleman@gmail.com
Coolsvilleman is offline   Reply With Quote
Old July 19th, 2008   #17 (permalink)
UAC
 
S.T.A.R.S.'s Avatar
 
Join Date: Jan 2006
Location: Planet Earth...
Posts: 362
You neeed to see my crappy code when I started learning C# 3 years ago... xD xD xD
__________________
Standard programmer in Microsoft Visual Studio 2005 C# Express Edition...
(GO PCSX2 TEAM!!!GO AND MAKE THE BEST PLAYSTATION 2 EMULATOR!!!YOU CAN DO IT!!!)
S.T.A.R.S. is offline   Reply With Quote
Old July 20th, 2008   #18 (permalink)
Emulation to the max!
 
Coolsvilleman's Avatar
 
Join Date: Jan 2004
Location: Canada
Posts: 2,413
So does your program update the