|
oh ok, I understand now, thanks.
by the way, great Dusk at Sea World photo, I love photography and this picture is very good
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
--Rich Cook
|
|
|
|
|
i want to built a socket program like a chating program in yahoo messanger.I want to know how i treat both modules(means is there are two threads reading & writing thread at both modules or something else?if u have some article about this ,tell me or give me some hints.
mughalali
|
|
|
|
|
There are hundreds if not thousands of examples of chat applications out there and even right here on CodeProject. Just http://www.google.com[^].
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
i want to built a socket program like a chating program in yahoo messanger.I want to know how i treat both modules(means is there are two threads reading & writing thread at both modules or something else?if u have some article about this ,tell me or give me some hints.
mughalali
|
|
|
|
|
I have a problem stopping the server process. Everything seems fine when i stop my multithreaded server, no exceptions when i shut down the application. But the server program wont stop executing. I cant find any more threads that i have forgot to abort. Still if i try to abort the CurrentThread in the end of the program i get an AbortException that cant be handled by the application. Is there something else i can try?
Michelangelo
|
|
|
|
|
Without seeing any code, all I can tell you is that to handle the seemingly uncatchable ThreadAbortException , handle the current AppDomain's AppDomain.UnhandledException event to catch ThreadAbortException s (or any others you might want caught).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
I presume your server is using a thread which continually listens on a port by running it on a thread.
Currently when you close your program down the lightweight process (the thread) continues to be executed.
In the applications "closing" event you need to ensure all threads running are interrupted and cleaned up.
Dont use the STOP method as it is very bad.. you could end up with part written data for the instance of the program.
Theres a pretty complete guide here on threads:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchusingthreads.asp[^]
Which is worth a glance, but for the quick solution do this in the closing event for the main form/application:
threadinstance.Interrupt();
|
|
|
|
|
The threads you have created may be foreground threads. These threads will keep the process running until they stop. Try setting the Thread.IsBackground property to true. Once all foreground threads have stopped executing the runtime will abort all background threads automatically.
|
|
|
|
|
I'm calling one of my own dll functions in an unmanaged (C++) dll from a managed (C#) dll. I am passing an array of structures, as an [In, Out] parameter, 512 elements long. It enters the function okay, does the necessary work, then on return from the function, i.e. when you step out of the scope of the unmanaged function in the debugger, the managed side catches a System.OutOfMemoryException with no additional info.
The C++ Structure is:
<br />
struct AudioFormat<br />
{<br />
unsigned long type;<br />
<br />
wchar_t name[MAXCHARS];<br />
<br />
unsigned short formatTag;<br />
unsigned short channelCount;<br />
unsigned long samplesPerSec;<br />
unsigned long avgBytesPerSec;<br />
unsigned short blockAlign;<br />
unsigned short bitsPerSample;<br />
unsigned short additionalBytes;<br />
};<br />
The C# structure is:
<br />
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]<br />
public struct DataFormat<br />
{<br />
public MajorDataType majorType;
<br />
public string name;<br />
<br />
public ushort formatTag;<br />
public ushort channelCount;<br />
public uint samplesPerSec;<br />
public uint avgBytesPerSec;<br />
public ushort blockAlign;<br />
public ushort bitsPerSample;<br />
public ushort additionalBytes;<br />
}<br />
The C++ function declaration is:
<br />
void DSDataTranslator::FilterGraph::AllAvailableFormats(AudioFormat *availableFormatsParam, unsigned long &availableFormatsLenParam)<br />
The C# function declaration is:
<br />
[DllImport("DSDataTranslator.dll", EntryPoint = "?AllAvailableFormats@FilterGraph@DSDataTranslator@@SAXPAUAudioFormat@2@AAK@Z", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]<br />
private static extern void AllAvailableFormats([In, Out] DataFormat[] availableFormats, ref uint availableFormatsCount);<br />
The C# call is:
<br />
uint maxCount = (uint)DataTranslatorConsts.MAXFORMATS;
<br />
DataFormat[] availableAudioFormats = new DataFormat[maxCount];<br />
<br />
for(uint index = 0; index < availableAudioFormats.Length; index++)<br />
{<br />
availableAudioFormats[index].name = "";<br />
}<br />
<br />
try<br />
{<br />
AllAvailableFormats(availableAudioFormats, ref maxCount);<br />
}<br />
catch(System.NullReferenceException e)<br />
{<br />
Debug.Assert(false);<br />
}<br />
catch(System.OutOfMemoryException e)<br />
{<br />
Debug.Assert(false);<br />
}<br />
Can anybody please help as I cannot for the life of me figure out what the issue is here? I certainly know that in the catch block if I examined the contents of availableAudioFormats it's garbage, whereas before the call everything had been intialized to 0. If anymore info is required, please ask.
|
|
|
|
|
|
I've looked at the managed extensions for C++ required to instantiate structs or classes in C#, I believe you use __gc, but unfortunately the method is declared as static in the .h file so it's not required and hence no joy there. Thanks for the reply though, and I do intend to try using __gc later on in my development. In fact I'll give it a go anyway to see what happens. Also, you might be interested to know that if I increase the array size to 4096 (rather large, I know but it is to cover all the different possible codecs and formats that a system might have installed) the exception caught is NullReferenceException.
|
|
|
|
|
The problem has nothing to do with Managed C++ extensions since you're P/Invoking the method from C#. I'm not sure what you were trying to get at, really.
If the native method is declared as static, then off-hand I really don't see what's wrong. Your declarations look correct, except that you're not declaring a fixed size for your string field. See the MarshalAsAttribute documentation for different ways of doing this, like using the SizeConst and setting that to the value of MAXCHARS you used in your native declaration.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Sorry, thought I'd read somewhere on MSDN that you can easily include classes defined in C++ by declaring them with the __gc managed extension, haven't really looked into it though.
I've already tried the [MarshalAs(UnmanagedType.LPWStr, SizeConst = MAXCHARS)] attribute and again there was no rejoicing. In fact I think I've just about tried everything, including adjusting my seat position and the way I tie my shoe laces but none of it seems to work... maybe a low carb diet might work... I'm really grasping at the proverbial straw here. The only way I see out of this at the moment is to pass in arrays of the same size, one for each variable in the structure that I need to set. That's really filthy, I'm going to wash my mouth out with carbolic soap, but if it works, it works.
|
|
|
|
|
You can, I just don't know why you brought it up in this discussion. If you declared your struct as a managed struct, then you wouldn't need to P/Invoke anything anyway.
One thing you could try is to fix the memory address of the array before passing it, also changing your first param to IntPtr , something like this:
GCHandle handle = GCHandle.Alloc(myArray);
MyMethod(handle.AddrOfPinnedObject(), ...);
handle.Free(); This makes sure that the GC doesn't move it while in use, which can lead to all sorts of problems.
I do a lot of unmanaged interop and frankly have never had this problem, so I'm sorry I can't be of more help. Believe me, it's bugging me now, too.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
More of an aside, in relation to your mention of instance methods, but it seems to have led to a possible solution by avoiding the use of P\Invoke by using the __gc declaration. In fact, with it being my own structure, there's no reason not to. Thanks again, I'll try this in the morning (this is the UK and it's nearly 23:00 and I've been staring at the screen for so long the walls of my house are covered in text). I'll update you to let you know how it goes but I've got a feeling it'll work.
|
|
|
|
|
Hello Heath,
after some time away and quite a bit of coding to wrap up my dll in C++ managed extension stuff I have perfect interoperability. The managed extensions make interop virtually seemless and the P/Invoke stuff more or less pointless for C++ stuff unless your only passing simple datatypes. Thanks for your help.
Luke
|
|
|
|
|
I'm still suffering from flickering problems and glitchy transparent corners (they show the background, not the controls underneath).
I think the solution to both problems could be to make a buffered bitmap. But i've no idea how to buffer the Theme as it's drawn directly to the screen.
I played around with attempting to use the bitmap graphics object to create an hDC handle, but it didn't work, i just got black.
Any ideas what I could do?
Cata
|
|
|
|
|
You can enable double-buffering for a control easily by calling the following in your constructor:
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.DoubleBuffer | ControlStyles.UserPaint, true); This tells the CLR to call OnPaint in a separate thread to draw to an off-screen bitmap. When the painting is done, the image is flipped to the device context. Or at least, it's something close that that. This saves you from having to swap everything yourself and from using an incompatible bitmap if not created correctly.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I already tried the Double Buffer bit, but it appears that it doesn't work with API called window themes. I just get black.
Works fine if I am not using themes though. :/
|
|
|
|
|
Oh, also pay attention to the PaintEventArgs.ClipRectangle . You don't always need to paint everything. In larger controls, this can make a difference. Determine which elements of your control need painting using that property and only repaint those. This is something commonly overlooked but necessary for better performance regarding painting your UI elements.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I've found that i can reduce flickering dramaticaly on the left and top edges by overriding the on paint event, and not drawing the background. The right and bottom areas, where the re-size takes place, are still affected though, so i'm not entirely sure what to do about that. (This LOOKS like when i re-size, the background is rendered before the move, and then drawn over, causing the flickering, despire using the SuspendLayout method).
However, this causes anomalies on the corners where the background displays the desktop / window behind, as the alpha blending goes straight through the non rendered background.
I'd really be interested in rendering the theme bitmap, over the background control images, to a buffer first. If that's possible.
|
|
|
|
|
It appears that it only flickers on Right and Bottom when i shrink the object on resize. Making it larger looks fine.
Moving still produces minor problems.
The lack of background rendering still causes weird stuff to happen to the transparent parts tho.
|
|
|
|
|
Hi.
If I have an object "x", in MFC I can create an array of objects "x" with CObList or CPtrList. In C# how could I do that?, what collection should I use?.
ArrayList??.
My object should be derived from some interface like ICollection or IDictionary?.
My object, (named CParameter), for now, have only two data members: ID and Description, and I have another class, (named CSistem), that have a list of objects CParameter.
Thank you.
|
|
|
|
|
You can create an array just like you can in C/C++:
x[] xes = new x[100]; An array (derives from Array at runtime) implements IList , which implements ICollection , which implements IEnumerable ). You could also use an ArrayList which implements the same interfaces. If you want to have your own typed collection, you could extend CollectionBase which implements all those interfaces or just make one from scratch.
Also, don't prefix your classes with "C". This old notation should not (i.e., that doesn't mean "must not") be used in .NET development. If you look at the FCL (Framework Class Library), you see that there is no Hungarian notation. This is to keep everything consistent, which is important in many development environments (especially RAD environments).
See Naming Guidelines[^] in the .NET Framework SDK for more information.
To serialize arrays, you have two options. One is runtime serialization, and is the most powerful. This uses classes and other elements from the System.Runtime.Serialization namespace and child namespaces. Both a BinaryFormatter and a SoapFormatter are provided in the FCL.
There's also XML serialization, which uses classes and other elements from the System.Xml.Serialization namespace. This is less powerful and offers less control (if you stick to what the documentation says and don't implement the IXmlSerializable interface).
For a discussion of both - and examples using each - see Serializing Objects[^] in the .NET Framework SDK.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
|