|
N a v a n e e t h wrote: So int[] numbers would take 4KB of memory and will be kept on managed heap.
No, it wouldn't. It's just a reference, so it will only take up four bytes (on a 32-bit system) wherever it's declared. If it's a local variable, it will take up four bytes of stack space.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Ohh it's 4bytes, not KB. Thanks Guffa for correcting. You rocks /
|
|
|
|
|
gigahertz205 wrote: How much memory do uninitialized arrays take up? EX: int[] numbers;
It only takes up the memory used by the reference that you have declared. You haven't created the actual array yet.
gigahertz205 wrote: Do uninitialized value types take space too?
They take up the same amount of memory regardless if they have a defined value or not.
A value type that is a class member is always initialised. It's only when you have a value type as a local variable in a method that it can be undefined. It still has a value, but that value is undefined, so the compiler protects you from using the value before you have assigned anything to the variable.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
i have developed an application which records the sound in c# i have this problem that
i have to enable the opetion of wave out mix in sound control panel using c# then the sound recording works other case no recording happens just blank file can you give me simple code example which just enable the wave out mixer option or stero mixer option in widows xp
just want to enable the option of setro mixer or wave out mixer using c#.THANKS in ADVANCE
its me yami
now cheers
|
|
|
|
|
I was gonna post this on Monday when back at work with the code in front of me but it's bugging me so I thought I'd post now! (will post code on Monday if needed).
For storing some semi-sensitive data I have a string.Encrypt extension method that uses Rijndael encryption. This encrypted string is then used as a key in a dictionary, which is serialized using Binary serialization.
All this works fine and the data is encrypted/decrypted perfectly but when doing something such as
Value myValue = myDictionary[key.Encrypt()]; the key is never found, but if I do a foreach and do a decrypt on the Key from the KeyValuePair, and compare with key (no encryption) it matches!?!
Any ideas cause it's got me beat!
Dave
modified on Friday, July 4, 2008 4:14 PM
|
|
|
|
|
What does the Encrypt method return? A byte array?
The default equality comparer for a byte array doesn't compare the contents of the arrays, it only compares the references of the array object. If you want a dictionary with a byte array as key, you have to create an equality comparer that the dictionary can use.
I wrote an article about how to implement an equality comparer for a dictionary:
Dictionary with a custom key[^]
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
It returns a string so it shouldn't be anything to do with references.
As I said the codes at work but I've coded up a quick example that replicates the issue here. As you will see, the encrypted keys are different (why?) but it decrypts correctly. (This example has ruled out serializazing/deserializing as the issue)
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Crypt
{
class Program
{
public static string password;
static void Main(string[] args)
{
Console.Write("Please enter your password: ");
password = Console.ReadLine();
string plainKey = "Test key";
string value = "Test value";
MyDictionary myDictionary = new MyDictionary();
Console.WriteLine("Adding key: " + plainKey);
myDictionary.AddItem(plainKey, value);
Console.WriteLine("Looking for " + plainKey.Encrypt(password));
Console.WriteLine("Found :" + myDictionary.ContainsKey(plainKey.Encrypt(password)));
Console.WriteLine("Dictionary contents:");
foreach (KeyValuePair<string, string> entry in myDictionary)
{
Console.WriteLine("Encrypted: " + entry.Key);
Console.WriteLine("Decrypted: " + entry.Key.Decrypt(password));
}
Console.ReadKey();
}
}
class MyDictionary : SortedDictionary<string, string>
{
public void AddItem(string plainKey, string value)
{ this.Add(plainKey.Encrypt(Program.password), value); }
}
public static class Extensions
{
public static string Encrypt(this string str, string password)
{
RijndaelManaged cipher = new RijndaelManaged();
byte[] plainText = System.Text.Encoding.Unicode.GetBytes(str);
byte[] salt = Encoding.ASCII.GetBytes(password.Length.ToString());
cipher.Padding = PaddingMode.ISO10126;
PasswordDeriveBytes key = new PasswordDeriveBytes(password, salt);
ICryptoTransform encryptor = cipher.CreateEncryptor(key.GetBytes(32), key.GetBytes(16));
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
cryptoStream.Write(plainText, 0, plainText.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
return Convert.ToBase64String(cipherBytes);
}
public static string Decrypt(this string str, string password)
{
try
{
RijndaelManaged cipher = new RijndaelManaged();
byte[] encryptedText = Convert.FromBase64String(str);
byte[] salt = Encoding.ASCII.GetBytes(password.Length.ToString());
cipher.Padding = PaddingMode.ISO10126;
PasswordDeriveBytes key = new PasswordDeriveBytes(password, salt);
ICryptoTransform decryptor = cipher.CreateDecryptor(key.GetBytes(32), key.GetBytes(16));
MemoryStream memoryStream = new MemoryStream(encryptedText);
CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
byte[] plainText = new byte[encryptedText.Length];
int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
memoryStream.Close();
cryptoStream.Close();
return Encoding.Unicode.GetString(plainText, 0, decryptedCount);
}
catch
{ return string.Empty; }
}
}
}
Dave
|
|
|
|
|
Bugger! Just found the issue - it's the padding! (Just commented out the cipher.Padding lines)
Why is it that the minute you post here the solution jumps out at you?
Will no padding cause issues?
Edit: Actually it's the ISO10126 padding mode that's causing the problem.
Dave
|
|
|
|
|
I wrote a library where there is a method which when called launches a thread (using backgroundworker) and returns an identifier. The thread started launches an event when done.
I have a Winforms client and everything works fine. When the method is called, it returns immediately and my Winforms app happily waits for the event.
Now, I tried to write a web service, which is in turn client of the said library. When I contact the web service http://localhost/blabla/blabla.asmx with IE and select teh web method that calls the library method which in turn starts the worker thread, the page blocks until the background thread is finished.
This behavior is observed whether the web service runs inside Visual Studio or IIS.
ASP.NET 2.0 on Windows Server 2003 R2, Standard Edition, Service Pack 2.
Is there something special to do to be able to do multithreading inside a web service?
Thank you
|
|
|
|
|
I don't use ASP, some of the othe guys here may be able to help more but a quick google search[^] for 'multithreading inside a web service' turned up a plethora of promising results.
Dave
|
|
|
|
|
When I clicked on you link, the 3rd item is your reply.
|
|
|
|
|
Le Centriste wrote: When I contact the web service http://localhost/blabla/blabla.asmx with IE and select teh web method that calls the library method which in turn starts the worker thread, the page blocks until the background thread is finished.
Strange. Do you have any waithandles waiting for signal ? Can you post some sample code ?
AFAIK, each web method in a web service offer an asynchronous fashioned method. If you have a web method say "Foo", you will also have "FooAsync" which returns immediately and does the job on another thread. Please try that.
|
|
|
|
|
Le Centriste wrote: When I contact the web service http://localhost/blabla/blabla.asmx with IE and select teh web method that calls the library method which in turn starts the worker thread, the page blocks until the background thread is finished.
That sounds right. Or are you expecting the page to return, then when the worker is finished, a magical AJAX call will notify you?
|
|
|
|
|
I provide another method that you can call to know the progress of the task started.
|
|
|
|
|
I have seen some codes as are shown bleow, although they works fine but i am asked to use equal() function with strings. What's the difference then.
string str="aa";
if(str=="aa")
{
}
|
|
|
|
|
I think Equals function compares the reference not the value itself.
|
|
|
|
|
No.
From Object Browser...
Determines whether this instance of System.String and a specified object, which must also be a System.String object, have the same value.
Dave
|
|
|
|
|
If you use Equals() you can specify the culture (your own, a preset or none at all) used for the string comparison.
You can also compare with an object. Consider this example:
ListBox listBox = new ListBox();
listBox.Items.Add("a");
string x = "a";
Console.WriteLine(x.Equals(listBox.Items[0]));
Dave
|
|
|
|
|
netJP12L wrote: string str="aa";
if(str=="aa")
{
}
This is equal to
string str="aa";
if(str.Equals("aa")){
} When you use str == "aa" , string class calls the Equals() method to do the comparison. Only advantage I can see if you use Equals explicitly is supplying the StringComparison enum.
I prefer to use string.Compare to compare strings.
|
|
|
|
|
All,
I am attempting to write a new Remote Registry key Value string, but I keep receiving a "Cannot write to the registry key" exception. I have full access to the remote key and have set that access (as an extra measure) inside my program. Below is the code I an attempting to use. I can read the value fine, but cannot edit it.
Thoughts?
bool SetRegKey(String hostname, String strPrimaryUserGroup)<br />
{<br />
String strBaseKey = "Software\\Agent";<br />
RegistryKey environmentKey = RegistryKey.OpenRemoteBaseKey(<br />
RegistryHive.LocalMachine, hostname).OpenSubKey(<br />
strBaseKey);<br />
String PUGValue = environmentKey.GetValue("PrimaryUserGroups").ToString();<br />
if (PUGValue != strPrimaryUserGroup)<br />
{<br />
environmentKey.SetValue("PrimaryUserGroups", strPrimaryUserGroup);<br />
}<br />
return false;<br />
}
|
|
|
|
|
Hi, I've been trying to get a list of module names of local processes on my box but I got access denied in the attempt. This issue happen with every high privileged process ie: smss, svchost, winlogon, ... I've tried google it but without success any hint on that ? Btw I'm using VS 2005 on Vista.
sh*t
|
|
|
|
|
Hello,
VS 2008
I have typed dataset and have added a single data table named dsMissedCalls.xsd and dtMissedCalls.
I would like to save some missed calls for my application. The table is very small not more than 10 rows.
Example. dtMissedCalls(ID, Caller, DateAndTime)
I have created a XML file, and I would like the xml file to have the schema of the typed dataset (dsMissedCalls.xsd).
Before when I have been creating xml file. I would add a new xml file. Then click the "Create Schema" button. That would creaet the schema for the xml file. However, as I have created a typed dataset that already has a schema. The question is. How do I get my xml file to relate to that schema?
So basically I have created a typed dataSet and would like to save added rows to the xml file.
The code below works, but I want to xml to use the dataset schema.
Many thanks for help with this confusing question,
<br />
DataRow row;<br />
row = ds.Tables[0].NewRow();<br />
row["Caller"] = callersName;<br />
row["DateTime"] = DateTime.Now.ToShortDateString();<br />
<br />
ds.Tables[0].Rows.Add(row);<br />
ds.WriteXml(missedCallsXML, XmlWriteMode.DiffGram);<br />
|
|
|
|
|
Hi,
I have used the picture box to draw an image on the screen. Its loading perfectly, now I want to draw over some parts of the image, with rectangles and lines.
I tried to draw on the picture box by doing the following but nothing shows up?
How can I go about doing this.
Graphics g = pictureBox1.CreateGraphics();
SolidBrush solidBrush = new SolidBrush(Color.Blue);
g.FillRectangle(solidBrush, 0, 0, 100, 100);
Any help would be greatly appreciated
|
|
|
|
|
You cannot simply draw a line or rectangle on top the image. They are drawn behind the image. You need to make them a control and set the foreground or background property.
|
|
|
|
|
"Graphics g = pictureBox1.CreateGraphics();"
That is a bad idea unless you're drawing something you want to erase, like a rubber band. Painting should take place in the paint event. And, at this point, you should just draw your bitmap in the paint event, then draw on it.
Christian Graus
Please read this if you don't understand the answer I've given you. If you're still stuck, ask me for more information.
|
|
|
|