|
I have a large C# application with many threads and queues. There is also heavy use of ArrayList and Hashtable objects that contain many different types of objects themselves.
How can I, in a simple way, prevent all of these objects (including some embedded objects) from being Garbage collected?
PS. Very sorry about the previous messed up message...
|
|
|
|
|
Keep a reference to them in some object that will last the application's lifetime. For example, if you have a main form that will be opened throughout the app's lifetime, store the hashtable as a variable in the form. The hashtable won't get garbage collected.
You may also want to look at the System.GC.KeepAlive method.
p.s. you might want to delete your duplicate message below.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Bought a House!
Judah Himango
|
|
|
|
|
Hi,
I have posted this question a few days ago and have recieved some answers but still can't seem yto get this to work. I want to pass the value of a
NumericUpDown control as a parameter to draw a line on a WinForm. The line will be drawn when the Button is clicked. I think I'm not setting the bool flag that determines if the line should be drawn correctly --
//Why doesn't this work?? -- thanks in advance......
private bool goAheadAndDrawTheLine;
private void Form1_Paint( object sender, System.Windows.Forms.PaintEventArgs e )
{
Graphics g = e.Graphics;
if( goAheadAndDrawTheLine )
DrawMyLine(g);
}
public void DrawMyLine( Graphics g )
{
{
g.DrawLine( somePen, 0, 0, 100, this.numericUpDown1.Value );
}
}
private void DrawA_Click( object sender, System.EventArgs e )
{
goAheadAndDrawTheLine = true;
}
|
|
|
|
|
Add a this.Invalidate() to the button click event. This forces the form to call OnPaint.
Also you should override the OnPaint method so that you can add your code to do the drawing when ever the form is redrawn.
|
|
|
|
|
If you click the DrawA button, it will then set the goAheadAndDrawTheLine to true. However, your form won't get painted magically; either the form has to be redrawn (for example, resizing the form will force a redraw) or your code has to tell the form to be redrawn. When the form is redrawn, the Form1_Paint event will occur, and the line we be drawn.
To force your form to be redrawn from code, call yourForm.Invalidate().
So to make your code work:
private void DrawA_Click( object sender, System.EventArgs e )
{
goAheadAndDrawTheLine = true;
this.Invalidate();
}
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Bought a House!
Judah Himango
|
|
|
|
|
Bingo. Works great now. Thanks for the help.
|
|
|
|
|
Recently I have created a control for my project. This control is basiclly a panel grouping 2 labels and a button.
I added this control to my main form. I need to add a click even to it so that it will bring up a message box.
When i dbl click on the control in the design view, it creates me a load event. I need a click event, so i manually added a click event in the properties.
I run the program and click on the panelgroup, it has no response. It seems like that event is not there, or doing nothing. I tried printing out a message and it doesnt
Can any one help? Is it because i need to create a click event in side the control??
-- modified at 12:16 Monday 28th November, 2005
|
|
|
|
|
Keep in mind the click event will only be raised if you click the control itself, not the labels or buttons on the control.
If you already know that, and it's still not working when you click the control (but not the labels or button), then we'd need to see some code in order to help you.
Also, to be clear, please tell us exactly what you're trying to do. Do you want your PanelGroup control to raise a Click event when any of the labels or buttons are clicked? Or do you want to create a custom event?
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Bought a House!
Judah Himango
|
|
|
|
|
Assuming that you want the instantiating class to receive a click event when the user clicks on the button in the user control, then you need to create an event/delegate pair in your user control - like this...
public delegate void ButtonClickedDelegate();
public event ButtonClickedDelegate OnButtonClicked;
Next, you need to create, within your user control, an event handler for the click event for your button using the IDE and include something like this for your event handler...
void myButton_Click (object sender, System.EventArgs e)
{
if (OnButtonClicked != null)
OnButtonClicked();
}
When you add your user control to a new project, then this public event you have declared should show up in the event list for the user control in the IDE. You can then specify an event handler for this custom event and receive the event within the instantiating class.
Darryl Borden
Principal IT Analyst
dborden@eprod.com
|
|
|
|
|
I get the "setup error-> failed to load resoures from resource file please check your setup." whay ??/
|
|
|
|
|
|
I'm still interfacing to an unmanaged C++ camera library with this code
typedef struct<br />
{<br />
void* pBuffer;<br />
unsigned long bufferSize;<br />
} CamFrame;
In C++ all is have to do for varying bufferSize is
myCamFrame.pBuffer = new char[myCamFrame.bufferSize];
then later call
GetCameraFrame(camHandle,&myCamFrame);
In C# I have
[StructLayoutAttribute(LayoutKind.Explicit)]<br />
public struct CamFrame<br />
{<br />
[FieldOffsetAttribute(0)]<br />
private IntPtr pBuffer;<br />
[FieldOffsetAttribute(4)]<br />
private uint bufferSize;<br />
}<br />
and <br />
GetCameraFrame(IntPtr camHandle, ref MyCamFrame);<br />
<br />
What I can't work out is how to allocate the necessary buffer (e.g using byte[] ) and then get the void pointer. Some things I've tried compile but the call to GetCameraFrame goes off into la-la land somewhere. I suspect this related to marshalling.
Ideas anyone?
Stewart DIBBS
|
|
|
|
|
|
Hi,
From reading something months ago, I've been under the impression that saying button1.Focus() is enough to focus on the button and highlight it (the dotted box). If this is true, I don't know what's wrong but it doesn't work. In certain circumstances, when a button is definitely focused on, you wouldn't be able to tell so from looking at the screen (the dotted box isn't there so it looks like nothing's in focus). I've been coming up with interesting ways around this, but the current situation is proving challenging, and I can't imagine there isn't some simple way to force the dotted line to appear. So, is there any manual way to force a button highlight?
Thanks so much!!!!!
Mel
|
|
|
|
|
I've got a form that is using an old COM object (AxSHDocVw.AxWebBrowser). Does anyone know how once I navigate to a screen I can automate logins using this control?
|
|
|
|
|
i write this cide to get data from data set but there is an error in line 10 and 11
why
if u plze help me and tell me how can i get all the data from data lis using for loop.
thanks in advance
1.OleDbConnection myConnection=new OleDbConnection("provider=microsoft.jet.oledb.4.0;datasource=alabrags.md");
2. DataSet ds1 =new DataSet();
3. OleDbCommand cmdsel1;
4. String sql1="select * from customers";
5. mdsel1=new OleDbCommand(sql1,myConnection);
6. OleDbDataAdapter myCommand1 ;
7. myCommand1=new OleDbDataAdapter();
8. myCommand1.SelectCommand=cmdsel1;
9. int total ;
10. total=ds1.Tables["customers"].Rows[0][1];
11. t1.Text=ds1.Tables["customers"].Rows[0[1].ToString();
MffM
maayah2000@yahoo.com
|
|
|
|
|
No place are you actually filling the dataset with data from your command. You are also setting up the select command, but are never executing it. There is a Fill command on the OleDbDataAdapter class that will fill in your DataSet. Call that between 8 and 9 and you should be good to go.
Steve Maier, MCSD MCAD
|
|
|
|
|
I have no previous programming knowledge and I have been learning C# for only 3 days, so bare with me if it is an extremely easy question.
As the title states,
age = (byte)(generator.NextDouble() * 100 - 20);
sometimes gives extremely high values, close to 255. I think its because when the program substracs 20 from the value it goes beyond 0 and starts discounting from 255.
Is there any way to stop it at 0 besides using an int?
I solved it by adding 25 if age is over 200, but there must be a better way to do it.
Here is the code, if you need it.
private static void doPersonnel()<br />
{<br />
Random generator = new Random();<br />
StreamWriter sw;<br />
int number = 0;<br />
byte dex, sma, wor, age, potAbility, ability;<br />
String line;<br />
while (number < 10000)<br />
{<br />
dex = (byte)(generator.NextDouble() * 255);<br />
sma = (byte)(generator.NextDouble() * 255);<br />
wor = (byte)(generator.NextDouble() * 255);<br />
age = (byte)(generator.NextDouble() * 100 - 20);<br />
if (age < 10 || age > 200)<br />
{<br />
age += 25;<br />
}<br />
do<br />
{<br />
potAbility = (byte)(generator.NextDouble() * 200);<br />
} while (potAbility < 1);<br />
do<br />
{<br />
ability = (byte)(generator.NextDouble() * 200);<br />
} while (ability > potAbility);<br />
line = Convert.ToString(dex + "," + sma + "," + wor + "," + age + "," + ability + "," + potAbility);<br />
sw = File.AppendText("D:\\Test.txt");<br />
sw.WriteLine("{0}", line);<br />
sw.Close();<br />
number++;<br />
}<br />
}
If I made any abvious mistakes tell me too please, its my first program after all
|
|
|
|
|
Yes, your thought is correct. You are picking a random number between -20 and 79, when you cast that to a byte, the values -20 to -1 becomes 236 to 255.
Why are you picking a number between -20 and 79 anyway? That seems like strange values for something called "age"... Are you attempting to pick a number between 20 and 99?
Anyhow, take a look at the Next() method of the Random class. It returns an integer value that is greater or equal to 0 and less than the specified maximum. This code gives the same result as your code, and creates a value between 0 and 254:
dex = generator.Next(255);
If you want a random number where the lower range is other than zero, you can get a random number where the lower range is zero, and just add a constant value. This creates a value between 20 and 99:
age = generator.Next(80) + 20;
But then again, there already exists a method for this:
age = generator.Next(20, 100);
---
I see that you are declaring byte variables. Unless you have some special plan that requires byte size variables, stick to the int data type. The processor internally only uses two data types; int and double. If you use these, your code will be faster.
---
b { font-weight: normal; }
|
|
|
|
|
Thanks, the book Im reading only tells about the NextDouble method.
And thanks for the additional input, as I said Its only my third day.
I wanted to get ages between 5 and 80 BTW
|
|
|
|
|
It is hard to tell what you are trying to accomplish. I will take a guess - you are trying to create a random positive integer.
Take a look at the Random.Next(int maxValue) method.
For a human age you might use code like:
int age = generator.Next(100) + 1;
This would give you an age between 1 and 100. Casting the result of a double to a byte may not give you the randomness you desire.
|
|
|
|
|
You can also use this.
int age = generator.Next(5,80);
And it will generate one between 5 and 80 with one simple step.
Steve Maier, MCSD MCAD
|
|
|
|
|
hi
i have program on c# with ms sql.when i start the program on my comp is work excelent but when i install it on some other i get this message(install=copy program and paste it on other computer)how to deside the problem??
"APPLICATION HAS GENERETE AN EXCEPTION THAT CLOUD NOT BE HADNLED PROCES ID=0X75(1884),THREAD ID=0X6F8(1784)
Thank you
|
|
|
|
|
papa1980 wrote: "APPLICATION HAS GENERETE AN EXCEPTION THAT CLOUD NOT BE HADNLED PROCES ID=0X75(1884),THREAD ID=0X6F8(1784)
As your error says, an exception was generated, and probably you do not have a try/catch block in witch to handle it. Your exception may have occured because you rely on something that is on your computer and on others may not be...
Put a try/catch block in your main procedure and display the exception's message to find out why your application crashes on other sistems.
protected internal static readonly ... and I wish the list could continue ...
-- modified at 15:11 Monday 28th November, 2005
|
|
|
|
|
I have it
static void Main()
{
try
{
Application.Run(new MainForm());
}
catch(Exception exp)
{
MessageBox.Show(exp.ToString());
}
}
this problem is occure not when i compilate the program on my computer is ocrure when i copy the program on other computer and try to use it????
|
|
|
|