![]() |
| | #1 |
| Registered User Join Date: May 2009 Location: Arizona
Posts: 35
| Illegal CrossThread Exception thrown when proccess is long 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 | |
| | #2 |
| Rampaging 35 Stone Welsh 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 | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #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);
}
}
|
| kairozamorro is offline | |
| | #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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |