473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Creating a pictureBox on the main form from a thread

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

Nov 21 '05 #1
3 3669
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

Nov 21 '05 #2
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


Nov 21 '05 #3
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
>

Nov 21 '05 #4

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

Similar topics

4
by: Dave Boyd | last post by:
Hi, I have two very similar forms each with a subform. The main form gets a few fields from the user and passes this back to a query that the subform is bound to. The requery is done when the...
3
by: Marius Rus | last post by:
I have an application write in c# and i want to offer to the user to create himself shortcuts with icons for the main menu items. I will very much appreciate if will receive an helping hand. Thank...
3
by: stumorgan | last post by:
There is probably an extremely simple answer to this question and I'm just being foolish. I have a main form (let's say FormMain) which opens other forms (let's say Form1, Form2). How do I keep...
0
by: Rajiv Das | last post by:
Environment: Visual Studio 2005, Beta 2 ..Net 2.0 Windows XP, SP2 C# Generics ------------------------------- Hi, I have a Windows Form whose contents I would like to dynamically change. I
5
by: Ivan | last post by:
I am used to VB6 and am not sure how to do this in Vstudio .NET. I have a main form which calls other forms. I want to disable that main form while other ones are called. I tried hiding it and...
7
by: Terry | last post by:
I have a Mainform with a Statusbar. When opening another form or doing some processing I want to display info in the Statusbar of the Mainform. I have read a lot of articles on this & have come up...
2
by: campos | last post by:
Hi all, I ran into a headache problem. I have a windows form with a progress bar on it. Then I new a thread to do calculation for a long time. I want the progress bar to show the calculation...
4
by: kimiraikkonen | last post by:
Hi, On my system which is 2.4GHZ P4 CPU, 1GB memory + 64MB DDR graphic card, if i create a simple picturebox docked on a form sized about 500x350 or less or more, doesn't matter, and also if i...
0
by: Sathishkumar K | last post by:
Hi All, I am facing problem while displaying the progress bar form on top of application main form. Let me explain bit more details. I running the main form through Application.Run(new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.