When you're running code on a different thread to the UI you must use the
Invoke mechanism to call methods in controls in the UI thread.
If you don't know whether you're running on a different thread you can use
the Control.InvokeRequired property to see if you need to invoke.
You can create a method in your control to do the PB creation task and then
use Invoke to run that method from the other thread such as:
Class foo
Inherits Control
Private t As System.Timers.Timer
Public Sub New()
t=new System.Timers.Timer(10000) ' fires after 10 seconds
AddHandler t.Elapsed, AddressOf tickhandler
t.Enabled=True
End Sub
public Sub UIThreadMethod()
'do something on the UI thread
End Sub
public sub tickhandler
'although it's always going to be true, for good practice look at
invoke required
If InvokeRequired Then
this.Invoke(new MethodInvoker(this.UIThreadMethod)) 'Invoke the
methosd on the UI thread
End If
End Sub
End Class
I do ask myself however, why you're creating a picturebox from a thread
anyway? Dynamic control creation schemes seem clever when first thought of
but can have serious implications. If you're using PictureBoxes as say,
image thumbnail displays for a directory of images, you're definitely doing
it wrong.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Ramuseco Limited .NET consulting
http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
"EnglishMan69" <ad*********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hi Bob,
Thanks for responding...
I have tried to do use a method to create the pictureBox, but the
program seems to exit the thread when it reaches the section of the
method that actually adds the picture box to the main form...
I have heard of invoking though, can you give me a rough example (the
book I am using is pretty sparse on examples) ?
Thanks again,
Adam
Bob Powell [MVP] wrote: I would advise you to define a method that creates the picturebox and
invoke
this method from the thread.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Ramuseco Limited .NET consulting
http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
"EnglishMan69" <ad*********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com... > Hello All,
> I am using VB2005 Beta 2 in VS 2005 and am running into a small
> problem. I need to be able to add a picture box to the main form from
> within a thread.
>
> The program goes to a web site, performs a search based on POST
> variables, retrieves the code, parses the information (including a url
> for a thumbnail image) and displays the results.
>
> I am using threads, since the program will appear to freeze while
> posting and retrieving the data otherwise. The problem I am having is
> that I cannot seem to create a new picture box on the main form from
> within the thread.
>
> Is there a way to do this from within the thread?
>
> I have tried using delegates (though I am could easily be making a
> mistake), I have tried having the main form wait for the thread to
> complete the processing before creating the picture boxes (though this
> makes the program freeze also). I am rapidly running out of ideas...
>
> Thanks!
>
> Adam
>