Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 117,608 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 2,441 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Error With Threads...(from Martyr2's Blog)

 
Reply to this topicStart new topic

Error With Threads...(from Martyr2's Blog), ...working from Martyr2's blog...

Footsie
post 3 Jul, 2008 - 08:29 AM
Post #1


D.I.C Regular

Group Icon
Joined: 20 Sep, 2007
Posts: 283



Thanked 2 times

Dream Kudos: 50
My Contributions


I've been reading and working through the following blog by Martyr2:
Threads, ProgressBars

After coding in all the different pieces I build and run the project to see it in action but I get the following error on the line 87progressBar1.Invoke(new updateBar(this.UpdateProgress));

ERROR: Message="Input string was not in a correct format."

csharp

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;


namespace ThreadPractice
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{

ThreadStart theProgress = new ThreadStart(ReadFile);

Thread startProgress = new Thread(theProgress);
startProgress.Name = "Update progress bar";
startProgress.Start();
}

public delegate void updateBar();

// This will run in the main thread so it can update the controls for us.
private void UpdateProgress()
{
progressBar1.Value += 1;

// Here we are just updating a label to show the progressbar value
label1.Text = Convert.ToString(Convert.ToInt64(label1.Text) + 1);
}

// This would be our version of main() for the new thread.
// We start reading a huge file with 17000+ lines in it.
private void ReadFile()
{
String bigFile = @"c:\\bigfile.txt";

// Lets get the length so that when we are reading we know
// when we have hit a "milestone" and to update the progress bar.
FileInfo fileSize = new FileInfo(bigFile);
long size = fileSize.Length;

// Next we need to know where we are at and what defines a milestone in the
// progress. So take the size of the file and divide it into 100 milestones
// (which will match our 100 marks on the progress bar.

long currentSize = 0;
long incrementSize = (size / 100);

// Open the big text file with open filemode access.
StreamReader stream = new StreamReader(new FileStream(bigFile, FileMode.Open));

// This buffer is only 10 characters long so we process the file in 10 char chunks.
// We could have boosted this up, but we want a slow process to show the slow progress.
char[] buff = new char[10];

// Read through the file until end of file
while (!stream.EndOfStream)
{
// Add to the current position in the file
currentSize += stream.Read(buff,0,buff.Length);

// Once we hit a milestone, subtract the milestone value and
// call our delegate we defined above.
// We must do this through invoke since progressbar was defined in the other
// thread.
if (currentSize >= incrementSize)
{
currentSize -= incrementSize;

//THE ERROR IS ON THE NEXT LINE
progressBar1.Invoke(new updateBar(this.UpdateProgress));
}
}

// Close the stream and show we are done.
// At the end of this ends the run of our thread.
stream.Close();
MessageBox.Show("Done");
}
}

}


I can't work out why.
Can someone help me?

This post has been edited by Footsie: 3 Jul, 2008 - 08:42 AM
User is offlineProfile CardPM

Go to the top of the page


zakary
post 3 Jul, 2008 - 12:20 PM
Post #2


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 382



Thanked 4 times

Dream Kudos: 175
My Contributions


Footsie this should fix your issue
csharp

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading; // was missing
using System.IO; // was missing

