等待dll完成 [英] Wait for the dll to finish

查看:94
本文介绍了等待dll完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个用户控件,它接受一个地址并返回lantitute和longtitudes。我在一个dll的主项目中使用它。



问题在于dll需要一段时间来连接google api并返回值以解决问题这个代码是我得到空的纬度和经度但在我的形式一切正常...



<前lang =vb> GetAdress1.FullFormattedAdress =。 postalAddress.Trim& & .postalAddressNo.Trim& & .postalAreaDescription.Trim& & .postalZipCode.Trim

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







那么我怎么能等待dll完成加载信息并将这些值放入我的班级后???非常感谢,抱歉我的英语不好...



自定义控制代码是这样的



 私有 _FullFormattedAdress 作为 字符串 
属性 FullFormattedAdress 作为 字符串
获取
返回 _FullFormattedAdress
结束 获取
设置( value As String
txtAdress.Text = value
_FullFormattedAdress = value
结束 设置
结束 属性
私有 _Latitude 作为 字符串
属性纬度作为 字符串
获取
返回 _Latitude
结束 获取
设置(值作为 字符串
_Latitude = value
lblLatitude.Text = _Latitude
结束 设置
结束 属性

私人 _Longitude 作为 字符串
属性经度作为 字符串
获取
返回 _Longitude
结束 获取
设置(值 As String
_Longitude = value
lblLongtitude.Text = _Longitude
结束 设置
结束 属性

私有 Sub txtAdress_TextChanged(发件人 As 对象,e As EventArgs)句柄 txtAdress.TextChanged

fmqRequest.Address = txtAdress.Text ' 需要转换为地理坐标的地址
如果 fmqResponse.Status = ServiceResponseStatus.Ok 那么
对于 每个 A fmqResponse.Results
' 将结果加载到列表中
next


私有 Sub lstAdressList_SelectedIndexChanged(sender 作为 对象,e As EventArgs)句柄 lstAdressList.SelectedIndexChanged

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


结束 如果
结束 Sub

解决方案

没有意义。没有调用DLL或等待DLL之类的东西。你总是叫一个功能;当你这样做时,调用线程正在执行该函数,直到它返回,而不执行任何其他操作。函数的位置,在DLL或执行调用的同一可执行模块中无关紧要。此外,对于.NET,如果某个模块是DLL或EXE并不重要:.NET的核心概念是程序集,它由模块组成,最常见的是每个模块一个汇编。



如果使用多线程,则需要考虑等待(更准确地说,是线程同步和与之关联的等待状态)。但是,正如你的问题表明你只需要按顺序做两件事,只需在一个线程中完成。这个问题根本就不存在。



如果你真的需要并行做一些事情(比如,只要保持UI响应直到所有的UI工作完成),这不是一个大问题。您需要在单独的线程中进行通信,并在某些操作完成时通知UI。



问题是您必须在UI线程和其他线程之间进行通信。您不能从非UI线程调用与UI相关的任何内容。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



-SA
-SA


您可以尝试以下方法(我个人从未尝试过,所以无法保证):

MSDN异步 [ ^ ]



需要.NET 4.5







您可以同步完成,只需等待一个循环直到它完成 - 考虑添加一个有限的计时器,如果它需要太长时间将带您离开循环你不会以无限循环结束。



 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 虽然


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...

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

 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

解决方案

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


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.

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


这篇关于等待dll完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