473,383 Members | 1,870 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,383 software developers and data experts.

Need help with a threading issue

Hey guys,

I'm trying to read a file and add some nodes to a treee view control. I
would like the GUI to remain interactive and have a progress bar and text box
be updated with info as the file is read.

I'm using a threadpool thread and reading the file from there. However, I
seem to be running into some problems. Sometimes the GUi seems to be
updating/repainting fine but than all of a sudden stops responding. Also, if
the open file dialog is on top off the form, it doesn't repaint at all.

Below is the code I am using. Any help is greatly appreciated.

==============================================

delegate void UpdateProgressBarCallback(decimal percentDone);
delegate void UpdateFlexbarTree(string line);

private void _loadToolStripMenuItem_Click(object sender, EventArgs e)
{
Stream fileStream = null;
OpenFileDialog openFileDialog = new OpenFileDialog();

openFileDialog.InitialDirectory =
Path.GetDirectoryName(Assembly.GetExecutingAssembl y().GetModules()[0].FullyQualifiedName);
openFileDialog.Filter = "lua files (*.lua)|*.lua";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
if ((fileStream = openFileDialog.OpenFile()) != null)
{
ThreadPool.QueueUserWorkItem(new
WaitCallback(CreateFlexbarTree), fileStream);
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original
error: " + ex.Message);
}
}
}

private void CreateFlexbarTree(object fileStreamState_)
{
Stream fileStream = fileStreamState_ as Stream;
using (fileStream)
{
string line;
decimal fileSize = fileStream.Length;
decimal percentDone = 0;
using (StreamReader streamReader = new StreamReader(fileStream))
{
while ((line = streamReader.ReadLine()) != null)
{
ReadLine(line.Trim());
percentDone = (streamReader.BaseStream.Position / fileSize) * 100;
UpdateProgressBar(percentDone);
}
}
}
}

private void UpdateProgressBar(decimal percentDone_)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (_readFileProgressBar.InvokeRequired)
{
UpdateProgressBarCallback d = new
UpdateProgressBarCallback(UpdateProgressBar);
Invoke(d, new object[] {percentDone_});
}
else
{
_readFileProgressBar.Value = (int)percentDone_;

//Im not sure but if the textbox is on the same form as the progress
bar, do I need to
//check if Invoke is required for the textbox as well? Either way,
taking out this line didn't seem
//to help
textBox1.Text = percentDone_.ToString();
}
}

private void ReadLine(string line)
{
if (_readFileProgressBar.InvokeRequired)
{
UpdateFlexbarTree d = new UpdateFlexbarTree(ReadLine);
Invoke(d, new object[] {line});
}
else
{
if (line.Contains("FBSavedProfile"))
{
_flexbarDataTree.Nodes.Add("FBSavedProfile");
}
}
}

Sep 9 '06 #1
0 1470

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

Similar topics

1
by: Ognjen Bezanov | last post by:
Hi, all Thanks all of you who helped me with the threading and queues issue. I am trying to get it working but I am having problems. When I try to run the following: cmddata =...
2
by: Jim W | last post by:
This is a cross-post from the .NET group since it looks like it may not be ..NET. New information since the original post is that is the wireless network is enabled and connected the socket...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
5
by: Chris | last post by:
Hi, I created a COM+ component which I am able to access with remote clients, however, I am unable to execute the component form a webservice. When I run the webservice I get The page cannot be...
2
by: Stressed Out Developer | last post by:
We have an application that has a 200 count loop that does the following: ' Each time thru the loop we pass the next IP Address is a range (aka 192.168.4.50 thru 192.168.4.254) Try If...
0
by: hynek.cihlar | last post by:
A strange behaviour thatI found in ASP.NET 2.0. I am trying to issue a callback request (handled by ICallbackEventHandler and RaiseCallbackEvent) and a regular GET request in the client browser...
31
by: Don Leverton | last post by:
Hi Folks, I've been away from this NG for quite a while, persuing other interests etc. I have made a few posts / replies here lately, and find this whole NG thing a little awkward and...
5
by: bean330 | last post by:
Hey, I'm somewhat new to C# and I need a little help, please! I'm selecting a bunch of records, setting properties on a COM executable and then calling a method on that executable to run. I...
3
by: dedalusenator | last post by:
Hello Folks, My first posting here and I am a stuck in figuring out the exact way to update a global variable from within a function that doesnt return any value (because the function is a...
1
by: =?Utf-8?B?QU1lcmNlcg==?= | last post by:
Sorry this is so long winded, but here goes. Following the model of http://msdn2.microsoft.com/en-us/library/system.runtime.remoting.channels.ipc.ipcchannel.aspx I made a remote object using the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...

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.