namespace WindowsApplication1
{
public partial class Form1 : Form
{
//added
public delegate void UpdateBarEvent(long channel);
//added
UpdateBarEvent updateBarEvent;
public Form1()
{
InitializeComponent();
updateBarEvent = new UpdateBarEvent(UpdateProgress);
label1.Text = "0";
}

private void Form1_Load(object sender, EventArgs e)
{

}
private void button1_Click(object sender, EventArgs e)
{
ThreadStart theProgress = new ThreadStart(ReadFile);

Thread startProgress = new Thread(theProgress);
startProgress.Name = "Update progress bar";
startProgress.Start();
}

// This will run in the main thread so it can update the controls for us.
private void UpdateProgress(long value)
{
//added
if (this.InvokeRequired)
{
this.progressBar1.Invoke(new UpdateBarEvent(UpdateProgress), new object[] { value });
}
else
{
this.progressBar1.Value = Convert.ToInt32(value);
// Here we are just updating a label to show the progressbar value
label1.Text = Convert.ToString(Convert.ToInt64(label1.Text) + 10);
}
}

// This would be our version of main() for the new thread.
// We start reading a huge file with 17000+ lines in it.
private void ReadFile()
{
String bigFile = @"c:\\bigfile.txt";

// Lets get the length so that when we are reading we know
// when we have hit a "milestone" and to update the progress bar.
FileInfo fileSize = new FileInfo(bigFile);
long size = fileSize.Length;

// Next we need to know where we are at and what defines a milestone in the
// progress. So take the size of the file and divide it into 100 milestones
// (which will match our 100 marks on the progress bar.

long currentSize = 0;
long maxSize = (size / 100);

// Open the big text file with open filemode access.
StreamReader stream = new StreamReader(new FileStream(bigFile, FileMode.Open));

// This buffer is only 10 characters long so we process the file in 10 char chunks.
// We could have boosted this up, but we want a slow process to show the slow progress.
char[] buff = new char[10];

// Read through the file until end of file
while (!stream.EndOfStream)
{
// Add to the current position in the file
currentSize += stream.Read(buff,0,buff.Length);

// Once we hit a milestone, subtract the milestone value and
// call our delegate we defined above.
// We must do this through invoke since progressbar was defined in the other
// thread.
if (currentSize <= maxSize)
{
maxSize -= currentSize;

//added
if (this.progressBar1.InvokeRequired)
{
this.progressBar1.Invoke(updateBarEvent, new object[] { currentSize });
}
else
{
progressBar1.Value = Convert.ToInt32(currentSize);
}
}
}

// Close the stream and show we are done.
// At the end of this ends the run of our thread.
stream.Close();
MessageBox.Show("Done");
}
}
}


but you may want to play with the progressbar levels so that they are more accurate

This post has been edited by zakary: 3 Jul, 2008 - 12:21 PM
User is offlineProfile CardPM

Go to the top of the page

Footsie
post 5 Jul, 2008 - 01:34 AM
Post #3


D.I.C Regular

Group Icon
Joined: 20 Sep, 2007
Posts: 283



Thanked 2 times

Dream Kudos: 50
My Contributions


Thanks for the post zakary.
But I'm still having issues when I added your code.
I've also tried creating a new project and copy and pasted your code into it. All I get is the form with button and progressbar but nothing happens when I click the button.

I think I'll try and redo the project from scratch.
User is offlineProfile CardPM

Go to the top of the page

zakary
post 7 Jul, 2008 - 04:40 AM
Post #4


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 382



Thanked 4 times

Dream Kudos: 175
My Contributions


How big is the file you are using. mine was 14KB, so i was able to see it working. if your file is smaller you may not see anything
User is offlineProfile CardPM

Go to the top of the page

Footsie
post 7 Jul, 2008 - 12:19 PM
Post #5


D.I.C Regular

Group Icon
Joined: 20 Sep, 2007
Posts: 283



Thanked 2 times

Dream Kudos: 50
My Contributions


zakary - My txt file is 327kb.

But after bumping my head a few times I think I found the error:
The error was actually in the method UpdateProgress().

In my original post,
Line 44:
CODE
label1.Text = Convert.ToString(Convert.ToInt64(label1.Text) + 1);  


Should read:
CODE
label1.Text = Convert.ToString(Convert.ToInt64(progressBar.Value) + 1);  


The original code was trying to update the label.Text value using the label.Text value and causing a circle and therefore the error.

The progress bar now runs right through and the label counts up but goes to 101. It should stop at 100. This is how I'm displaying only to 100.
csharp

// This will run in the main thread so it can update the controls for us.
private void UpdateProgress()
{
progressBar1.Value += 1;

// Here we are just updating a label to show the progressbar value
if (progressBar1.Value < 100)
{
label1.Text = Convert.ToString(Convert.ToInt64(progressBar1.Value) + 1);
}
}


Is there a better way?
User is offlineProfile CardPM

Go to the top of the page

zakary
post 7 Jul, 2008 - 12:59 PM
Post #6


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 382



Thanked 4 times

Dream Kudos: 175
My Contributions


yeah use a percent complete for you it would be
csharp

long complete = (totalSize/currentSize)*100;

totalSize is the size of the file. currentSize would have to be where you are at in the file not what you have it defined as. Then when you invoke your progressbar update you pass in complete.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/7/08 11:36PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month