|
|
Hi All.
I writing some application and in it i wrote some Log file that will be as instance in each class and in each class that use the log - will able to send string to the log file.
I have some problem that create some exception in my log file.
In log class i have method call "Write" that getting string and writing it to file.
In the end of method "Write" the log class close the file by doing this line
if (m_cFileStream!= null)
m_cFileStream.Close();
After i call the "Write" class again i getting Exception in the line that do :
StreamWriter cStreamWrite = new StreamWriter(m_cFileStream);
[The variable m_cFileStream is not a null and he hold the same information they were in it before calling the block that close the FileStream]
I realy dont understand why i getting an exception and how to solve this problem.
Someone can Help me ??
|
|
|
|
|
The problem is that when you call c_fileStream.Close() you are in essence destroying this object. Stream.Close ends up mappnig to Stream.Dispose(). You'll either have to create a new instance of the stream class or not destroy it the first time.
Jared Parsons
jaredp@beanseed.org
http://spaces.msn.com/members/jaredp/
|
|
|
|
|
Jared you nailed it!
Yanshof here is some design info for ya:
When designing a Logger application here is a basic strategy to employ in your architecture. Note I highly recommend using the Async architecture to keep you application running efficiently instead of blocking all over the place. That means Stream::BeginRead, Stream::EndRead, Stream::BeginWrite, Stream::EndWrite for your cStreamWriter object, as well as Socket::BeginAccept, Socket::EndAccept, Socket::BeginReceive, and Socket::EndReceive for your socket or network stream objects.
At any rate:
Startup sequence:
Open the log file. This includes the necessary checks to determine if it exists, needs to be created, or pehaps is too large and needs to be replaced, etc. The key here is once you get the file open you need to keep it open and preserve the file stream until you are ready to shutdown or encounter a recoverable exception and restart. Also whatever stragey you employ for archiving log files and naming log files should be considered part of startup as well as part of some event based trigger, e.g, midnight, file size exceeds 1MB, etc.
Incoming Log Requests:
Decode the message, format the message, write it to the log file. Since we're talking I/O make sure you are using Try/Catch blocks in the event your file stream goes south suddenly. And here is where you would start the write with the m_cFileStream.BeginWrite() call. When the write completes the underlying driver tickles the CLR which in turn posts an IAsyncResult to your End???? handler.
A note about flushing the cache. If there is any need to flush the cache instead of leaving it up to the OS, then you should employ your threshold for flushing here. For example after every 100 messages you decide. That said there is no real need unless you can come up with one.
Timer or Event based Action:
As noted your Logger should worry about file size and rollover. Based on some timer firing, some file size grown past its threshold, or whatever, your application should take this into account. Since your application should be Async I do NOT recommend doing this in a While loop in the mainline code.
Shutdown Sequence:
OK, Here is where you close the sockets and lastly the file. In otherwords, this is the appropriate place to be calling m_cFileStream.Close(). Before you do that however make sure all client connections and Shutdown and Closed first.
Async vs. Threads
In this reponse I have advocated the Async model. But if you enjoy the threaded approach you can definitely implement a logger that way too. Suffice to say its another can of worms though.
Good Luck with it!
Mike
|
|
|
|
|
i need an API for Information Extraction (from text)
in C#.
if somebody can help me and send me some references about it
For example for Java there is a software called Extractor.
i hope somebody will help me
thhank you all
eyalso
|
|
|
|
|
Java Extractor is an unzipping application.
Is that what you are after, or are you looking for a Regex (find/sort text in text") approach ?
|
|
|
|
|
hi
i am looking for Regex (find/sort text in text") approach
in C#
eyalso
|
|
|
|
|
|
Not sure if this should be here or in the C# forum but here goes. I want to create an out-of-process COM component (like an ActiveX exe in VB6) in C# - does anybody know how to do this? I've created in-process COM component with no problems. Any ideas/links would be most appreciated.
Cheers
|
|
|
|
|
Hi Friends,
In my C# application, i have tried to serialize some structured data using Soapformatter. While calling the serialize funciton i got the exception message like
"mscorlib....The type <appname>.<structurename> in Assembly <appname>, Version=1.0.2162.36249, Culture=neutral, PublicKeyToken=null is not marked as serializable."
What is the meaning of the message and how can i solve this issue ?
Regards,
Prakash.
A Journey of thousand miles must begin with a single step.
|
|
|
|
|
The data you want to serialize does not has the Serializable attribute. The formatter wants something like this:
[Serializable()] public class SomeData {...}
Pompiedompiedom...
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.."
-- Mark McCormick
|
|
|
|
|
Thanks.... i solved my issue by putting the serializable attribute in fornt of the class name.
Regards,
Prakash.
A Journey of thousand miles must begin with a single step.
|
|
|
|
|
Suppose I have 2 multi dimensional arrays that I want to save to the hard drive, but I can only save one or the other, never both.
I have looked for a solution on the web and some books, and if I understood it correctly the code below should work, however it doesnt (maybe because I should use a constructor?).
Any advice besides serializing each array one by one?
Thanks for helping a complete novice.
[Serializable]<br />
public class test<br />
{<br />
<br />
public static int[,] arrtest = new int[1000, 1000];<br />
public static int[,]lalala = new int[2000, 1000];<br />
}<br />
public class read<br />
{<br />
public void doWrite()<br />
{<br />
test per = new test();
IFormatter formatter = new BinaryFormatter();<br />
Stream str = new FileStream("D:\\guardar.dat", FileMode.Create, FileAccess.Write);<br />
formatter.Serialize(str, per);
str.Close();<br />
<br />
}<br />
}
|
|
|
|
|
Hi!
Why did you declare your arrays as static ?
When you convert your arrays into instance members, they should get serialized.
Another problem though: You're creating a new test right before serializing it. That way your arrays will be empty (i.e. filled with default value 0). I can't imagine that's what you want...
Regards,
mav
|
|
|
|
|
Hi! Is there a way to use JPanel on Windows.Forms? If this is not possible, can i open a java window as a mdichild of my c# application?
Thanks..
|
|
|
|
|
Hello gurus,
I'd like to know how to translate this C code in C#?
<br />
WORD calc=0;<br />
WORD tmp=0;<br />
<br />
for (int i=0; i<m_nBlockSize; i++)<br />
{<br />
tmp = ((calc ^ data[i]) & 0xF) * 0x1081;<br />
calc = tmp ^ (calc >> 4);<br />
tmp = ((calc ^ (data[i] >> 4)) & 0xF) * 0x1081;<br />
calc = (tmp ^ (calc >> 4)) & 0xFFFF;<br />
}<br />
<br />
return calc & 0xFFFF;<br />
The return value is a 16 bytes value (a WORD ).
Thanks in advance.
Best regards.
There is no spoon.
-- modified at 8:44 Friday 2nd December, 2005
|
|
|
|
|
That code won't compile. What kind of for loop is that?
But I have the answer:
return (ushort)16;
Regards,
Alvaro
I cannot take anything the Bush administration does seriously. The corruption, the cynical disregard for humanity, the cronyism and incompetence, all wrapped in a slimey flag of ultra-marketed nationalism repulses me. -- consdubya from fark.com.
|
|
|
|
|
This loop calculates a 16 bits CRC.
It works perfectly in C++, but I have conversion problems in C#. The function must return a 2 bytes value.
I have converted like this, but I'm not sure it's the correct C# translation:
<br />
private Int16 CheckCRCHP(Byte[] data)<br />
{<br />
Int16 calc = 0;<br />
Int16 tmp = 0;<br />
<br />
for (int i = 0; i < _blockSize; i++)<br />
{<br />
tmp = (Int16) (((calc ^ data[i]) & 0xF) * 0x1081);<br />
calc = (Int16) (tmp ^ (calc >> 4));<br />
tmp = (Int16) (((calc ^ (data[i] >> 4)) & 0xF) * 0x1081);<br />
calc = (Int16) ((tmp ^ (calc >> 4)) & 0xFFFF);<br />
}<br />
<br />
return (Int16) (calc & 0xFFFF);<br />
}<br />
Best regards.
There is no spoon.
|
|
|
|
|
WORD = ushort, the rest should compile fine as far as I can see.
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Ok, it compiles and works fine with ushort
Thanks
There is no spoon.
|
|
|
|
|
Bitmasks in C# are pretty simple, as noted you can use ushort or ulong depending on how many bits you need. That's probably the closest to C and gives you rapid conversion. However it's a good idea to avoid magic numbers and use constants instead. Less you get run over by the proverbial train and some poor soul has to figure out the rationale for your bit manipulations.
As an alternative you can also define your Bit Mask as a class with an enum. In c# an enum is equivalent to a uint, i.e. 32 bits. Thus you can mask off the high order bytes or take advantage of the extra bits for additional states. Normally I would only use a class if the bit manipulations were esoteric or voluminous. Nevertheless here is a quick example relative to your sample above:
class MyMaskOps<br />
{<br />
public enum eMyMask<br />
{<br />
None = 0,<br />
State1 = 0x1,<br />
State2 = 0x2,<br />
State3 = 0x4,<br />
StateRed = 0x200,<br />
StateBlue = 0x400;<br />
StateOrange = 0x800,<br />
AllStates = 0xe07,<br />
Multiplier = 0x1081,<br />
AllBits = 0xffff<br />
}<br />
<br />
private eMyMask mCalc = eMyMask.None;
private eMyMask mTemp = eMyMask.None;
<br />
public eMyMask Calc<br />
{<br />
set { mCalc |= value; }<br />
get { return mCalc; }<br />
}<br />
public eMyMask Temp<br />
{<br />
set { mTemp |= value; }<br />
get { return mTemp; }<br />
}<br />
public eMyMask Xor(eMyMask msk, uint bits)<br />
{<br />
return (msk ^ bits) & 0xf;<br />
}<br />
public eMyMask And(eMyMask msk, uint bits)<br />
{<br />
return msk & bits;<br />
}<br />
public eMyMask Mul(eMyMask msk, uint bits)<br />
{<br />
uint tmp = msk * bits;<br />
return (eMyMask)tmp;<br />
}<br />
public ushort DoCalc(uint[] data)<br />
{<br />
mCalc = eMyMask.None;<br />
mTemp = eMyMask.None;<br />
<br />
for (int i = 0; i < data.Length; ++i)<br />
{<br />
mTemp = Mul(Xor(mCalc, data[i]), eMyMask.Multiplier);<br />
mCalc = Xor(mTemp, (mCalc >> 4));<br />
mTemp = Mul(Xor(mCalc, (data[i] >> 4)), eMyMask.Multiplier;<br />
mCalc = And(Xor(mTemp, (mCalc >> 4)), eMyMask.ShortBits);<br />
}<br />
<br />
ushort result = (ushort)And(mCalc, eMyMask.ShortBits);<br />
return result;<br />
}<br />
}
Mike
|
|
|
|
|
Hi Mike,
I don't play much with CRC functions, actually I took it from another project
But for sure, your sample code looks to me as the C# way I like it
Also, for doing an 8 bits CRC I don't think that a class could be done as the loop is 2 lines, but maybe the class you gave me can be extended with other CRCs. It could be interested to develop a class with most CRCs inside (maybe it already exist)
best regards.
There is no spoon.
|
|
|
|
|
How do i marshal an array of strings. I'm trying to do it as below.
I have a I C dll with a C structure I want to pass
[code]
#define MAX_KEYWORDS 50
#define KEYWORD_LENGH 20
struct KeywordListStruct
{
char ks_keywords[MAX_KEYWORDS][KEYWORD_LENGH];
};
[/code]
and a function I want to export
[code]
// ...
#define MYEXPORT_API __declspec(dllexport)
// ...
MYEXPORT_API int Tester(KeywordListStruct& testStruct)
{
int retval = 0;
// do stuff
return retval;
}
[/code]
and I want to call this from a C# app
[code]
namespace DllImports
{
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct KeywordStruct
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
Imports.KEYWORD_LENGTH)] public String keyword;
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct KeywordListStruct
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = Imports.MAX_KEYWORDS)] public KeywordStruct[] words;
}
public class Imports
{
DllImport("KeywordDLL.dll", EntryPoint="Tester", SetLastError=true,
CharSet=CharSet.Ansi, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern short Tester(ref KeywordListStruct rTest);
public const int MAX_KEYWORD_LENGTH = 20;
public const int MAX_KEYWORDS = 50;
}
[/code]
Which I call as follows
[code]
//...
KeywordListStruct kl = new KeywordListStruct();
//...
Imports.Tester(ref kl);
[/code]
but I get the following runtime error.
An unhandled exception of type 'System.TypeLoadException' occurred. Additional information: Can not marshal field words of type DllImports.KeywordListStruct: This type can not be marshaled as a structure field.
Can anyone help?
|
|
|
|
|
Hi friends,
Plz give me the code for calling child form in which the parent windows form dispose as the child takes the control............
Thanks,
Sandeep
+919891027854
|
|
|
|
|
You can't do that. If you dispose the parent form, all it's child die with it.
If you dispose the startup form, your entire application closes with it.
I take it you're trying to implement some kind of splash screen or login form?
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|