Monday 15 April 2013

c# - WPF Window with Bing Map being called from Winform -



c# - WPF Window with Bing Map being called from Winform -

i developing windows form take user's latitude , longitude, perform long calculations , display user's location , routes on map. map shown in separate wpf window using bing maps api. since calculations need performed after user enters latitude , longitude complicated , take lot of time, need perform calculations in background thread , phone call new wpf window background thread.

at first, wasn't able open wpf window background thread because of thread affinity, fixed after setting flat state of thread apartmentstate.sta. here procedure gets called after user enters latitude , longitude , presses on button on winform:

private void button1_click(object sender, eventargs e) { thread t1 = new thread(foo); t1.setapartmentstate(apartmentstate.sta); t1.isbackground = true; t1.start(); }

the procedure gets executed when thread starts foo(). have read on forums need phone call system.windows.threading.dispatcher.run() wpf window doesn't close thread finishes:

private void foo() { mainwindow window = new mainwindow(lat.text, lon.text); window.show(); window.closed += (sender2, e2) => window.dispatcher.invokeshutdown(); system.windows.threading.dispatcher.run(); }

wpf window called mainwindow. here code:

public partial class mainwindow : window { public mainwindow(double latitude, double longitude) { initializecomponent(); mymap123.focus(); //set map mode aerial labels mymap123.mode = new aerialmode(true); } }

finally, in case of need xaml file wpf window, here is:

<window x:class="wpftestapplication.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:m="clr-namespace:microsoft.maps.mapcontrol.wpf;assembly=microsoft.maps.mapcontrol.wpf" title="mainwindow" height="350" width="525"> <grid> <m:map credentialsprovider="key" x:name="mymap123"/> </grid> </window>

when input latitude , longitude , press on button, wpf window appear, bing map doesn't show world map. instead, background grey, , if seek zoom, or alter size of window, system.nullreferenceexception (object reference not set instance of object) gets thrown. debugger tells me exception thrown @ line system.windows.threading.dispatcher.run();. however, if not seek center map , don't set map aerial mode, works fine. should can phone call wpf window background thread , alter bing map's properties @ runtime without problems? checked have worked fine if wpf window getting called main ui thread. interestingly, if close wpf window , press on button again, new wpf window shows bing maps , system.nullreferenceexception doesn't thrown.

i ended calling begininvoke() on 1 of ui elements. allowed me create thread created ui element execute function pass argument.

c# wpf multithreading winforms bing-maps

No comments:

Post a Comment