|
-
Mar 9th, 2012, 12:26 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Multi-threading Difficulties
Okay, so here is the difficulty. I have a main form on which there are controls I need to manipulate. This manipulation is quite resource intensive for a few seconds as it makes call to API's of other running MS Office software products.
I also have a loading form on which there is an animated loading gif picture. In order to ensure that the animated gif runs, I have had to fire the control manipulation onto a new thread but this is causing difficulties. I'll explain further:
- I have Form A on which there is a datagrid
- I have Form B on which there is a loading style animated gif
- I create a new background worker thread on Form A which should make the API calls and populate the datagrid.
- Thread 2 (the worker thread) doesn't like this because it isn't thread safe. So instead, I have had to use the Invoke method to access Thread 1 again to manipulate the controls; making the entire process pointless.
- Because Thread 1 is having to work again, the loading form freezes.
In short, what should I do? I have considered creating yet another thread, but i'm not sure whether or not this would help. In short, how can I keep Thread 1 smooth when another Thread has to access it using the Invoke method because otherwise it isn't thread-safe. :/
Argh!
Last edited by intraman; Mar 9th, 2012 at 12:54 PM.
-
Mar 9th, 2012, 12:30 PM
#2
Fanatic Member
Re: Multi-threading Difficulties
You should call a delegate (as you are doing) using BeginInvoke syntax, not Invoke. IMO.
Without seeing your code, it isn't clear why you are using two threads. However, if you need to interact with Thread1 from Thread2, you do have to make sure that you use appropriate synchronization to avoid a deadlock. You should look at the SyncLock and Monitor methods to see what is appropriate for your task.
Dick
Richard Grier, Consultant, Hard & Software
Microsoft MVP (Visual Basic)
-
Mar 9th, 2012, 12:48 PM
#3
Thread Starter
Hyperactive Member
Re: Multi-threading Difficulties
 Originally Posted by DickGrier
You should call a delegate (as you are doing) using BeginInvoke syntax, not Invoke. IMO.
Without seeing your code, it isn't clear why you are using two threads. However, if you need to interact with Thread1 from Thread2, you do have to make sure that you use appropriate synchronization to avoid a deadlock. You should look at the SyncLock and Monitor methods to see what is appropriate for your task.
Dick
Dick,
Thanks for getting back.
I'm using two separate threads one for the loading form and one for the main form as the main form thread activity causes the animated GIF on the loading form to freeze. Hence, the reason why I have allocated them both to different threads.
I've had a play with both SyncLock and Monitor methods, neither of which seem to have given me any joy. It is strange, as it is at the moment, I create the thread, use the new thread to show the loading form and then (in this same thread) Invoke the subroutine on Thread 1. However, the Invoke or BeginInvoke just doesn't seem to run when I use the ShowDialog method in the new thread just before it.
I must be missing something crucial. Unfortunately, I cannot legally post up much of the code, which is very frustrating of itself.
Thread 2's worker sub looks like this:
Code:
Private Sub runload()
'Invoke firstthread
newload.ShowDialog()
Me.BeginInvoke(New MethodInvoker(AddressOf emailaddsub))
End Sub
With emailaddsub being a subroutine in Thread 1 and Newload being a standard form control.
Last edited by intraman; Mar 9th, 2012 at 12:52 PM.
-
Mar 9th, 2012, 12:55 PM
#4
Thread Starter
Hyperactive Member
Re: [RESOLVED] Multi-threading Difficulties
Just an update. After a play around I just solved this. 
Basically, when Thread 1 is finished doing its work, I have it close the form itself. Thanks for your help.
-
Mar 9th, 2012, 01:00 PM
#5
Fanatic Member
Re: [RESOLVED] Multi-threading Difficulties
I don't know enough.
What I "think" you want to do is to use a class that creates a background thread to show the first form and its UI elements. Then, you use a second method that creates another background thread to do something useful. You raise an event (RaiseEvent) from the second thread for which the event handler is added to the first form. Inside that event hander you call BeginInvoke to call a method that updates UI in the form.
I'm just guessing. I do this sort of thing fairly freqently, though I don't often use threads to show forms. I just show them. I certainly is possible to show a form from a thread, but I'm unsure of the utility to that process. What I do in threads is to handle things that would otherwise block user interaction.
Dick
Richard Grier, Consultant, Hard & Software
Microsoft MVP (Visual Basic)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|