|
It has no sense in using a structure like that.
There is no need for you to "group" fields inside a structure, becouse you already have them inside your class.
Maybe you should read some article that covers the OO aproach to desigening programms
Q:What does the derived class in C# tell to it's parent?
A:All your base are belong to us!
|
|
|
|
|
To add, you may want to consider caching the settings instead of reading them from the registry each time. If they can be changed throughout your application, then have additional methods on this class that can reset some flag so that the values would be read and cached again.
Also, use a private constructor so that callers can't instantiate this class since that would serve no purpose. In .NET 2.0, you don't need to do this if you use static classes (prefix your class declaration with static ). This will be out early next year, just FYI.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hello:
Thanks for responding. I'm loading the values into the global properties one time at application startup. In that instance, would caching the values give me any benefit?
|
|
|
|
|
Well first, you need to expose them. You're simply setting them on a struct, which goes out of scope so they can never be read again. While I don't particularly find this approach to settings at all attractive from an OO standpoint, the least you can do is provide static properties on this class using the guidelines I outlined. What you're doing right now won't work at all because the struct simply goes out of scope like in the rest of your thread the other replier mentioned.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi, I think in a near time you'll hate me, I'm porting code from C to C#, I've this structure define in unrar.h
<br />
struct RAROpenArchiveDataEx<br />
{<br />
char *ArcName;<br />
wchar_t *ArcNameW;<br />
unsigned int OpenMode;<br />
unsigned int OpenResult;<br />
char *CmtBuf;<br />
unsigned int CmtBufSize;<br />
unsigned int CmtSize;<br />
unsigned int CmtState;<br />
unsigned int Flags;<br />
unsigned int Reserved[32];<br />
};<br />
and should be used by unrar.dll's function
HANDLE PASCAL RAROpenArchiveEx(struct RAROpenArchiveDataEx *ArchiveData);
I tryied using mashalling to mirror dll's structure in C#, but invoking RAROpenArchiveEx it hangs up, supposing due to the pointer to ArchiveData....
How should I go on? I don't know how to convert
unsigned int Reserved[32];
wchar_t *ArcNameW;
thanks....
|
|
|
|
|
Paolo Ponzano wrote:
Hi, I think in a near time you'll hate me, I'm porting code from C to C#, I've this structure define in unrar.h
I think in a near time you'll hate me, but why aren't you using managed C++? Simply write a bunch of wrapper classes, compile with /CLR, and you're done!
Due to technical difficulties my previous signature, "I see dump people" will be off until further notice. Too many people were thinking I was talking about them...
|
|
|
|
|
I don't know...... I started with C# and since I also have applied a lot of improvement I don't want to redo all again =)
May I only do the unpacking routine in C++ managed and all the rest in C#?
|
|
|
|
|
Thats the idea
|
|
|
|
|
but have I to create a new project and write a dll that will be invoked? or what? can I use C++ code in a C# project? I don't think so.....and I don't also how to let C# to use C++ code...thanks again
Paolo
|
|
|
|
|
It should look something like this:
[StructLayout(LayoutKind.Sequential)]
public struct RAROpenArchiveDataEx
{
[MarshalAs(UnmanagedType.LPStr)] string ArcName;
[MarshalAs(UnmanagedType.LPWStr)] string ArcNameW;
[MarshalAs(UnmanagedType.U4)] int OpenMode;
[MarshalAs(UnmanagedType.U4)] int OpenResult;
[MarshalAs(UnmanagedType.LPStr)] string CmtBuf;
[MarshalAs(UnmanagedType.U4)] int CmtBufSize;
[MarshalAs(UnmanagedType.U4)] int CmtSize;
[MarshalAs(UnmanagedType.U4)] int CmtState;
[MarshalAs(UnmanagedType.U4)] int Flags;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=32)] int[] Reserved;
}
[DllImport("rar32.dll")]
private static extern IntPtr RAROpenArchiveEx(ref RAROpenArchiveDataEx data);
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
thanks a lot... but how I declare in my method the structure??
this way -> RarOpenArchiveDataEx data = new RarOpenArchiveDataEx()???
or since in dll's function RAROpenArchiveEx I've a ref to that structure I need to use something I don't know?
thanks again
Paolo
|
|
|
|
|
Create an instance of your struct and pass it to your method:
RAROpenArchiveDataEx data = new RAROpenArchiveDataEx();
IntPtr retVal = RAROpenArchiveEx(ref data);
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
|
Hello!
I need some suggestions, i need a control that allows me to print data from a dataset in a way similar to the datagrid (columnheaders and so on), but the user should not be able to write anywhere in the control, and when pressed on it should only mark the whole line/row.. not the clicked post as in datagrids.. can this be solved with datagrids or are there another component that can fix this?
|
|
|
|
|
I think if you simply set ReadOnly property of DataGrid to true you will get what you want.
Mazy
"I think that only daring speculation can lead us further and not accumulation of facts." - Albert Einstein
|
|
|
|
|
For physical printing of the MS DataGrid, you can use ComponentOne's Preview.NET control. If you need a new grid component, ComponentOne's FlexGrid.NET will allow you to set it up so that when a user clicks in the grid, the entire corresponding row is selected. Our FlexGrid.NET component has printing functionality also. For more information, please visit http://www.componentone.com[^].
Suelinda W
Customer Engagement, ComponentOne LLC
|
|
|
|
|
hai there,
in my project i need to read all ip address which is avilable in my local network. after that i need to control the host machine(remote desktop sharing).Appart that i need to read some data from that the host machine hold.
is it possible to read the ip table which is avilable in server from a client machine ?
sreejith
hai, feel free to contact
Sreejith SS Nair
|
|
|
|
|
sreejith ss nair wrote:
read the ip table which is avilable in server
|
|
|
|
|
what you mean by that
|
|
|
|
|
sreejith ss nair wrote:
read all ip address which is avilable in my local network
Your going to have to ping and scan the ports of each IP address in your network to build this table.
sreejith ss nair wrote:
control the host machine(remote desktop sharing).
A LARGE project ... You will need to have a server on the target machine that can show your client app the desktop image and push in mouse and keyboard clicks. This server will also have to have some file transfer capabilities.
sreejith ss nair wrote:
is it possible to read the ip table which is avilable in server from a client machine
What server? No machine keeps track of used IP addresses. Unless, of course, it's a DHCP server, and even then, a machine can use an IP address that the server did not dole out to it.
Is it worth the thousands of dollars your going to invest in writing this thing or is the money better spent on an off-the-shelf package with the same capabilities?
RageInTheMachine9532
|
|
|
|
|
Hi,
I'm trying to run an insert statement continously for 24 hrs, data is coming from different Ip/PORT (100). Could somebody please help me by checking my codes if there's any way I can do to improved my locking so that I'll not be able to get the message below. Before the execution of insert is done another thread is getting the adapter that's why the current insert always fails.
CODE:
DBCommand = new SqlCommand(strInsert,DBConnect);
Monitor.Exit(DBCommand);
try
{
if (DBConnect.State.ToString() == "Closed")
{DBConnect.Open();}
DEBUG.PRINT("BEFORE EXECUTE:" + straddr);
DBCommand.ExecuteNonQuery();
DEBUG.PRINT("AFTER EXECUTE:" + straddr);
DBCommand.Dispose();
DBCommand.Connection.Close();
DBConnect.Close();
Monitor.Exit(DBCommand);
}
TRACE DATA FOR CLEARER INFORMATION:
BEFORE EXECUTE: 172.30.57.125:21307
INSERT ERROR1 172.30.98.249:20417==? insert into rop values
System.InvalidOperationException: There is already an open DataReader associated with this Connection which must be closed first.
at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean executing)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at ossrop.Services.ossAdminService.SaveToSQLDB(String strFile, String strOpt, String straddr)
AFTER EXECUTE: 172.30.57.125:21307
|
|
|
|
|
Define your SQL connection as a static instance, this will allow u to check whether its open or not in a threadsafe manner (without having to resort to locks).
|
|
|
|
|
Don't resort to static methods to solve a problem that's pretty simple as leppie suggests. I mean, it may be a good way to go, but to use it simply to paint-over a small problem isn't a good way to fix anything. That's shoddy programming and will bite you in the ars at some point more than likely.
Instead of using DBConnect.State.ToString for a case-sensitive string comparison (not a good idea), use bitwise operators to determine if the connection is closed, something like this:
if ((DBConnect.State & ConnectionState.Closed) != 0) return; Several enumeration members can be set at once (this could always be true when an enumeration is attributed with the FlagsAttribute ) so you need to mask any values and see if they're set.
For instance, ConnectionState.Closed = 0, and ConnectionState.Broken = 16. These could both be set at the same time.
0 = 00000000
16 = 00010000 If both were set, the State property would be set to 16, or 00010000. If you use a bitwise AND to mask Closed , then it looks like this:
State = 00010000
Closed = 00000000
& = 00000000 = 0 Without AND'ing it, the result is 16 which does not equal 0.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Health,
Thank you, I got your point. That's why i'm having messages that connection is already open even though I already closed it or the other way.
Anyway, Thank guys for all your help. I was able to fix my problem by using the Monitor class. It's under System.Threading. I locked the connection before executing the insert statement and then unlock unlock it once it's done. In that case the succeeding insert will not be able to get the 1st connection until it's release.
for other guys who will have the same problem.
CODE:
DBCommand = new SqlCommand(strInsert,DBConnect);
Monitor.Enter(DBConnect);
try
{
if (DBConnect.State.ToString() == "Closed")
{
DBConnect.Open();
}
DBCommand.ExecuteNonQuery();
DBCommand.Connection.Close();
DBConnect.Close();
Monitor.Exit(DBConnect);
}
catch (Exception objerr)
{
System.Diagnostics.EventLog.WriteEntry("ossrop", "Insert Failed : " + objerr.Message);
}
PS: If you have comments with my solution, feel free to modify it so that other people whill read this email in the future will get the correct solution.
|
|
|
|
|
Yes, I do have a comment: you aren't using the Monitor correctly. If an exception is thrown, your Monitor is never signalled and your app will handle on the next request to lock the monitor!
Instead, use the lock statement like so:
lock (DBConnect)
{
} ...which, FYI, gets compiled as:
Monitor.Enter(DBConnect);
try
{
}
finally
{
Monitor.Exit(DBConnect);
} See the difference? If an exception occurs, no matter what the monitor is signalled and any exception is either handled within any nested catch blocks, or is passed up the caller stack.
Microsoft MVP, Visual C#
My Articles
|
|
|
|