PDA

View Full Version : Load new map in separate thread to keep ui responsive?



TicK
11-09-2005, 09:41 AM
Anyone know how I can accomplish this? I don't like how my program freezes when I'm loading a new map. Here's my attempt, can anyone tell me what I'm missing or what I should be reading up on?



Private Delegate Sub loadMapItem()

Private Sub Thread1Work()

Dim load As loadMapItem

Dim Result As IAsyncResult

load = New loadMapItem(AddressOf DelegateWork)

Result = AxMappointControl1.BeginInvoke(load)

Console.WriteLine("Thread 1 Done")

Console.WriteLine(Result.IsCompleted.ToString())

AxMappointControl1.EndInvoke(Result)

Console.WriteLine(Result.IsCompleted.ToString())

End Sub

Private Sub DelegateWork()

AxMappointControl1.Visible = False
AxMappointControl1.NewMap(1)
AxMappointControl1.Visible = True
Console.WriteLine("Delegate Done")

End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Thread1 As Thread

Thread1 = New Thread(AddressOf Thread1Work)

Thread1.Start()

End Sub

Wilfried
11-09-2005, 01:33 PM
Hi,

You dont tell us what the problem is !
But you cannot use a visual component in a thread where it is not created in unless it is specially designed for that purpose using win32 CriticalSection.

BTW, not only visual components, but the most of it...

TicK
11-09-2005, 01:50 PM
Is it just me or does loading a new map take forever? (using axcontrol.newmap()). So you're saying it's not possible to thread the loading of the control so the program doesn't freeze up? Is it possible to speed this up somehow?



Private t As Thread


Private Sub Thread1Work()

AxMappointControl1.Visible = False
t.Sleep(1000)
AxMappointControl1.NewMap(1)
AxMappointControl1.Visible = True
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

t = New Thread(AddressOf Thread1Work)
t.Start()

End Sub

my second attempt has a little better results ...it at least gives it a chance to repaint my main window before the axcontrol.newmap() is called...

Wilfried
11-11-2005, 12:07 PM
Hi,


Is it just me or does loading a new map take forever?

Can take up to a few seconds, depending on the speed of the machine even a douzan of seconds :(


So you're saying it's not possible to thread the loading of the control

No I did not say that. Euh maybe I did, but it was not meanth so (English is not my native language, so...). What I was trying to say is that it quite well possible that this works, but you will pretty sure go into troubles later because you are for sure doing an untested situation, and it is for sure not by design to be used as such.

As general rule any component's methods should be called by the thread where it is created in, this includes the destroying of it. Unless the component is by design using CriticalSections to be called by several threads.

For example there is no problem creating / using / destroying Mappoint object in a thread, but it can be a problem creating it in main thread and calling something from another thread.