|
I had a look at your original post, and I think it will make it easier for you if you change your design a bit.
If I uderstand correctly, you want to show your main form, and then show a splash screen right on top of it with a progress bar showing the advancement of some intialization work carried out by a background thread.
Try this way:
- Use the standard Main() for a Windows Form application, which will just run the app showing the main form (Form1 ).
- In the Shown() event handler for Form1 , show the splash screen (Form2 ), create your thread, link its events to functions in Form2 and start the thread.
- When you get an event from the thread, use Invoke() to update the progress bar. As others told you, this is the best way to do that.
- If you feel unsure about Invoke() , set up a thread-safe (using lock ) property in Form2 and update it from the events handlers, then have a timer check it and update your progress bar. This is not good design, but it will work.
I hope I got your problem correctly, and hope this can help you.
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
This would be a producer-consumer problem[^] it's really common so I'm sure there are lots of articles about it on CP.
Often implemented with a non-binary semaphore and a queue (or 2 semaphores and a circular buffer)
|
|
|
|
|
If thread2 is a UI thread, all you need to do is call Invoke/BeginInvoke on one of the controls created on that thread. Otherwise, the typical way is to share a queue between the two threads, and have the second thread watch the queue and process commands as they come in.
|
|
|
|
|
hi
my project is like this..
basing on the inputs given by user, i should generate a Drawing in Autocad
my problem is ...
client has so many Autocad versions
example: Autocad 2004,2005 ,2006,2007,2008,2009
and his requirement is like this ..
if the Current active Document belongs to Autocad 2006 then the drawing should be generated in that version apllication
what the client is saying is ..
suppose he opens Autocad 2006 application and runs the project then the drawing should be generated in Autocad 2006 application only
please help me
thanks in advance
vijay kumar d
|
|
|
|
|
Hi, i am using mysql connector for my C# project.
I want to retrieve all the values from the database where username = "testing"
Database.cs
public MySqlDataReader getAccountDetails(String username)<br />
{<br />
Connect();<br />
command = conn.CreateCommand();<br />
command.CommandText = "Select * from accounts where admin_num ='" + username + "'";<br />
reader = command.ExecuteReader();<br />
Close();<br />
return reader;<br />
}
frmMain.cs
private void frmMain_Load(object sender, EventArgs e)<br />
{<br />
toolStripDateLabel.Text = DateTime.Now.ToString();<br />
db.Connect();<br />
MySqlDataReader reader = db.getAccountDetails(username.ToString());<br />
<br />
while (reader.Read())<br />
{<br />
string thisrow = "";<br />
for (int i = 0; i < reader.FieldCount; i++)<br />
thisrow += reader.GetValue(i).ToString() + ",";<br />
listBox1.Items.Add(thisrow);<br />
}<br />
db.Close();<br />
}<br />
it gives me an error "Invalid attempt to Read when reader is closed."
|
|
|
|
|
|
benjamin yap wrote: public MySqlDataReader getAccountDetails(String username)
{
Connect();
command = conn.CreateCommand();
command.CommandText = "Select * from accounts where admin_num ='" + username + "'";
reader = command.ExecuteReader();
Close();
return reader;
}
You close the database connection after running ExecuteReader() , that's your problem. When you close the connection, the associated reader is closed too.
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
|
hmm how do i retrieve the value from the reader
I want to specify the column name instead of the row number
reader.GetValue("username");
something like this
|
|
|
|
|
You can use Reader["ColumnName"] - it will return an object, so you might need to convert it to the specific data type. For example:
double var = 0;
if (Reader["MyField"] != DBNull.Value)
{
var = Convert.ToDouble(Reader["MyField"]);
}
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
Open Internet Explorer(IE) with about:blank as its home page
The links which i will traverse are
1.http://www.google.co.in
2.http://www.msn.com/
3.http://www.yahoo.com
Then If I press the BACK button(i.e.,navigation toolbar), http://www.msn.com/ is opened on IE, but instead of http://www.msn.com/ I want the link http://www.google.co.in to be opened. How can I do this in c#?
I mean when we click the back button once, I want the the back button operation sholud be performed twice. How to do this.
(OR)
How can I make the IE to directly traverse from http://www.yahoo.com to http://www.google.co.in in one click of back button?
|
|
|
|
|
Why are you trying to do this? In line with your other post earlier on today, it looks like you are trying to implement something for malicious purposes. I, for one, will not help write code that is destined to act in an inappropriate manner.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Hi all,
Within my application I need the ability to copy a file. The thing is I also want to use a progress bar to show how much of the file is uploaded/total etc.
So this rules out the use of System.IO.File.Copy() - Of course, I know, I will use a stream and write a chuck of data at a time and then report progress in-between each chuck... Yeah, that seems to work great...
PROBLEM: I used this method to copy a Word Document (Office 2007). The copy was fine, but the problem is, when I try to open the document in Word its says something about the file being corrupt, would you like to restore the file? (which succeeds, but is not good enough)
If I use File.Copy() this problem does not happen.
So, question is, how do I create a proper copy of a file while maintaining the ability to notify the user of the progress?
Thanks for any help
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
You read the file into a byte array using a StreamReader of some description, and write it out using a StreamWriter. Use a BackgroundWorker to run this in, and raise progress changes at appropriate intervals.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
As I have said I have tried with streams (StreamReader, FileStream and BinaryStream) and this is causing the file to become corrupt - when word tries to open it, it has to restore it.
This does not seem to happen when doing other file types. I can only assume that there is some 'additional info' stored with the file that File.Copy() is capable of handling, but streams are not.
Any thoughts?
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
 here is a code snippet that I use
