|
hello every one i am doing a project on jpeg..
please tell me how to free a variable in C#.
please tell me a code which will reinitialize variable value to zero.
or free the memory allocated for a variable just like free() in C.
|
|
|
|
|
.Net (and in your particular case C#) does autmatically memory management. There is a component called Garbage Collector that handles memory related stuff. This garbage collector (GC) releases the memory of an object if it determines that tha object will not be used anymore. In other words you should not worry about memory allocation an freeing.
As an alternative you could use the managed language in an unmanaged manner (which is not encouraged) with the unsafe keyword.
There are a couple of articles on CP with this unmanaged stuff, like Unmanaged Arrays in C# - No Problem[^].
I have no smart signature yet...
|
|
|
|
|
ya i know GC releases memory.
but in case of prog first picture is being compressed with no prob but from 2nd on i am getting high distortion in terms of color.
there are lot of static arrays in prog i think the values from prev pic is causing the porb. is there any effiecient method that will set array value to ZERO w/o using loop.
|
|
|
|
|
System.Array.Clear will work for value types. If you have a reference types (ie. an array of classes) the previous method will set all the elements to null.
Check out this reference[^].
I have no smart signature yet...
|
|
|
|
|
try using .dipose()...good luck...
|
|
|
|
|
It depends on what the variable is:
If it is a value type, then to zero it, assign zero to it:
int i = 0;
If it is a reference type, then is gets more complex. If it implements the IDisposable interface, then it makes some sense to zero it by calling Dispose() - however, this alone will not free memory. However, it is very good practice to use Dispose() (or a using block) where possible to minimize the drain on system resources.
The only way to truly free the memory used by a variable is to remove all references to to it, and allow the garbage collector to do its' thing. This means setting all references to the variable to null, or allowing them to go out of scope.
Unlike C, you have little direct control over memory use in C# - by design, as it is intended that you "can't get the memory leak problems" that C and C++ exhibit. I promise not to laugh at this point...
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
hi,
i am trying to send multiple sms one after one programatically using skype api with c# language.
my code is
--------------
for (int i = 0; i <= arr.Count - 1; i++)
{
SKYPE4COMLib.SkypeClass oSms = new SKYPE4COMLib.SkypeClass();
oSms.SendSms(arr[i].ToString(), strMsg, "");
System.Threading.Thread.Sleep(60000);
oSms = null;
}
so i am trying to send multiple sms in loop one after one but the problem is after sending two sms one error message i am getting and the error is
SMS: CREATE SMS takes a list of phone numbers.
so i just do not understand what is wrong is there in my code.
so please help me to fix this error as a result i can send the same text message to many person one after one from my c# code.
awaiting for ur answer..........
thanks in advance.
tbhattacharjee
|
|
|
|
|
hi,
i want to add auto update feature in my app..which is the best way?
|
|
|
|
|
Deploying it by using ClickOnce allows you to have your application check for updates. That's one way of doing it (though I'm not gonna say for sure that it's the best ).
|
|
|
|
|
I our product needs hardware protection level ... we are planned use dongle protection level . now we was purchase HASP device ..
We was get serial no of storage device USB using WMI ( PNPdevice Id), its unique for all system . But these same device id not gave unique for all system in Aladdin HASP device .... its correct way for secure protect using dongle .... or want use another way for these scenario .... if u have any idea related these please share with us...
Thank you...
|
|
|
|
|
try to get process id and insert it into your program
EASY COME EASY GO
|
|
|
|
|
Follow the HASP API documentation. If I remember correctly, there should be a function to retrieve the dongle HASP ID or some other way to identify that it is indeed your product's dongle.
To secure your product with the dongle, the best strategy is to use the dongle to store or encrypt part of your product's code or a critical data file. Do as little validation on the decrypted code/data as you can in order to prevent an attacker from being able to reconstruct what the code or data should be.
|
|
|
|
|
Hi,
I've created the following code:
AutoResetEvent CheckQ = new AutoResetEvent(false);
public void Q_Insert( QEntry entry )
{
lock( My_Q )
{
My_Q.Enqueue( entry );
CheckQ.Set();
}
}
public QEntry Q_Pop()
{
lock ( My_Q )
{
if( My_Q.Count > 0 )
return (QEntry) My_Q.Dequeue();
}
CheckQ.Reset();
CheckQ.WaitOne();
lock ( My_Q )
{
if( My_Q.Count > 0 )
return (QEntry) My_Q.Dequeue();
}
}
Basically, if there is nothing in the queue, it will just wait until something comes into the queue.
However, my code never wakes up from CheckQ.WaitOne even though CheckQ.Set() was called. I later check the Handle ID in "CheckQ" from both function Q_Insert and Q_Pop, found out that the same variable has different Handle IDs ( as well as WaitHandle ID ) in 2 different functions. That might be the reason that WaitOne never wakes up, but my question is: how did this happen ? I only created one variable CheckQ in the file. I am so puzzled. Really appreciated if someone can explain this. Thanks in advance !
|
|
|
|
|
Assuming you have two or more threads (and probably a multi-core CPU), where one thread is doing one or more Inserts, another thread trying one or more Pops, you may end up in a situation where:
1. inside Pop, the first lock finds no entries
2. then Insert() gets the lock, inserts, and sets CheckQ
3. only now Pop resets CheckQ and waits forever
solution:
either put the CheckQ.Reset() inside the existing lock
or, much better, use an AutoResetEvent rather than a ManualResetEvent
|
|
|
|
|
I a trying to have a hashtable class to populate all the variables from my database to be saved and used in my application as public variables.
this is my public class
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace cure_hospital_management
{
class public_class
{
public Hashtable _sysem_parameters_hash;
public void get_system_parameters()
{
_sysem_parameters_hash = new Hashtable();
_sysem_parameters_hash.Add("Name", "Jassim Rahma");
}
}
}
In my MainFom_Load I am calling:
public_var.get_system_parameters();
public_var._sysem_parameters_hash.Add("Age", "36");
then I am trying to read the hashtable value like this:
MessageBox.Show(public_var._sysem_parameters_hash("Name").ToString());
but getting this error on the reading line (the MessageBox):
System.NullReferenceException was unhandled by user code<br />
Message="Object reference not set to an instance of an object."<br />
Source="cure"<br />
StackTrace:<br />
at cure_hospital_management.frmDoctorDashboard.frmDoctorDashboard_Load(Object sender, EventArgs e) in C:\Users\Jassim\Documents\Visual Studio 2008\Projects\Cure\Cure\doctor_dashboard.cs:line 2050<br />
at System.EventHandler.Invoke(Object sender, EventArgs e)<br />
at System.Windows.Forms.Form.OnLoad(EventArgs e)<br />
at System.Windows.Forms.Form.OnCreateControl()<br />
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)<br />
at System.Windows.Forms.Control.CreateControl()<br />
at System.Windows.Forms.Control.WmShowWindow(Message& m)<br />
at System.Windows.Forms.Control.WndProc(Message& m)<br />
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)<br />
at System.Windows.Forms.ContainerControl.WndProc(Message& m)<br />
at System.Windows.Forms.Form.WmShowWindow(Message& m)<br />
at System.Windows.Forms.Form.WndProc(Message& m)<br />
at DevExpress.XtraEditors.XtraForm.WndProc(Message& msg)<br />
at cure_hospital_management.frmDoctorDashboard.WndProc(Message& m) in C:\Users\Jassim\Documents\Visual Studio 2008\Projects\Cure\Cure\doctor_dashboard.cs:line 84<br />
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)<br />
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)<br />
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)<br />
at DevExpress.Utils.Win.Hook.ControlWndHook.CallWindowProc(IntPtr pPrevProc, IntPtr hWnd, Int32 message, IntPtr wParam, IntPtr lParam)<br />
at DevExpress.Utils.Win.Hook.ControlWndHook.WindowProc(IntPtr hWnd, Int32 message, IntPtr wParam, IntPtr lParam)<br />
at System.Windows.Forms.UnsafeNativeMethods.IntCreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)<br />
at System.Windows.Forms.UnsafeNativeMethods.CreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)<br />
at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)<br />
InnerException:
-- Modified Tuesday, April 20, 2010 7:01 PM
|
|
|
|
|
Done too much VB coding?
MessageBox.Show(public_var._sysem_parameters_hash["Name"].ToString());
FYI: instead of Hashtable, use Dictionary<string, string> and you get rid of all those (string) castings and ToString() conversions.
|
|
|
|
|
jrahma wrote: unresolved hashtable problem
Are't all problems unresolved? once they are resolved, they become references
|
|
|
|
|
|
Ok. I went through the whole thing. So far, someone else hasn't been able to repeat your error. Everytime, binarycoder is answering your specific questions that you ask.
Could you post your complete code that shows error? If it shows error then it can be solved. May be some mistake is happening somewhere.
|
|
|
|
|
|
Hi jrahma,
I cannot compile this code for obvious reasons coz there are so many links in it.
Can't you strip down this code to bare minimum and see just post the problem area? Basically public_class with minimum requirements and two basic forms with calling public_class?
if the problem doesn't happen in that form, it is not a problem related to hashtable.
Som
|
|
|
|
|
as I have mentioned in my MSDN thread, I have no problme compiling the code because no errors dring the build. The error pops up a run time. I als don't have problem when moving the line:
MessageBox.Show(public_var._sysem_parameters_hash("Name").ToString());
to main_form.cs
|
|
|
|
|
What I understood from the complete QA is that you face an error when you access a member of hashtable.
I cannot go through each line of your code to identify where the problem happens till the time i cannot compile the complete code. If you can provide a stripped down version of three cs files which i can compile and find the error during run time, i could help.
You could otherwise try to add a breakpoint just before the line where error happens and look into contents of hashtable. may be you can write a method to do just this.
The following method will show the contents of the hashtable in the output window. When you see the error in the messagebox line, u can see if the content is available in the hashtable or not.
private void PostDataIntoDebug(Hashtable _sysem_parameters_hash)
{
#if DEBUG
foreach (var a in _sysem_parameters_hash.Keys)
{
Debug.WriteLine(string.Format("{0} - {1}", a, _sysem_parameters_hash[a]));
}
#endif
}
|
|
|
|
|
I am getting:
Object reference not set to an instance of an object.
on this line:
foreach (var a in public_var._sysem_parameters_hash.Keys)
from your code...
you might say because hashtable is not initilized or somkething similar.. but why? that's my question...
Thanks...
|
|
|
|
|
jrahma wrote: you might say because hashtable is not initilized or somkething similar.. but why? that's my question...
Well, this is the answer not the question. You need to go through your code step by step and find out where the hashtable was initialized.
I went through the complete discussion once again on MSDN. What i understand is that you are utilizing the same hashtable from different forms but you are initializing them separately.
I suggest making a small change of making the class and variable static and see if this solves your purpose:
static class public_class
{
public static Hashtable _sysem_parameters_hash;
public static void get_system_parameters()
{
_sysem_parameters_hash = new Hashtable();
_sysem_parameters_hash.Add("Name", "Jassim Rahma");
}
}
|
|
|
|