473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

using WriteFileEX from C#

tom
I'm trying to implement a class that writes blocks of data asynchronously to disk. Each block is an array of ulongs. I tried to use the standard beginwrite functions, but apparently they only support byte arrays. Due to the nature of my problem (huge amounts of data) it would be very cpu unfriendly to convert all the ulongs to bytes.

Therefore I switch to the native methods( WriteFileEx) but I got in trouble dealing with the overlapped structure and the callback routines. Does anybody know if there's some good c# example on how to deal with these.

Thanks for helping me out !

Tom
Nov 16 '05 #1
6 2398
Have you thought about putting the writing into a worker thread (create a
delegate and use the threadpool is easiest) ?

You can then use BinaryWriter to write out your ulongs, and it won't
interefere with the main thread.

--
John Wood
EMail: first name, dot, last name, at priorganize.com
"to*@vandeplas.com" <to*************@discussions.microsoft.com> wrote in
message news:B6**********************************@microsof t.com...
I'm trying to implement a class that writes blocks of data asynchronously to disk. Each block is an array of ulongs. I tried to use the standard
beginwrite functions, but apparently they only support byte arrays. Due to
the nature of my problem (huge amounts of data) it would be very cpu
unfriendly to convert all the ulongs to bytes.
Therefore I switch to the native methods( WriteFileEx) but I got in trouble dealing with the overlapped structure and the callback routines.
Does anybody know if there's some good c# example on how to deal with these.
Thanks for helping me out !

Tom

Nov 16 '05 #2
Hi, Tom

You should consider using BinaryWriter with FileStream, which is opened in
asynchronous mode. This doesn't require explicit conversion coding in your
program.
Did you try this?

Generally speaking, your statement about "cpu unfriendliness" is not
substantiated by anything except your guess here, so I don't think this
style of reasoning is valid. Did you do any tests? Considering performance
gap between CPU and disk IO you might be surprised to see that whatever
conversions you do do not influence speed of IO.

HTH
Alex

"to*@vandeplas.com" <to*************@discussions.microsoft.com> wrote in
message news:B6**********************************@microsof t.com...
I'm trying to implement a class that writes blocks of data asynchronously to disk. Each block is an array of ulongs. I tried to use the standard
beginwrite functions, but apparently they only support byte arrays. Due to
the nature of my problem (huge amounts of data) it would be very cpu
unfriendly to convert all the ulongs to bytes.
Therefore I switch to the native methods( WriteFileEx) but I got in trouble dealing with the overlapped structure and the callback routines.
Does anybody know if there's some good c# example on how to deal with these.
Thanks for helping me out !

Tom

Nov 16 '05 #3
well an array of ulongs is just a loop writing out a ulong :)

--
John Wood
EMail: first name, dot, last name, at priorganize.com
"to*@vandeplas.com" <to*************@discussions.microsoft.com> wrote in
message news:6D**********************************@microsof t.com...
Hi John,

the reason I didn't use the Binary writer is that it only supports single ulong's, not arrays of ulong's (it only supports arrays of type byte or
char).
Working with the threadpool would indeed have been a solution. The reason why I decided not to go along this path is that the native methods allow
extra optimizations, like for example the one for sequential access.
Tom

"John Wood" wrote:
Have you thought about putting the writing into a worker thread (create a delegate and use the threadpool is easiest) ?

You can then use BinaryWriter to write out your ulongs, and it won't
interefere with the main thread.

--
John Wood
EMail: first name, dot, last name, at priorganize.com
"to*@vandeplas.com" <to*************@discussions.microsoft.com> wrote in
message news:B6**********************************@microsof t.com...
I'm trying to implement a class that writes blocks of data asynchronously
to disk. Each block is an array of ulongs. I tried to use the standard
beginwrite functions, but apparently they only support byte arrays. Due to the nature of my problem (huge amounts of data) it would be very cpu
unfriendly to convert all the ulongs to bytes.

Therefore I switch to the native methods( WriteFileEx) but I got in

trouble dealing with the overlapped structure and the callback routines.
Does anybody know if there's some good c# example on how to deal with

these.
Thanks for helping me out !

Tom


Nov 16 '05 #4
You could also use BitConverter.GetBytes() on each long, in a loop, to copy
the longs into the byte array.

