C Board  

Go Back   C Board > Platform Specific Boards > Windows Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-29-2009, 04:49 PM   #1
Registered User
 
Join Date: May 2009
Location: Arizona
Posts: 35
Illegal CrossThread Exception thrown when proccess is long

I didn't want to post this in the C# forum because the programs VB, so here it is...

I got a proccess thread thats in charge of loading a listbox full of the first line of text for richtextbox items. Though I use Me.Invoke(New MethodInvoker...) to add items on the main thread so its thread safe it coughs when large amounts of rtf data are thrown at it, acting as if I hadnt called adding the items on the main thread.

Up at the class level:
Dim Worker As System.Threading.Thread

In the load procedure for the form:
Worker = New System.Threading.Thread(AddressOf WorkerDo)
Worker.Start()

Private Sub WorkerDo()
Me.Invoke(New MethodInvoker(AddressOf FillList)
End Sub

Private Sub FillList()
For i As Integer = 0 To 'Upper index of collection of richtextbox controls
lstItems.Items.Add(RichTextBoxArray.Items(i).Lines (0))
Next
End Sub

List listItems is the list. I've tried creating a temporarily listbox in memory and than copy over the items to the desired listbox, but its getting stuck with the same exception even though the listbox has been created within the same subroutine (Filllist that is).

I admit I'm not too knowledgeable about pThreads yet, but I can't have it fill the list at the form's loading procedure because there are no items to put into the list until the task of loading up the RichTextBox array (also part of the pThread) has been finished. So right now I'm stuck at a dead end again...
kairozamorro is offline   Reply With Quote
Old 08-29-2009, 06:04 PM   #2
Rampaging 35 Stone Welsh
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 2,930
Try the VB forum here.
__________________
He is free, you say. Ah! That is his misfortune… These men… [have] the most terrible, the most imperious of masters, that is, need. … They must therefore find someone to hire them, or die of hunger. Is that to be free? - Simon Linguet
abachler is online now   Reply With Quote
Old 08-30-2009, 02:58 AM   #3
train spotter
 
Join Date: Aug 2001
Location: near a computer
Posts: 3,359
what is the value of 'i' when it throws?
__________________
"Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
Friedrich Nietzsche

"I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
George Best

"If you are going through hell....keep going."
Winston Churchill
novacain is offline   Reply With Quote
Old 08-30-2009, 01:01 PM   #4
Registered User
 
Join Date: May 2009
Location: Arizona
Posts: 35
The value of i is 0, for the first index of the collection of rtboxes. The exception is thrown immediately on the first literation when it tries to add contents from index 0 to the list. I think I might be able to bypass this mess by using a timer, not ideal but if does the trick (wouldn't be the first time I had to use timers to get around limitations in .net ). I'll go ahead and post this in the VB forums too when I get the chance. Just already registered here and the problem seems to be .net related rather than anything wrong with VB.
kairozamorro is offline   Reply With Quote
Old 08-30-2009, 03:59 PM   #5
Registered User
 
Join Date: May 2009
Location: Arizona
Posts: 35
Well tried a SyncLock but even thats still throwing the exception....
kairozamorro is offline   Reply With Quote
Old 08-30-2009, 04:33 PM   #6
Registered User
 
Join Date: May 2009
Location: Arizona
Posts: 35
Sigh.... Well with a timer I got it working, but if I try to select an item for example from the list on the form after it has loaded the same exception is being thrown
kairozamorro is offline   Reply With Quote
Old 08-30-2009, 05:44 PM   #7
Registered User
 
Join Date: May 2009
Location: Arizona
Posts: 35
Dangit MS. Got rid of threading the app. Its working now but its time consuming and I don't get the advantage of a progress bar
kairozamorro is offline   Reply With Quote
Old 08-30-2009, 06:25 PM   #8
Registered User
 
Join Date: May 2009
Location: Arizona
Posts: 35
Well thanks for the help everyone, but I think I'm going to need to make some major changes outside the scope of this topic...
kairozamorro is offline   Reply With Quote
Old 08-31-2009, 03:56 PM   #9
Ex scientia vera
 
Join Date: Sep 2007
Posts: 426
What you need to be doing is using BeginInvoke on the control whose data you want to modify, so it calls a function, in its own thread, to modify the data. The reason you are getting that error is because you are trying to modify data in another thread, the thread that manages the GUI.

Google your exception and you will find how to do it in the first few results.
__________________
"What's up, Doc?"
"'Up' is a relative concept. It has no intrinsic value."
IceDane is offline   Reply With Quote
Old 09-01-2009, 01:14 PM   #10
Registered User
 
Join Date: May 2009
Location: Arizona
Posts: 35
Me.Invoke invokes a no parameter subroutine on the main thread, so the adding of listbox items takes place on the main thread and works when the rtf data only contains plain text, but throws the exception if rtf data contains things like images.
kairozamorro is offline   Reply With Quote
Old 09-05-2009, 06:08 PM   #11
Registered User
 
Join Date: May 2009
Location: Arizona
Posts: 35
I learned a lot about delegates today and decided to start using them to setup the form's controls. Problem now is, its still throwing the same exception on a control's invoke method. So what now I wonder....
kairozamorro is offline   Reply With Quote
Old 09-06-2009, 04:20 PM   #12
Registered User
 
Join Date: May 2009
Location: Arizona
Posts: 35
BTW, how rude of me not post any code.

Here's the example:

Code:
private delegate void AddListBoxItemDelegate(object item);
private void AddListBoxItem(object item)
{    
if (this.listBox1.InvokeRequired)    
{
        // This is a worker thread so delegate the task.        
this.listBox1.Invoke(new AddListBoxItemDelegate(this.AddListBoxItem), item);    
}    
else    
{        
// This is the UI thread so perform the task.        
this.listBox1.Items.Add(item);    
}
}
Any other ways of calling the main thread to update a control anyone? I'm finding this more and more ridicuous as I go along here that I'm not having any problems with the application other than updating form controls.
kairozamorro is offline   Reply With Quote
Old 09-06-2009, 09:54 PM   #13
Hat seller extraordinaire
 
Join Date: Apr 2008
Posts: 159
I came across this the other day while searching for something else. I don't do VB but it might be worth a cheeky peek.
adeyblue is offline   Reply With Quote
Old 09-06-2009, 10:25 PM   #14
Registered User
 
Join Date: May 2009
Location: Arizona
Posts: 35
adeyblue, thank you for the link to that article

I can't guarantee it'll solve my problem (the circumstances are slightly different), but at least it helps me to understand some of the underhood non-sense in .net that can cause nightmares for the programmer.

Let me go ahead and expand on the actual program a little more. It has three esential forms, one for a startup form, one for editing the program's files, and another for using them (the purpose of having the program). These threading problems are occuring for the edit form which is accessed after the user has selected the file they want to edit from the startup form.

What makes my case different from the article? Well, the only default instance of any form in the application is the project's startup form. When a user wants to edit a file, a new instance of the edit form is created (the default instance isn't used). I also am getting an exception, while the author didn't on an invoke method that should have invoked on the thread the control is on (though through experimenting I was able to produce the bug described in the article before now).

Regardless of the differences, I'll take in the article's solution and see if it can help me. Hopefully it well, for the sake of getthing this non-sense over with so I concentrate on the actual program. Having to create a variable to the instance from the edit form itself? Who woulda thought of it? Well I guess thats .net for ya...

Last edited by kairozamorro; 09-06-2009 at 10:27 PM.
kairozamorro is offline   Reply With Quote
Old 10-22-2009, 07:53 PM   #15
Registered User
 
Join Date: May 2009
Location: Arizona
Posts: 35
Well about 2 months later I'm still battling this. I've got another projet I've almost finished with and its only taken me 3 days. This project? Oh about 5 months. Its ridicously amazing how something seemingly so simple be so complicated. I've removed multithreading completely for updating GUI controls and got it only for loading the input file. Problem is, richtextbox contents are not being loaded and I'm getting control accessed from another thread than the one it was created on now. I'll post again here if I find a solution.
kairozamorro is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting a swf file in a windows application face_master Windows Programming 12 05-03-2009 11:29 AM
illegal operation jdinger Windows Programming 3 04-07-2002 07:14 PM
Background-process app philip C++ Programming 14 03-26-2002 12:22 PM
need help emperor C Programming 1 03-04-2002 12:26 PM
can someone check this out and let me know ? javaz C Programming 5 01-21-2002 02:13 PM


All times are GMT -6. The time now is 03:45 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22