Click here to Skip to main content
15,868,006 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have created a user control that takes an address and returns lantitute and longtitudes . I use this in my main project from a dll .

The problem is at the excecution the dll needs some time to connect with google api and return the values so the problem with this code is that i get empty latitude and longtitude but in my form everything works fine...

VB
GetAdress1.FullFormattedAdress = .postalAddress.Trim & " " & .postalAddressNo.Trim & " " & .postalAreaDescription.Trim & " " & .postalZipCode.Trim

                       User.Info.Postal_Lat = GetAdress1.Latitude
                       User.Info.Postal_Lon = GetAdress1.Longitude



So how can i wait for the dll to finish loading the info and after put that values in my class ??? Thank you very much and sorry for my bad english ...

The custom control code is something like this

VB
 Private _FullFormattedAdress As String
   Property FullFormattedAdress As String
       Get
           Return _FullFormattedAdress
       End Get
       Set(value As String)
           txtAdress.Text = value
           _FullFormattedAdress = value
       End Set
   End Property
Private _Latitude As String
   Property Latitude As String
       Get
           Return _Latitude
       End Get
       Set(value As String)
           _Latitude = value
           lblLatitude.Text = _Latitude
       End Set
   End Property

   Private _Longitude As String
   Property Longitude As String
       Get
           Return _Longitude
       End Get
       Set(value As String)
           _Longitude = value
           lblLongtitude.Text = _Longitude
       End Set
   End Property

Private Sub txtAdress_TextChanged(sender As Object, e As EventArgs) Handles txtAdress.TextChanged

        fmqRequest.Address = txtAdress.Text 'Address that needs to be converted to geographic coordinates
If fmqResponse.Status = ServiceResponseStatus.Ok Then
           For Each A In fmqResponse.Results
                 'Load the results in a list
           next


Private Sub lstAdressList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstAdressList.SelectedIndexChanged

           _Latitude = fmqLocation.Latitude
           _Longitude = fmqLocation.Longitude
           _FullFormattedAdress = a.FormattedAddress


       End If
   End Sub
Posted
Updated 8-Nov-14 0:51am
v2
Comments
Kschuler 7-Nov-14 9:47am    
Can you provide more context? Could you show us your DLL code? Have you debugged into your DLL to make sure it's really hitting the code that connects with the google API?
JNDIONYSIM 8-Nov-14 6:58am    
Yes the control works fine , the problem is that the two lines of code that saves latitude and longtitude excecutes before the code inside the control finishes ...

I solved my issue by adding an event handler in the custom control and raise it when lstAdressList_SelectedIndexChanged .... and then moving this two lines inside this event ... I think i need to optimise my customControl Code somehow but i am not sure how to do it :)

It makes no sense. There is not such thing as "call a DLL" or "wait for DLL". You always call a function; and when you do, the calling thread is executing that function until it returns, not doing anything else. It does not matter where the function is, in a DLL or in the same executable module where you do the call. Moreover, for .NET, it does not matter if some module is DLL or EXE: the central concept of .NET is assembly, which consists of modules, most usually one per assembly.

You would need to consider waiting (more exactly, thread synchronization and wait states associated with them) if you use multithreading. But as your question suggests that you just need to do two things in sequence, just do it all in one thread. The problem simply does not exist.

If you really need to do something in parallel (say, just keep UI responsive until all that UI work completes), this is not a big problem, too. You need to do communication in a separate thread and notify UI when some action is complete.

The problem is that you have to communication between UI thread and some other thread. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
—SA
 
Share this answer
 
v2
Comments
JNDIONYSIM 8-Nov-14 5:52am    
Sergey thank you you very much for your for your quick answer , You are absoloutly right 'There is not such thing as "call a DLL" or "wait for DLL". I know nothing about threading , and i think The resources in your answer will be very helpfull to me , thank you very much again.
Sergey Alexandrovich Kryukov 8-Nov-14 20:02pm    
You are welcome.
Consider reading on the topic and then accepting the answer formally (green "Accept button).
In all cases, your follow-up questions will be welcome.
—SA
You could try the following (I personally never tried it so cannot guarantee anything):
MSDN Async[^]

It requires .NET 4.5



You could do it synchronously simply waiting in a loop until it finishes - consider adding a finite timer that will take you out of the loop if it takes too long so you don't end up with infinite loop.

VB
GetAdress1.FullFormattedAdress = .postalAddress.Trim & " " & .postalAddressNo.Trim & " " & .postalAreaDescription.Trim & " " & .postalZipCode.Trim
While GetAddress1.Latitude.Trim = String.Empty
                       User.Info.Postal_Lat = GetAdress1.Latitude
                       User.Info.Postal_Lon = GetAdress1.Longitude
End While
 
Share this answer
 
Comments
JNDIONYSIM 8-Nov-14 7:03am    
This is throwing an excption of null argument , Besides it dont seems for a clean solution , maybe other problems will raise for examplelong waitings etc ... Thank you very much for your time
Sinisa Hajnal 10-Nov-14 2:09am    
I used your code fragment, of course you need to ensure that everything is instatiated before using it. And I did comment upon adding a timer to prevent too long a wait.

The solution was to give you two examples on how you can do it, not to do your work for you. You could use an event, but that again assumes update of UI thread from async operation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900