--
John Wood
EMail: first name, dot, last name, at priorganize.com
"to*@vandeplas.com" <to*************@discussions.microsoft.com> wrote in
message news:5F**********************************@microsof t.com...
You know... if you convert the ulong[] to a byte[] (ulong by ulong) you end up with somethign that is exactly the same as the original... at least
if you look how it's respresented in the memory... so if I just could assign
the address of the ulong[] to a byte[] my problems would be solved....
Most of the time I love managed code... but sometimes I really hate it ;-)

T

"John Wood" wrote:
well an array of ulongs is just a loop writing out a ulong :)

--
John Wood
EMail: first name, dot, last name, at priorganize.com
"to*@vandeplas.com" <to*************@discussions.microsoft.com> wrote in
message news:6D**********************************@microsof t.com...
Hi John,

the reason I didn't use the Binary writer is that it only supports single
ulong's, not arrays of ulong's (it only supports arrays of type byte or
char).

Working with the threadpool would indeed have been a solution. The
reason why I decided not to go along this path is that the native methods allow
extra optimizations, like for example the one for sequential access.

Tom

"John Wood" wrote:

> Have you thought about putting the writing into a worker thread
(create a
> delegate and use the threadpool is easiest) ?
>
> You can then use BinaryWriter to write out your ulongs, and it won't
> interefere with the main thread.
>
> --
> John Wood
> EMail: first name, dot, last name, at priorganize.com
> "to*@vandeplas.com" <to*************@discussions.microsoft.com>
wrote in > message news:B6**********************************@microsof t.com...
> > I'm trying to implement a class that writes blocks of data

asynchronously
> to disk. Each block is an array of ulongs. I tried to use the standard > beginwrite functions, but apparently they only support byte arrays. Due to
> the nature of my problem (huge amounts of data) it would be very cpu
> unfriendly to convert all the ulongs to bytes.
> >
> > Therefore I switch to the native methods( WriteFileEx) but I got
in > trouble dealing with the overlapped structure and the callback routines. > Does anybody know if there's some good c# example on how to deal

with these.
> >
> > Thanks for helping me out !
> >
> > Tom
>
>
>


Nov 16 '05 #5
Check out Buffer.BlockCopy()

Ken
"to*@vandeplas.com" <to*************@discussions.microsoft.com> wrote in
message news:5F**********************************@microsof t.com...
You know... if you convert the ulong[] to a byte[] (ulong by ulong) you end up with somethign that is exactly the same as the original... at least
if you look how it's respresented in the memory... so if I just could assign
the address of the ulong[] to a byte[] my problems would be solved....
Most of the time I love managed code... but sometimes I really hate it ;-)

T

"John Wood" wrote:
well an array of ulongs is just a loop writing out a ulong :)

--
John Wood
EMail: first name, dot, last name, at priorganize.com
"to*@vandeplas.com" <to*************@discussions.microsoft.com> wrote in
message news:6D**********************************@microsof t.com...
Hi John,

the reason I didn't use the Binary writer is that it only supports single
ulong's, not arrays of ulong's (it only supports arrays of type byte or
char).

Working with the threadpool would indeed have been a solution. The
reason why I decided not to go along this path is that the native methods allow
extra optimizations, like for example the one for sequential access.

Tom

"John Wood" wrote:

> Have you thought about putting the writing into a worker thread
(create a
> delegate and use the threadpool is easiest) ?
>
> You can then use BinaryWriter to write out your ulongs, and it won't
> interefere with the main thread.
>
> --
> John Wood
> EMail: first name, dot, last name, at priorganize.com
> "to*@vandeplas.com" <to*************@discussions.microsoft.com>
wrote in > message news:B6**********************************@microsof t.com...
> > I'm trying to implement a class that writes blocks of data

asynchronously
> to disk. Each block is an array of ulongs. I tried to use the standard > beginwrite functions, but apparently they only support byte arrays. Due to
> the nature of my problem (huge amounts of data) it would be very cpu
> unfriendly to convert all the ulongs to bytes.
> >
> > Therefore I switch to the native methods( WriteFileEx) but I got
in > trouble dealing with the overlapped structure and the callback routines. > Does anybody know if there's some good c# example on how to deal

with these.
> >
> > Thanks for helping me out !
> >
> > Tom
>
>
>


Nov 16 '05 #6
tom
Hi,

thanks for sending me this, probably I shouldn't say it either but this is a GREAT tip. It will most certainly end up in my hall off fame of best .net tips. Having a background in embedded C, you can not imagine how many times I missed the "misuse the memory" feature in .NET ;-)

Thanks, Tom

"Daniel Jin" wrote:
this advice is exactly what you shouldn't do, but I'm gonna tell you anyway. :)

