manzoor10
January 19th, 2009, 10:15 AM
EDIT: yea yea !!! I did it by myself
so while(!(succeed = TryAgain())) :D
How to convert this C# code to C++/CLI?
using System;
using System.IO;
using System.Text;
using Sys tern.Threading;
public class AsyncDemo
{
// Stream object for reading
static FileStream fileStm;
// Buffer to read
static byte[] readBuf;
// Async-Call-back delegate
static AsyncCallback Callback;
public static void Main(String[] args)
{
Callback = new AsyncCallback(CallBackFunction);
fileStm = new FileStream(@"C:\Networking\Streams\Test.txt",
FileMode.Open, FileAccess.Read,
FileShare.Read, 64, true);
readBuf= new byte[fileStm.Length];
// Call async read
fileStm.BeginRead(readBuf, 0, readBuf.Length, Callback, null);
// Simulation of main execution
for (long i = 0; i < 5000; i++)
{
if (i % 1000 == 0)
{
Console.WriteLine("Executing in Main - " + i.ToString());
Thread.Sleep(10);
}
}
fileStm.Close();
}
static void CallBackFunction(IAsyncResult asyncResult)
{
// Gets called when the buffer is full.
int readB = fileStm.EndRead(asyncResult);
if (readB > 0)
{
fileStm.BeginRead(readBuf, 0, readBuf.Length, Callback, null);
Console.WriteLine(Encoding.ASCII.GetString(readBuf, 0, readB));
}
}
}
I'm having difficulties on how to do it? should I make a class with all the members the same as in the above C# code and then program main separately? Will this work?
My try but it doesn't works
#include "stdafx.h"
using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Net::Sockets;
using namespace System::Threading;
ref class AsyncDemo
{
public:
static FileStream^ fileStm = gcnew FileStream("C:\\Test.txt", FileMode::Open, FileAccess::Read,
FileShare::Read, 64, true);
static array^ readBuf = gcnew array(fileStm->Length);
static AsyncCallback^ Callback;
static void CallBackFunction(IAsyncResult^ asyncResult)
{
// Gets called when the buffer is full.
int readB = fileStm->EndRead(asyncResult);
if (readB > 0)
{
fileStm->BeginRead(readBuf, 0, readBuf->Length, Callback, nullptr);
Console::WriteLine(Encoding::ASCII->GetString(readBuf, 0, readB));
}
}
};
void main(void)
{
AsyncDemo^ async = gcnew AsyncDemo();
async->fileStm->BeginRead(async->readBuf, 0, async->readBuf->Length, async->Callback, nullptr);
for (long i = 0; i < 5000; i++)
{
if (i % 1000 == 0)
{
Console::WriteLine("Executing in Main - " + i.ToString());
Thread::Sleep(10);
}
}
async->fileStm->Close();
}
I get the following errors
System::AsyncCallback' : cannot use this type here without a top-level '^'
null' : undeclared identifier
so while(!(succeed = TryAgain())) :D
How to convert this C# code to C++/CLI?
using System;
using System.IO;
using System.Text;
using Sys tern.Threading;
public class AsyncDemo
{
// Stream object for reading
static FileStream fileStm;
// Buffer to read
static byte[] readBuf;
// Async-Call-back delegate
static AsyncCallback Callback;
public static void Main(String[] args)
{
Callback = new AsyncCallback(CallBackFunction);
fileStm = new FileStream(@"C:\Networking\Streams\Test.txt",
FileMode.Open, FileAccess.Read,
FileShare.Read, 64, true);
readBuf= new byte[fileStm.Length];
// Call async read
fileStm.BeginRead(readBuf, 0, readBuf.Length, Callback, null);
// Simulation of main execution
for (long i = 0; i < 5000; i++)
{
if (i % 1000 == 0)
{
Console.WriteLine("Executing in Main - " + i.ToString());
Thread.Sleep(10);
}
}
fileStm.Close();
}
static void CallBackFunction(IAsyncResult asyncResult)
{
// Gets called when the buffer is full.
int readB = fileStm.EndRead(asyncResult);
if (readB > 0)
{
fileStm.BeginRead(readBuf, 0, readBuf.Length, Callback, null);
Console.WriteLine(Encoding.ASCII.GetString(readBuf, 0, readB));
}
}
}
I'm having difficulties on how to do it? should I make a class with all the members the same as in the above C# code and then program main separately? Will this work?
My try but it doesn't works
#include "stdafx.h"
using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Net::Sockets;
using namespace System::Threading;
ref class AsyncDemo
{
public:
static FileStream^ fileStm = gcnew FileStream("C:\\Test.txt", FileMode::Open, FileAccess::Read,
FileShare::Read, 64, true);
static array^ readBuf = gcnew array(fileStm->Length);
static AsyncCallback^ Callback;
static void CallBackFunction(IAsyncResult^ asyncResult)
{
// Gets called when the buffer is full.
int readB = fileStm->EndRead(asyncResult);
if (readB > 0)
{
fileStm->BeginRead(readBuf, 0, readBuf->Length, Callback, nullptr);
Console::WriteLine(Encoding::ASCII->GetString(readBuf, 0, readB));
}
}
};
void main(void)
{
AsyncDemo^ async = gcnew AsyncDemo();
async->fileStm->BeginRead(async->readBuf, 0, async->readBuf->Length, async->Callback, nullptr);
for (long i = 0; i < 5000; i++)
{
if (i % 1000 == 0)
{
Console::WriteLine("Executing in Main - " + i.ToString());
Thread::Sleep(10);
}
}
async->fileStm->Close();
}
I get the following errors
System::AsyncCallback' : cannot use this type here without a top-level '^'
null' : undeclared identifier