When I call form.show in a thread it close when the thread completes.

  • Thread starter Thread starter PAPutzback
  • Start date Start date
Need a little more information to be specific to what you're doing, but
Form.ShowDialog will not allow the thread to close until the form is closed.

Chris
 
That worked. Thanks

My second form was taking focus away from the first form when it was popping
up. So I put it on a seperate thread, but when the form.show fired off in
the thread it immediately closed. .showdialog is keeping it up.
 
Any idea on why the form might take so long to paint. I know in Foxpro if
you scrolled thru the items of a listbox before showing it, that the form
would come up a lot faster.
 
How many items are you putting in the listbox? How long is long? Someone
was asking about the time it took to populate 12000 records in a listbox
yesterday or the day before. I don't know of any way to speed it up if that
is what's taking so long. If you post some simplified code for form2 that
shows the problem we may be able to help.

Chris
 
Public Class frm_PCPlist
Public dt_PCP As New DataTable
Friend m_PCPThread As Thread


Public Sub New()
'/// <summary>
'/// To speed up the process of displaying the list of providers.
'/// create a seperate thread to create the datatable in the
background
'/// </summary>
Dim ts1 As ThreadStart = New ThreadStart(AddressOf preLoadPCP)
m_PCPThread = New Thread(ts1)
m_PCPThread.IsBackground = True
m_PCPThread.Start()
End sub

Private Sub preLoadPCP()
'/// Get the provider list and load the lsit box on the PCPlist form
Dim PCPSql As String = _
"Select rtrim(provider) + ' | ' + rtrim(plast_nme) +', '
+ " _
& "rtrim(pfirst_nme) as name, provider from provider
order by plast_nme,pfirst_nme"
Dim PCPCmd As New SqlCommand(PCPSql, conn_EMC)
Dim PCPAdptr As New SqlDataAdapter(PCPCmd)
PCPAdptr.Fill(dt_PCP)
PCPList.DataSource = dt_PCP
PCPList.DisplayMember = "name"
PCPList.ValueMember = "provider"
End sub

Form1 instantiates this form and does not show it until the thread stops.

But once the thread stops, the form takes a few seconds to show. The
records - about 12 thousand have already been loaded into the datatable. so
it is not like the user opened the form directly and then got the data. I'd
understand the delay then. But the data is already loaded. So my guess is
the painting of the form.

-Phil
 
My guess would be it is doing the binding of actually loading the items into
the listbox. I don't have a solution for your problem though. The only
thing I can think of is to do some heavy customization of the listbox so
that you only load the data in the listbox that is shown at one time. If
you can see 6 items at one time, just show the first six items. When the
user moves up or down in the list, add/remove items as they move. That way
you won't have the lag time. You would be basically writing your own
databindings. If you load a subset of the data, say 100 records, does it
work fasters? To test this bind the listbox to a DataView of the datatable
instead of the datatable itself.

Chris
 
Well that speeds it up alot. I am not sure how to go about adding recs as
the move. The strange thing is that I don't try to show the form until the
data is there. So it must have to do with how much data is in the listbox.
It either doesn't bind the datatable to each item until it paints or the
amount of data in the table affects how quickly it loads.

My theory atleast.
 
PAPutzback said:
How do I keep the form up.


Always create your forms in your application's main UI thread. Instance
members of Windows Forms forms and controls are not safe for multithreading,
so accessing them directly can cause problems. In addition to that, your
forms need a message pump which is normally provided by the app's main UI
thread.

Multithreading in Windows Forms applications
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=multithreading&lang=en>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top