public static void Copy(string sourcePath, string destPath, bool overwrite, int bufferSize, ProgressBar tmp_pb)
{
if (!overwrite && File.Exists(destPath))
throw new ArgumentException("File already exists.");
if (!Directory.Exists(Path.GetDirectoryName(destPath)))
Directory.CreateDirectory(Path.GetDirectoryName(destPath));
FileStream tmp_fr = new FileStream(sourcePath, FileMode.Open);
FileStream tmp_fw = new FileStream(destPath, FileMode.Create);
long fileSize = new FileInfo(sourcePath).Length;
if (fileSize < bufferSize)
bufferSize = (int)fileSize;
long numOfLoops = (fileSize / bufferSize);
tmp_pb.Step = tmp_pb.Maximum / (int)numOfLoops;
byte[] tmp_data;
for (int a = 0; a < numOfLoops; a++)
{
tmp_data = new byte[bufferSize];
tmp_fr.Read(tmp_data, 0, bufferSize);
tmp_fw.Write(tmp_data, 0, bufferSize);
tmp_pb.PerformStep();
Application.DoEvents();
}
int leftBytes = (int)(fileSize - tmp_fr.Position);
if (leftBytes > 0)
{
tmp_data = new byte[leftBytes];
tmp_fr.Read(tmp_data, 0, leftBytes);
tmp_fw.Write(tmp_data, 0, leftBytes);
}
tmp_fr.Close();
tmp_fw.Flush();
tmp_fw.Close();
tmp_pb.Value = 0;
}
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
My code is basically the same (just different layout, variables etc.)
Are you able to test that with a Office 2007 word document, and then try to to open the file after the copy? - would really appreciate that if you can thanks
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
dude, its a working code...it can even copy entire office Its just a byte game.
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
It's all good now, small mistake on my part. Thanks for your time anyway
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
SharpZipLib contains Core.StreamUtils.Copy(some args including progress callback)
You could look how it works (it's quite short) and do something that looks like it..
When copying blocks did you use the right length and did you flush the output stream? I'm sure you did, but, just checking.. forgetting one or both can result in corruption
|
|
|
|
|
Not using flush (don't think I need with code I'm using) but am sure the length is correct. Here's a snippet...
StreamReader sr = new StreamReader("inputfile.docx");
StreamWriter sw = new StreamWriter("outputfile.docx", false);
FileInfo fi = new FileInfo("inputfile.docx");
long total = fi.Length;
byte[] buffer = new byte[10000];
long byteCount = 0;
while(true)
{
int bytesRead = sr.BaseStream.Read(buffer, 0, buffer.Length);
byteCount += bytesRead;
if(bytesRead == 0)
break;
sw.BaseStream.Write(buffer, 0, buffer.Length);
}
sr.Close();
sw.Close();
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
musefan wrote: sw.BaseStream.Write(buffer, 0, buffer.Length);
There's the mistake, see it? You must write the number of bytes read to correctly handle a partially filled buffer.
Alan.
modified on Wednesday, June 3, 2009 7:10 AM
|
|
|
|
|
nice spot, thanks.
I will give it a go but it make perfect sense so I cant see it not working. Just for any others I am going to change that line to...
sw.BaseStream.Write(buffer, 0, bytesRead);
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Shouldn't that be: (untested and I give no guarantee that I'm even making sense)
StreamReader sr = new StreamReader("inputfile.docx");
StreamWriter sw = new StreamWriter("outputfile.docx", false);
FileInfo fi = new FileInfo("inputfile.docx");
long total = fi.Length;
byte[] buffer = new byte[10000];
long byteCount = 0;
while(true)
{
int bytesRead = sr.BaseStream.Read(buffer, 0, buffer.Length);
byteCount += bytesRead;
if(bytesRead == 0)
break;
sw.BaseStream.Write(buffer, 0, bytesRead);
}
sr.Close();
sw.Close(); Last modified: 14mins after originally posted -- changed to answer
|
|
|
|
|
Yeah, Alan has just said too. But thanks for the response. Rookie mistake really, but sometimes it happens. The problem is that it appeared to work fine with other files so I didn't notice there was an error. It was just because Microsoft Word actually noticed crap at the end of the file that pointed it out. So hey, thanks to Microsoft too
Oh, can either you out Alan change the post to be an 'answer' so I can mark as answer please.
Thanks again, to everyone who took an interest
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|