[RESOLVED] Picture boxes and threading
I am designing a program that is supposed to find test examples for a problem according to certain rules. I have a small form with some text boxes for rule input. When a button is pressed this starts a new thread with the necessary calculations running at below normal priority so as not to interfere with my other processes while it does the calculations in the background.
Within this thread a new form is opened with a picture box. The results of the calculations are supposed to be viewed in this window until another valid example is found (then that one should be shown). However, when I move the window to see the original window (so that I can see the progress being made) or even just click on the window, the contents of the window go white and do not return even when other results should be drawn.
How can I change my code so that the picture remains and changes even if the window is moved around? Also, how do I prevent the closing of the picture window (Form2) from closing the other window running the calculations (Form1)?
Below is an outline of the code:
Code:
Public Class Form1
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button.Click
'Some initializing
Dim t As Threading.Thread
t = New Threading.Thread(AddressOf Me.Calculations)
t.Priority = ThreadPriority.BelowNormal
t.Start()
'Some more stuff
End Sub
Public Sub Calculations
Dim F2 As New Form2
Do While <more calculations are to be made>
'Some calcuations
If <the calculations are valid> Then
If Not F2.Created Then
F2.Show()
End If
AddHandler F2.PictureBox.Paint, AddressOf F2.PictureBox_Paint
F2.PictureBox.Refresh()
End If
Loop
End Sub
End Class
Public Class Form2
Public Sub PictureBox_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs)
'Draw a whole bunch of things
End Sub
End Class
Re: Picture boxes and threading
Add this line in ..
Code:
AddHandler F2.PictureBox.Paint, AddressOf F2.PictureBox_Paint
F2.PictureBox.Refresh()
Application.DoEvents()
End If
Re: Picture boxes and threading
Thanks Boet
That works perfectly!
Re: Picture boxes and threading
No Problem ... I'm glad you solved it ...
But do us a small favour, Please mark this thread resolved.. (Thread tools, just above the first post) ...
Re: Picture boxes and threading
Cool, will do so. Could you help me though with the problem of making the windows independent of each other (other than the info sent to Form2), so that if I close one, the other stays open. For example, when closing F2 while another picture could still be drawn?
Re: Picture boxes and threading
Okay for this, i need to know a bit more about whats happening ... it sounds like what you are saying is that when form2 opens form1 closes ??
Can you explain it a little more ...
Re: [RESOLVED] Picture boxes and threading
The following happens. When I initiate the program, Form1 opens with a whole bunch of text boxes for input. When I press a button, these inputs are used to create data. If a valid data set is created, the output is printed to a picture box on Form2.
If I close F1 during the calculations, the form closes but the calculations continue (in the thread) and any valid data sets are still drawn to F2. I want it to stop, but for F2 to remain open.
If I close F2 with F1 open or not, the program crashes when a valid data set is found and attempt is made to draw it. How do I prevent that from happening?
I hope that makes it clearer.
Re: [RESOLVED] Picture boxes and threading
Okay .. i think i get it ..
Give me a little time to lookup a few things..