Closing forms. Multi-Threading

jimctr

Well-known member
Joined
Dec 1, 2011
Messages
52
Programming Experience
Beginner
I have a main form (form1) and when I press a Load Button, it executes a bunch of code on the main page and then ultimately spawns a new Form (i.e. Form2). However, control ultimately stays with form1, as I am operating in an infinite loop on Form 1.

From Form1
VB.NET:
Dim Form2 = New ChartingForm
            Form2.Show()
            ExitInfLoop = False
            Do Until ExitInfLoop = True
                Application.DoEvents()
                Form2.DynamicChartCreation(dt)
            Loop


Ultimately, Form2 will Spawn another form, Form3.

VB.NET:
'At this point you can view thumbnails of the associated components
        If ThumbnailView = True Then
            Form3 = New ThumbnailForm
            Form3.ThumbnailListCreation(dt, DataPointIndex, RemChartLevel, YearSelect, MonthSelect)
            Form3.Show()
            ThumbnailView = False
        End If


If the person presses the Load Button on Form1 again, even if I invoke Form2.Close() and Form3.Close() from the button press, the forms do not close, most likely because the code behind those two forms is still executing. Hence, I get a duplication of forms. Rather, I want the previous processes to stop so I can close the forms when new data is entered into the system. I believe this is done with multi-threading but I don't know how to set this up and associate with the two forms I wish to close. Can anyone provide guidance? Thanks
 
It appears that I was able to close one of the forms, Form2, without resorting to multi-threading. The problem with that one was that not only did I have Form2.Show() declared on Form1, but I also declared Me.Show() on form2 itself. Its closing form2 when I comment out Form2.Show() on Form1. What's odd about this is that I clearly don't exit the loop, and so Me.Show() on Form2 should therefore be continually hit, preventing me from closing the document I would think. (Note that It's still not good if another process is running in the background, even though I succeeded in closing the form)

It doesn't appear that I can close form3 from form1 for some reason. Form3 shows on the basis of a conditional statement which requires some activity on form2. Barring that activity, Form3 should not reassert itself. In other words, Form3 should not be impacted by the infinite looping as its condition won't be satisfied once Form2 is closed.

I would employ ChartingForm.Form3.Close(), where ChartingForm is Form2's name from the vantage point of Form1 when the load button is pressed.
 
This is what I did to resolve the Form3 closing issue. Previously I declared Form3 on Form2: Public Form3 As New ThumbnailForm. I moved this declaration to Form1 and simply employed Form3.Close() from Form1 where the button resides. Not certain why I could not close it the other way, but at least this is a temporary workaround. Hmmm... Maybe it was the order I tried to do things. If I successfully closed Form2 first but then tried to close Form3 via Form2 reference, possibly that would cause me some problems.
 
Back
Top