성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Roll A Lisp In C - Reading ; https...
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>PInvoke 호출을 이용한 비동기 파일 작업</h1> <p> 그냥 잠깐 만들어 보았는데 기록을 남기는 차원에서 써봅니다. ^^<br /> <br /> 닷넷은 비동기 메서드 호출이 잘 되어 있지만, 가끔은 PInvoke로 직접 해야 하는 경우도 있을 듯 싶어 예제 코드 삼아 만들어 보았습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using Microsoft.Win32.SafeHandles; using System; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using System.Text; using System.Threading; class Program { [DllImport("kernel32.dll", SetLastError = true)] public static extern SafeFileHandle CreateFile( String pipeName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplate); [DllImport("kernel32.dll", SetLastError = true)] static extern bool WriteFile(SafeFileHandle hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, out uint lpNumberOfBytesWritten, [In] ref System.Threading.NativeOverlapped lpOverlapped); public const int FILE_FLAG_OVERLAPPED = 0x40000000; static void Main(string[] args) { EventWaitHandle exitEvent = new EventWaitHandle(false, EventResetMode.ManualReset); string txt = new string('t', 1024 * 1024 * 600); byte[] buf = Encoding.ASCII.GetBytes(txt); while (true) { File.Delete(@"C:\temp\test.txt"); using (SafeFileHandle pHandle = <span style='color: blue; font-weight: bold'>CreateFile</span>(@"c:\temp\test.txt", (uint)FileAccess.ReadWrite, 0, IntPtr.Zero, (uint)2, <span style='color: blue; font-weight: bold'>FILE_FLAG_OVERLAPPED</span>, IntPtr.Zero)) { uint written; <span style='color: blue; font-weight: bold'>NativeOverlapped o = new NativeOverlapped();</span> using (ManualResetEvent writeEvent = new ManualResetEvent(false)) { <span style='color: blue; font-weight: bold'>o.EventHandle = writeEvent.SafeWaitHandle.DangerousGetHandle();</span> Stopwatch st = new Stopwatch(); st.Start(); if (<span style='color: blue; font-weight: bold'>WriteFile(pHandle, buf, buf.Length, out written, ref o)</span> == false) { int lastError = Marshal.GetLastWin32Error(); if (lastError == 997) // ERROR_IO_PENDING == 997 { Console.WriteLine("async"); } else { // Write File Error Console.WriteLine("Write File Error"); break; } WaitHandle[] waitHandles = new WaitHandle[] { writeEvent, exitEvent }; int completeCode = <span style='color: blue; font-weight: bold'>WaitHandle.WaitAny</span>(waitHandles, Timeout.Infinite, false); if (completeCode == 1) // exitEvent signaled { break; } } st.Stop(); Console.WriteLine(st.ElapsedMilliseconds); } Thread.Sleep(1000); } } } } </pre> <br /> 아울러, 아래와 같은 자료들이 있으니 읽어보시는 것도 좋겠습니다. ^^<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Synchronous and Asynchronous I/O ; <a target='tab' href='https://docs.microsoft.com/en-us/windows/win32/fileio/synchronous-and-asynchronous-i-o'>https://docs.microsoft.com/en-us/windows/win32/fileio/synchronous-and-asynchronous-i-o</a> Asynchronous Disk I/O Appears as Synchronous on Windows NT, Windows 2000, and Windows XP ; <a target='tab' href='http://support.microsoft.com/kb/156932'>http://support.microsoft.com/kb/156932</a> </pre> <br /> (<a target='tab' href='http://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=883&boardid=331301885'>첨부 파일은 이 글의 예제 코드를 포함</a>합니다.)<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1031
(왼쪽의 숫자를 입력해야 합니다.)