|
Hi all,
What is the best methodology for messaging (IPC) betweeen two applications
on the same PC by programming in C# ?
thanks
Patrick
|
|
|
|
|
.NET Remoting[^]
This describes a contract between two or more AppDomains with which to communicate. Think of it as a more advanced version of XML Web Services (though on the same machine - or even remote machines - using a BinaryFormatter with a TcpChannel is much faster to serialize and send through the wire - but it's an extensible system).
In .NET 2.0, a new IpcChannel will be supported for actual IPC calls (like for pipes, which you'd currently have to P/Invoke to use).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi all,
An interresting problem in the context of interprocess communication. Consider following code:
<br />
public void StartSepThread()<br />
{<br />
ThreadStart tsThread = new ThreadStart(RunServerSepThread);<br />
TestThread = new Thread(tsThread);<br />
TestThread.Name = "IPC.Test";<br />
TestThread.Start();<br />
}<br />
public void RunServerSepThread()<br />
{<br />
while(true)<br />
{<br />
(lot of code here...)<br />
}<br />
}<br />
The code above proves to be 50% less performant than following code:
<br />
public void RunServerSameThread()<br />
{<br />
while(true)<br />
{<br />
(exact same code here...)<br />
}<br />
}<br />
When running the server in the same thread, we are able to move 163 Mbit/sec of data between two running processes. However, when running the same code as a seperate thread, performance goes down by almost 35% to a level of 111 Mbit/sec.
Anybody any idea what this might be???
Thanks
|
|
|
|
|
Ummm... Just in case you have misssed this, try Thread.Sleep(0); immediately after you do TestThread.Start(); method. Do post the resultant performance.
MSDN documentation on System.Threading.Thread overview says "On a uniprocessor machine, the (child) thread does not get any processor time until the main thread yields."
|
|
|
|
|
Well... the performance gain is nothing.
I didn't mentio it in my code posted, but inside the While loop there is a blocking call. This is exactly the same code in the single thread and new thread code. So that doesn't make any difference.
I believe the CPU would go 100% if no blocking call was inside the loop and Thread.Sleep(0) was missing...
Thanks
Peter
|
|
|
|
|
Hi,
Does anyone know of a way to specify the number of decimal places in a fixed-point formatted string in C#?
This is how I was doing it in VC++(6.0):
CString thisString;
double val = 12.3456789;
thisString.Format(_T("Format to 4 decimal places %.4f"), val);
Much thanks for any help!
B
|
|
|
|
|
double newW = 12.345678;
string pw = string.Format("{0:N3}", newW);
//pw is now 12.345
|
|
|
|
|
As always it is very humbling to have the forest pointed out!
Thanks for the quick reply.
B
|
|
|
|
|
Try this
<br />
double MyFloat = 123.456;<br />
string MyString;<br />
MyString = String.Format("{0:00.00}", MyFloat);<br />
Console.WriteLine(MyString);<br />
or even better
<br />
double MyFloat = 123.456;<br />
Console.WriteLine(String.Format("{0:00.00}", MyFloat));<br />
Salil Khedkar [^]
|
|
|
|
|
I am making my first attempt at using the ToolBar. I have defined an ImageList, and put a 16x16 pixel in it, and associated the first button in my ToolBar with index 0. The problem is, the image that is already only 16x16 shows up on the ToolBar button as shrunken to an even smaller size. AutoSize is set to true for the ToolBar, and the ImageList shows the image size as 16x16.
Am I missing an important setting? What could be causing the incredible shrinking image? And on a tangent, why doesn't the Transparent property of ImageList work?
|
|
|
|
|
Even though the toolbox button size is automatic, what does it report in the PropertyGrid for the ToolBar.ButtonSize ? This should be something larger than 16x16 since a margin is required to draw the buttons both in 3D and flat (for when you hover) mode.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
The ButtonSize is shown as 24, 24. That should be plenty for a 16x16 image. The button is displayed with a gap of several pixels between the shading on the edges of the button and the shrunken image.
|
|
|
|
|
Yeah, that definitely sounds right. Did you set the ImageList.ImageSize property before or after you added the images? It's important to have all properties set before you add images, including the ImageSize and TransparentColor . If you didn't set these first, you'll need to remove your ImageList (i.e., delete it completely) and recreate it with the proper property values, then add your images.
If you did perform those operations in that order, than frankly I'm at a loss. You could try to unassign the ImageList from the control, close the designer (perhaps close VS.NET entirely), then re-open the control and reassign the ImageList . Sometimes - but rarely in my experience - the designer state gets a little "off" and needs a kick in the pants. I doubt this is the problem though; it's rare and is often caused by massive number of changes to the designer, especially when working with custom controls.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
That solved the problem! I made a new ImageList, with the right properties set, added the exact same image to it, then associated the image list with the toolbar. Now the image is behaving properly, including the transparency. Thanks!
|
|
|
|
|
I have an sbyte array with a length of 4. What these four bytes represent is a single 32-bit number.
I don't know how to convert the 4 sbyte elements into a single 32-bit number in C#. I could do this easily in C or C++ by casting a pointer to the first element as a pointer to Long and then dereferencing the pointer to get at the long.
How do I do this in C#?
BRCKCC
|
|
|
|
|
Use BitConverter.ToInt32
The bitconverter class has many handy conversion routines.
This should help.
Dies
|
|
|
|
|
Use the shift operator >> to move the individual values to the right place, then add them. If you're not familiar with it, read this page:
http://www.c-sharpcorner.com/Language/BitWiserOpsInCSCH001.asp[^]
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
Hi all,
I want to use MSMQ in C# to perform messaging sending between two applications and have a few questions as below,
1. will MSMQ in c# consume much resources(memory and CPU) while it runs ?
2. Is MSMQ exist on Window 98 and Window Me if .Net on those platform is installed ?
3. can the MSMQ messaging by C# be programmed between two window services, or between on window service and one exe app ?
Thanks
Patrick
|
|
|
|
|
You msut perform performance benchmarks and determine if the results are adequate for your needs. Most often managed code requires more memory but requires about the same number of CPU cycles as unmanaged code - though not always (this depends on a number of factors, especially demanding CAS permissions that cause a stack walk).
If you look at the bottom of all the class documentation, the system requirements are specified.
Since the .NET BCL defines the MSMQ client code, it does not require MSMQ to be installed (which is supported on Win2K and newer). The "Platforms" section under "Requirements" states that Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family is supported.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi,
I was reading the MCE SDK 2005, there is an example of MediaState, I’ve compiled it to test it on my MCE. I had to add the needed register component that i found on the SDK help file.
When i run the app, it does not get any information from the MediaCenter. What could be wrong? I think the COM object is not working properly because i cant read any information of MCE. Does anyone have a clue?
Greetings.
|
|
|
|
|
In order to diagnose and recommend solutions to your problem, you need to be more specific. What exception did you get? What was the stack trace of the exception? What was the detailed message?
If you're doing this from managed code, you need to include the interop assembly for the COM object that was generated automatically by VS.NET (or tlbimp.exe). This is usually named "Interop.Something.dll". This must be resolvable.
If you're not doing this with managed code - specifically C# - then you're in the wrong forum and need to choose the appropriate forum for your problem.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
I didnt get any exception, the code gives no errors. I wrote i managed code. I read in the SDK help file that i need to register the MSASState.dll with
[regasm.exe MSASState.dll /codebase] and placed the following registry
[HKEY_CLASSES_ROOT\CLSID\{380AA9F1-5047-40D0-8BE9-18AB489CA469}
\Implemented Categories\{FCB0C2A3-9747-4c95-9d02-820AFEDEF13F}]
@="Media Status Sink"
This was needed to read mediastate information about the Media Center.
I wrote a simple code that checks if the MCE is active or not active. I have loaded the MediaState.dll in to my app.
<br />
....<br />
MediaState mediaState = new MediaState();<br />
mediaState.MediaCenter.Started += new EventHandler(OnMediaCenterStarted);<br />
mediaState.MediaCenter.VolumeChanged += new EventHandler(MediaCenter_VolumeChanged);<br />
mediaState.Connect();<br />
if (mediaState.MediaCenter.Active)<br />
{<br />
label8.Text = "Media Center is Active";<br />
}<br />
else<br />
{<br />
label8.Text = "Media Center is NOT Active";<br />
}<br />
....<br />
I'm getting that the status is NOT active. I have tried the mediastatedisplay example that came with the MCE SDk 2005 on my MCE and that also cant read any information about MCE while it is running. I have no idea what could be wrong.
|
|
|
|
|
When you use /codebase with regasm.exe the path to the assembly is registered. This means your assembly must be in the same place. If you don't use /codebase, you must add the assembly to the GAC (requires it to be strong-named, which is always a good idea anyway).
So the code above is in the assembly exposed to COM (a COM-Callable Wrapper, or CCW)? I've not seen this example but I do know a lot about COM and know that COM won't load an EXE. Since this is obviously code for a Windows Forms components (well, judging by label8 ), I know that the Media Center won't load it.
The component category that you implenented is used by the Media Center to figure out which components to load. Somewhere, then, you need to get the running instance of your component that you registered.
Have you followed the SDK sample exactly?
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
My friend
The sample code I wrote and the sample came with the SDK is not executed by MCE. It's an windowsform based app that can read status information about the MCE. My example is not integrated in to MCE, it's running beside MCE on the background. To read status information about MCE i had to register the MSASstate COM sink. This component delivers the mediastatus about the MCE. I have checked the registery after the (regasm.exe MSASState.dll /codebase) and found the following registry items.
[HKEY_CLASSES_ROOT\CLSID\{380AA9F1-5047-40D0-8BE9-18AB489CA469}]
@="MSASState.MediaStatusSink"
[HKEY_CLASSES_ROOT\CLSID\{380AA9F1-5047-40D0-8BE9-18AB489CA469}\Implemented Categories]
[HKEY_CLASSES_ROOT\CLSID\{380AA9F1-5047-40D0-8BE9-18AB489CA469}\Implemented Categories\{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}]
[HKEY_CLASSES_ROOT\CLSID\{380AA9F1-5047-40D0-8BE9-18AB489CA469}\InprocServer32]
@="mscoree.dll"
"ThreadingModel"="Both"
"Class"="MSASState.MediaStatusSink"
"Assembly"="MSASState, Version=1.0.1740.26563, Culture=neutral, PublicKeyToken=a097a53b6b26c493"
"RuntimeVersion"="v1.0.3705"
"CodeBase"="file:///C:/WINDOWS/system32/MSASState.DLL"
[HKEY_CLASSES_ROOT\CLSID\{380AA9F1-5047-40D0-8BE9-18AB489CA469}\InprocServer32\1.0.1740.26563]
"Class"="MSASState.MediaStatusSink"
"Assembly"="MSASState, Version=1.0.1740.26563, Culture=neutral, PublicKeyToken=a097a53b6b26c493"
"RuntimeVersion"="v1.0.3705"
"CodeBase"="file:///C:/WINDOWS/system32/MSASState.DLL"
[HKEY_CLASSES_ROOT\CLSID\{380AA9F1-5047-40D0-8BE9-18AB489CA469}\ProgId]
@="MSASState.MediaStatusSink"
If i'm correct the settings are correct according the SDK.
I have three framework versions on my MCE. v1.0/v1.1/v2.0Beta i have registered with all versions the MSASState.dll and its still not working.
The problem is that the MSASState.dll does not deliver any information about the MCE. Where can i see that the MSASState.dll is running? I can't find it in the Component Services.
|
|
|
|
|
The fact that you have to register a component category (which you can do programmatically when regasm.exe is called on your assembly; read about the ComRegisterFunctionAttribute and ComUnregisterFunctionAttribute classes in the .NET Framework - which run in addition to the normal registration) means that some COM client is going to use ICatInformation::EnumClassesOfCategories (or equivalent) to get COM servers implementing supported interfaces. Your DLL is one such COM server. The MSAS is a COM server that is actually querying for this information about implementations such as your state service. BTW - you should not be installing your files into the system directory. Since your using regasm.exe you can install them anyway (but keep them there), or - better yet (recommended) sign the assembly (more secure anyway) and install it into the GAC using gacutil.exe and then use regasm.exe MSASState.dll (no /codebase, which tells the mscoree.dll shim to look in the GAC).
It's your job as the developer of this sink to communicate your data to whatever client you want. With .NET, your best approach is to use .NET Remoting.
If you haven't already, read Retrieving Media Status[^] in the SDK that explains how the MSAS loads the sinks (your assembly, acting as a COM server indirectly through the mscoree.dll shim).
And creating a Windows Forms application that references this assembly won't do any good - you need to reference the object that MSAS has loaded. This is where .NET Remoting comes in. You need to communicate this data between the AppDomain that was created for your sink and the AppDomain for your Windows Forms application (using statis will not work - each AppDomain is a separate context).
Read the .NET Remoting Overview[^]. There's also several good articles on this site about .NET Remoting, as well as a couple good books like "Microsoft .NET Remoting" from Microsoft Press[^] and "Advanced .NET Remoting" by Ingo Rammer[^].
Another way - though pretty difficult and more intensive (requires lots of marshaling) - is to register your MSAS sink with the ROT (Running Object Table) then using a moniker in your Windows Forms application to get it back out. The problems with this approach are many:- You must marshal data from unmanaged to managed to unmanaged to unmanaged to managed (that seems right) in one direction, and vice versa for the other (to call methods on the running object).
- You must P/Invoke many native functions and re-declare several COM interfaces in managed code (see
ComImportAttribute for starters) manually since several of these won't interop correctly using VS.NET or tlbimp.exe (like methods that can return S_FALSE , which is still a success code) - Your adding complexity to a design that will most likely introduce bugs
- You really end up writing a COM client and server in managed code, not taking advantage of the managed features of the CLR.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|