NetworkStream.Write异步问题(NetworkStream.Write asynchronous issue)
我在C#中写入NetworkStream时遇到问题。 从MSDN我读到:
Write方法将一直阻塞,直到发送请求的字节数或抛出SocketException
好吧 - 就我而言,它的行为就像一个异步方法。 线程未被阻止。
这是一个代码示例,稍微启发一下情况:
TcpClient tcpcl = new TcpClient("192.168.1.128", 1337); NetworkStream netst = tcpcl.GetStream(); byte[] will_send = File.ReadAllBytes(@"large_file_120_MB.mp4"); Console.WriteLine("Starting transmission..."); netst.Write(will_send, 0, will_send.Length); Console.WriteLine("File has been sent !"); (... later instructions ...)
执行1秒后控制台的结果:
开始传输......
文件已发送!
第二条消息立即显示。 后来的指令正在执行中。
同时服务器仍然接收文件,并在其一侧一切正常。 它变得更好 - 如果我杀了发送程序,在传输过程中,接收将不会停止。 Degugger清楚地表明应用已完全结束。 然而,在完全停止接收之前,仍然会传输更多的兆字节。
所以我的问题 - 有没有办法阻止主线程,直到Write方法完成?
I have a problem with writing to a NetworkStream in C#. From MSDN i read:
The Write method blocks until the requested number of bytes is sent or a SocketException is thrown
Well - in my case, it behaves like an asynchronous method. Thread is not being blocked.
Here is a code sample, to enlighten situation a bit:
TcpClient tcpcl = new TcpClient("192.168.1.128", 1337); NetworkStream netst = tcpcl.GetStream(); byte[] will_send = File.ReadAllBytes(@"large_file_120_MB.mp4"); Console.WriteLine("Starting transmission..."); netst.Write(will_send, 0, will_send.Length); Console.WriteLine("File has been sent !"); (... later instructions ...)
Result from console after 1 second of execution:
Starting transmission...
File has been sent !
Second message shows immediately. Later instructions are being executed.
Meanwhile server still receives the file and on its side everything works well. It gets better - if i kill sending program, during transmission, receiving won't stop. Degugger shows clearly that app has ended entirely. However few more megabytes will still be transmitted, until receiving stops completely.
So my question - is there a way to block main thread, until Write method is finished ?
原文:https://stackoverflow.com/questions/14839245
最满意答案
Delphi XE2包含dbExpress ODBC驱动程序,该驱动程序可与PostgreSQL ODBC驱动程序一起使用以访问此数据库。 不幸的是,
dbExpress ODBC driver
仅支持Windows(现在)。对于跨平台开发,您可以尝试DevArt中的PgDAC组件
PgDAC是一个跨平台解决方案,用于使用各种IDE开发应用程序:适用于x86和x64平台的Windows,Mac OS X,iOS,Linux和FreeBSD上的RAD Studio,Delphi,C ++ Builder,Lazarus(和FPC)。 PgDAC还为FireMonkey平台提供支持,该平台允许您为Windows和Mac OS X开发视觉上引人注目的高性能本机应用程序。
Delphi XE2 includes the dbExpress ODBC driver which can be used with the PostgreSQL ODBC driver for access this database. Unfortunally the
dbExpress ODBC driver
only supports windows (for now).For cross-platform development you can try the PgDAC component from DevArt
PgDAC is a cross-platform solution for developing applications using various IDEs: RAD Studio, Delphi, C++Builder, Lazarus (and FPC) on Windows, Mac OS X, iOS, Linux, and FreeBSD for both x86 and x64 platforms. PgDAC also provides support for the FireMonkey platform, which allows you to develop visually spectacular high-performance native applications for Windows and Mac OS X.
相关问答
更多-
如何调整按钮大小以适应Delphi FireMonkey中的文本?(How to adjust button size to fit the text in Delphi FireMonkey?)[2023-07-26]
FireMonkey通过使用TTextLayout类的方法呈现文本。 我们可以通过类助手访问这些方法,然后根据布局提供的信息更改按钮大小。 uses FMX.TextLayout; type TextHelper = class helper for TText function getLayout : TTextLayout; end; function TextHelper.getLayout; begin result := Self.fLayout; end; proce ... -
ARC是根据OS平台实现的,而不是GUI框架。 Android,iOS和Linux编译器对对象使用ARC内存管理。 Windows和OSX编译器使用经典的手动内存管理,其中ARC仅支持接口引用,而不支持对象。 VCL是一个仅限Windows的框架,它仅在经典编译器下运行。 另一方面,FMX作为跨平台框架,并使用不同的内存管理系统,具体取决于它运行的OS平台。 try...finally Free块在ARC编译器上确实是无用的(在与Free方法结合的安全防护对象释放的上下文中)。 但是,如果您编写必须在两个内 ...
-
使用Form的Left和Top属性(和你在VCL中一样)。 Use the Form's Left and Top properties (same as you do in VCL).
-
FireMonkey尚不存在操作。 顺便说一句,您可以使用如下代码创建相同的行为: IdHTTP1: TIdHTTP; ... procedure TForm2.MenuItem1Click(Sender: TObject); const FILENAME = 'C:\Users\Whiler\Desktop\test.htm'; URL = 'http://stackoverflow.com/questions/7491389/firemonkey-and-tdownloadurl'; ...
-
解决问题的方法是: procedure TForm1.Button1Click(Sender: TObject); begin RemoveObject(Button1); end; 在VCL中使用.Free,但FMX的工作方式不同。 The way to solve it is: procedure TForm1.Button1Click(Sender: TObject); begin RemoveObject(Button1); end; In VCL worked with the .Fr ...
-
InputBox不提供您使用的键盘选择,无论是FMX.Dialogs.InputBox还是VCL.Dialogs.InputBox版本。 这是一个使用标准键盘布局的标准化功能。 如果您想要不同的行为,请使用其他方法来获取输入。 设计自己的表单,提供专门的行为并显示它以获取用户的输入。 InputBox doesn't offer a choice of which keyboard you use, whether it's the FMX.Dialogs.InputBox or VCL.Dialogs.I ...
-
现代鼠标上的滚轮取代了中间按钮。 在这方面,FireMonkey事件与VCL事件完全相同。 只测试涉及的中间按钮。 例如: procedure TMyForm.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y ...
-
Delphi XE2包含dbExpress ODBC驱动程序,该驱动程序可与PostgreSQL ODBC驱动程序一起使用以访问此数据库。 不幸的是, dbExpress ODBC driver仅支持Windows(现在)。 对于跨平台开发,您可以尝试DevArt中的PgDAC组件 PgDAC是一个跨平台解决方案,用于使用各种IDE开发应用程序:适用于x86和x64平台的Windows,Mac OS X,iOS,Linux和FreeBSD上的RAD Studio,Delphi,C ++ Builder,La ...
-
是的,这是可能的,但你不会有表格或视觉设计师(就像你只使用API编写Windows应用程序一样)。 菜单项File->New->Other ,选择Console Application 。 右键单击Project Manager中的Target Platforms ,选择OSX 32 ,选中Make target active ,然后OK 。 您可以从项目源中删除{$APPTYPE CONSOLE} (如果您愿意)。 像任何API应用程序一样添加单位,但$(BDS)\Source\rtl\osx文件夹除外。 ...
-
请参阅这些文章以帮助您入门。 在Delphi XE5 Firemonkey应用程序中禁用IOS设备空闲超时 使用ObjectiveC Bridge以编程方式禁用iOS上的空闲计时器 使用Delphi为您的iOS应用程序提供后台服务支持 在Delphi XE5 Firemonkey中处理Android和IOS生命周期事件 See these articles to get you started. Disable The IOS Device Idle Time Out In Your Delphi XE5 ...