473,659 Members | 2,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 UpdateProgressB arCallback(deci mal percentDone);
delegate void UpdateFlexbarTr ee(string line);

private void _loadToolStripM enuItem_Click(o bject sender, EventArgs e)
{
Stream fileStream = null;
OpenFileDialog openFileDialog = new OpenFileDialog( );

openFileDialog. InitialDirector y =
Path.GetDirecto ryName(Assembly .GetExecutingAs sembly().GetMod ules()[0].FullyQualified Name);
openFileDialog. Filter = "lua files (*.lua)|*.lua";
openFileDialog. FilterIndex = 1;
openFileDialog. RestoreDirector y = true;

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

private void CreateFlexbarTr ee(object fileStreamState _)
{
Stream fileStream = fileStreamState _ as Stream;
using (fileStream)
{
string line;
decimal fileSize = fileStream.Leng th;
decimal percentDone = 0;
using (StreamReader streamReader = new StreamReader(fi leStream))
{
while ((line = streamReader.Re adLine()) != null)
{
ReadLine(line.T rim());
percentDone = (streamReader.B aseStream.Posit ion / fileSize) * 100;
UpdateProgressB ar(percentDone) ;
}
}
}
}

private void UpdateProgressB ar(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 (_readFileProgr essBar.InvokeRe quired)
{
UpdateProgressB arCallback d = new
UpdateProgressB arCallback(Upda teProgressBar);
Invoke(d, new object[] {percentDone_}) ;
}
else
{
_readFileProgre ssBar.Value = (int)percentDon e_;

//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_.To String();
}
}

private void ReadLine(string line)
{
if (_readFileProgr essBar.InvokeRe quired)
{
UpdateFlexbarTr ee d = new UpdateFlexbarTr ee(ReadLine);
Invoke(d, new object[] {line});
}
else
{
if (line.Contains( "FBSavedProfile "))
{
_flexbarDataTre e.Nodes.Add("FB SavedProfile");
}
}
}

Sep 9 '06 #1
0 1493

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

Similar topics

1
1475
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 = mediaplay.initcommandqueue() #initiates the Queue to send commands down
2
2081
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 connect time is 4x longer! Disable wireless and it is back down to just very slow. Any ideas are appreciated. -----
77
5260
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 the moment. I'd be *very* grateful if people with any interest in multi-threading would read it (even just bits of it - it's somewhat long to go through the whole thing!) to check for accuracy, effectiveness of examples, etc. Feel free to mail...
5
1220
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 displayed HTTP 500 - Internal server error Internet Explorer
2
2350
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 mUIO_Threads(i) Is Nothing Then mUIO_Threads(i) = New System.Threading.Thread(AddressOf mUIO_DAQ(i).InitDAQ) mUIO_Threads(i).Name = mUIO_DAQ(i).UIO_IPAddr mUIO_Threads(i).IsBackground = False
0
2456
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 and handle them at the same time - in parallel. The funny thing is that the behaviour of the implementation I created depends on the existence of Global.asax in the Web application. Here is the source of the page that handles the regular GET...
31
4625
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 cumbersome.(I'm using Microsoft Outlook) Compare this with the hotrodders.com bulletin board (http://hotrodders.com/forum/) and you'll see what I mean ... it's awesome. You can include pictures with your posts (either as attachments if they are from your...
5
2027
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 want to run the executable in separate threads because they can be long-running and it would be optimal for us to run a bunch simultaneously. I've got that part working - it's pretty easy in C#. What I'm having a hard time with is managing the...
3
12777
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 target of the thread and I dont know how exactly return would work in such a case). I am sure I am missing something very fundamental here. The essential pieces of my code that cause the problem would be something like this:...
1
1707
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 IpcChannel Class (vs 2005, vb, fw 2.0). Everyting works fine. The object is registered with WellKnownObjectMode.Singleton The remote object appears at the bottom of this posting. The code is deliberately obtuse to expose an issue about when...
0
8427
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...
0
8330
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8850
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2749
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
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
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.