|
One common method is to have a static collection property on some class that all your code can access like so:
public sealed class DB
{
private static SqlConnection connection;
private static object syncRoot = new object();
public static SqlConnection Connection
{
get
{
if (connection == null)
lock (syncRoot)
if (connection == null)
connection = new SqlConnection(
ConfigurationSettings.AppSettings["ConnectionString"]);
return connection;
}
}
} The caller is still responsible for opening and closing the connection, but you should leave it that way so you don't leave connections open longer than you have to. This can degrade performance (and limit connections too much) on your database server. Also note that I use SqlConnection only as an example. You could use any connection class, or even the ADO.NET interface IDbConnection if you wanted to. The latter idea would fit nicely with using a provider pattern.
I also used an example where the connection string is placed in the appSettings section of your app's .config file, which is also another good idea.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I try sending a message to a queue and AppSpecific contains the type of my message, the body is object of a class (Order). and i set formater for the queue.
_mq.Formatter = new XmlMessageFormatter(
new Type[] { typeof(Order) });
When I receive a message from the queue, I just got the body, not the info in AppSpecific field.
I tried to set
DefaultPropertiesToSend.AppSpecific
but don't know which info should i give to it.
If i send AppSpecific along with the message body do i have to change the formater to receive AppSpecific or just leave
it the same as above.
Could anyone here show me how to solve this problem.
Thanks,
Huy
|
|
|
|
|
Hello,
Can anybody tell me how to use serial communications rs232 and rs485 in the .net compact framework. I found some commercial links, but is there not a cheaper solution?
Thanks,
Jelle
|
|
|
|
|
|
Can anybody tell me how to hide the taskbar (with windows icon on the left down corner) in the .Net compact Framework?
Please help.
Jelle
|
|
|
|
|
I've never written for Windows CE or using the .NET CF, but I think you might want to check into P/Invoking this[^] API call.
RageInTheMachine9532
"...a pungent, gastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Hello,
This is a C++ function. How can I access it in C#?
Jelle
|
|
|
|
|
Use P/Invoke (platform invoke). Please see the PInvoke.net wiki[^] for more info.
#include "witty_sig.h"
|
|
|
|
|
Hello,
I am using c#. How can I include this in c#?
Jelle
|
|
|
|
|
One more time... P/Invoke the function call.
See this[^] MSDN document for a little primer on P/Invoke.
RageInTheMachine9532
"...a pungent, gastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Hello Dave,
I found:
[DllImport("aygshell.dll")]
static extern uint SHFullScreen(???? hwndRequester, ??? dwState);
I do not know how to call this function with what parameters.
I found on msdn: SHFullScreen(HWND hwndRequester, DWORD dwState);
DWORD should match Int32, but HWND? InPtr is a good guess?
Finally, how can I find out what the dwstates are? I found on MSDN SHFS_SHOWTASKBAR, but what is the value of this state?
Please help.
Jelle
|
|
|
|
|
HWND and DWORD will both translate to an Int32 in this case. It really depends on the function your calling.
To get the hWnd of your form, just use Me.Handle. The dwState constants can be found in the Aygshell.h file. Just do a search on your hard drive to find it. The constants will be written in C++ so you'll have to translate them into C# code so you can use them in your project.
RageInTheMachine9532
"...a pungent, gastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I found the constants on my harddrive. However, I cannot get the handle with me.handle. Also this.Handle does not work in .NET compact framework.
Please help.
///SHFS_SHOWTASKBAR 0x0001
///SHFS_HIDETASKBAR 0x0002
///SHFS_SHOWSIPBUTTON 0x0004
///SHFS_HIDESIPBUTTON 0x0008
///SHFS_SHOWSTARTICON 0x0010
///SHFS_HIDESTARTICON 0x0020
|
|
|
|
|
Hi,
I am getting an InvalidOperationException with the message:
"Cannot call Invoke or InvokeAsync on a control until
the windows handle has been created."
This message occurs only at design time (so far) when we build and run the
project no errors related to this occur. Has anyone found the cause of this?
Thanks in Advance
|
|
|
|
|
What is your control doing when it is created that requires the Invoke call?
You really shouldn't be calling Invoke in Design mode...
RageInTheMachine9532
"...a pungent, gastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Actually, an instance of the control exists even at design-time but you must still make sure the handle exists (IsHandleCreated ) before invoking anything that requires a valid window handle.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
You're trying to call invoke on the control before the control's window is created and the handle is assigned.
I get around this with an if/else
if (myControl.IsHandleCreated)
{
}
else
{
}
Grim (aka Toby) MCDBA, MCSD, MCP+SB
|
|
|
|
|
Another common way is if the handle isn't created (see IsHandleCreated ) to queue the operation and invoke it when the HandleCreated event if fired. Many classes in the .NET FCL (especially in Windows Forms) do this.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I've never actually had to do it, so I'm curious as to how it gets queued? I'd tear into the class code with the disassembler but I'm nowhere near a machine with VS.NET on it at work.
RageInTheMachine9532
"...a pungent, gastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
All you need is the .NET Framework SDK (which requires the .NET Framework). VS.NET just installs the SDK if you opt for it, but it doesn't come with just the SDK as well.
If you were to set a property for example, the set accessor would contain something like this:
if (IsHandleCreated)
{
SendMessage(Handle, SOME_MESSAGE, 0, value);
fieldSet = true;
}
else
{
fieldSet = false;
}
fieldName = value; In the override to OnHandleCreated , you'd have something like this:
if (!fieldSet)
SendMessage(Handle, SOME_MESSAGE, 0, fieldName); To do this with delegates wouldn't be a whole lot different since you can store the delegate in a field or an IList and invoke it later.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I thought of that, but was quickly shot down. I can't install it on anything where I work...
RageInTheMachine9532
"...a pungent, gastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Nevermind. I found a couple of examples of the technique.
I've been up for four hours and still can't keep my mind or eyes open...
RageInTheMachine9532
"...a pungent, gastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Hi all,
I need some advice on the best method to impelemt some functionality.
I have a reader obj that reads all the files in a folder, running in a thread. After each file has been read I call a processor obj to manipulte the data and fill a database. I'd like to process one file while reading the next, but limit the processor to only one instance (to prevent duplication of data in the DB). If the reader finishes reading before the processor it should wait until the processor is done its current data load, but if the porcessor has finished then the reader should make a new processor and then continue to read the next file.
So whats the best method of controling the reader depending on the state of the processor?
I should point out that I'm fairly new to C# (six months)
Thanks
|
|
|
|
|
If your only going to be using a single instance processor, why read ahead of it? You're not gaining any real performance benefit because of the single threaded nature of your design. One thread is constantly waiting for another to complete it's work. The FileStream classes already do read/write caching on their own, so why duplicate its effort?
If you could use a multiple instance processor, instead of a single, THEN you'd see the performance gain. Also, your reader object shouldn't be reading the files itself. Your design should have an engine that hunts down files, then passes the filename to a threaded instance of the processor. That way, the processor thread is doing the reading and processing of the file while your engine goes on it's merry way to launch another processor on the next file. The thread pool could be used for such a task. That way, you wouldn't have to include very much code to do thread management.
I only say this because I did something similar to this a few years ago. But in my case, the database made sure I wasn't duplicating data, not my processor objects.
RageInTheMachine9532
"...a pungent, gastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Thanks for your comments.
The majority of the time the app will be run on a single instance processor machine, but there may be a case where the volume of data is so large a multi machine could be used.
I wish the database would control data duplication, but the data is so poor and badly supplied that I have to do all the checking that I can. For example the data is in XML format and one 33mb file had over 10,000 violations against the schema, but it was well formed!
In the interests of simplicity I leave it as single threaded.
|
|
|
|