this is a trick I discovered, you can use the strucklayout attribute to gain access to ulong array through byte array.

[StructLayout( LayoutKind.Explicit )]
public class OverlappedArray
{
[FieldOffset( 0 )] public byte[] ByteView;
[FieldOffset( 0 )] public ulong[] ULongView;
}

then you can do something like this

overlappedArray.ByteView = new byte[64];

for( int i = 0; i < OverlappedArray.ByteView.Length / 8; i++ )
overlappedArray.ULongView[i] = ulong.MaxValue;

then pass ByteView to anything that's expecting a byte array.

remember, be careful with the bound check. it will always to the size of the array you first initialized, so if you look at the Length through the ULongView, you'd still see 64, and you'd be corrupting memory. :) use this at your own risk.

"to*@vandeplas.com" wrote:
You know... if you convert the ulong[] to a byte[] (ulong by ulong) you end up with somethign that is exactly the same as the original... at least if you look how it's respresented in the memory... so if I just could assign the address of the ulong[] to a byte[] my problems would be solved....

Most of the time I love managed code... but sometimes I really hate it ;-)

T

"John Wood" wrote:
well an array of ulongs is just a loop writing out a ulong :)

--
John Wood
EMail: first name, dot, last name, at priorganize.com
"to*@vandeplas.com" <to*************@discussions.microsoft.com> wrote in
message news:6D**********************************@microsof t.com...
> Hi John,
>
> the reason I didn't use the Binary writer is that it only supports single
ulong's, not arrays of ulong's (it only supports arrays of type byte or
char).
>
> Working with the threadpool would indeed have been a solution. The reason
why I decided not to go along this path is that the native methods allow
extra optimizations, like for example the one for sequential access.
>
> Tom
>
> "John Wood" wrote:
>
> > Have you thought about putting the writing into a worker thread (create
a
> > delegate and use the threadpool is easiest) ?
> >
> > You can then use BinaryWriter to write out your ulongs, and it won't
> > interefere with the main thread.
> >
> > --
> > John Wood
> > EMail: first name, dot, last name, at priorganize.com
> > "to*@vandeplas.com" <to*************@discussions.microsoft.com> wrote in
> > message news:B6**********************************@microsof t.com...
> > > I'm trying to implement a class that writes blocks of data
asynchronously
> > to disk. Each block is an array of ulongs. I tried to use the standard
> > beginwrite functions, but apparently they only support byte arrays. Due
to
> > the nature of my problem (huge amounts of data) it would be very cpu
> > unfriendly to convert all the ulongs to bytes.
> > >
> > > Therefore I switch to the native methods( WriteFileEx) but I got in
> > trouble dealing with the overlapped structure and the callback routines.
> > Does anybody know if there's some good c# example on how to deal with
these.
> > >
> > > Thanks for helping me out !
> > >
> > > Tom
> >
> >
> >

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Debra Castro | last post by:
I have a backup job that occasionally gets this error and the backup fails. Microsoft SQL Server 2000 8.00.818 Standard Edition is running on Windows 2003. Does anyone have any ideas why this...
2
by: rawCoder | last post by:
Hi All, I have a *.cer file, a public key of some one and I want to encrypt some thing using this public key. Can someone point me to a sample code for Encrypting some file using...
1
by: Mike | last post by:
When trying to compile (using Visual Web Developer 2005 Express Beta; frameworkv2.0.50215 ) the source code below I get errors (listed below due to the use of ICallBackEventHandler. Ultimately I...
10
by: Christopher Benson-Manica | last post by:
Why can't I use a class destructor in a using declaration: using MyClass::~MyClass; ? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org ...
17
by: beliavsky | last post by:
Many of my C++ programs have the line using namespace std; but the "Accelerated C++" book of Koenig and Moo has many examples where the library names are included one at a time, for example ...
8
by: Petter Reinholdtsen | last post by:
I ran into a problem on HP-UX 11.00 the other day, where it refused to compile a program using 'using namespace std;' at the top. The reason seem to be that the compiler refuses to accept 'using...
14
by: john.burton.email | last post by:
I've done some extensive searching and can't seem to find an answer to this - Is it correct to using "using" with templates, for example: using std::vector; Or do I need to specify the type...
1
by: dvestal | last post by:
I'm trying to use Overlapped I/O from C#, and utterly failing. I've tried to boil down my code to as simple an example as possible, in hopes that you people can point to where I'm going wrong. ...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.