|
I have a class1 and in one of its methods I created a local instance of class2 passing to its ctor a member var of class1.
Within class2 I modified the parameter passed to its ctor. To my surprise when I returned to class1 its member var was also modified? C++ wouldve handled this as a local var within class2. What's going on here?
Tia.
I am a SysAdmin, I battle my own daemons.
|
|
|
|
|
you are passing a reference to the object by value, not the object itself by value. This has often been a source of confusion as it appears at first that value types and reference types behave differently. If you used a struct instead i suspect you would see the behaviour you were expecting.
It's one of those things that is very hard to explain so i'm not going to try right now as I'll probably just confuse the issue. There are a couple of articles around the place if you google for something like 'c# reference pass type by value' or similar you should find a more eloquent description of what's happening than i could provide.
HTH
Russell
|
|
|
|
|
C# passes objects by reference, but the references themselves are passed by value.
Newbies find this very confusing. I know, 'cause I did.
Cheers,
Vikram.
The cold will freeze our stares
We won't care... Join the CP group at NationStates. Password: byalmightybob
|
|
|
|
|
hi all,
i have a program that make a lot of graphical drawing so it consumes a very big space of memory (i think memory leak)
Is any one know how to find where is the leak comes from
(i means where is the unreachable code)
thanx
Generator
|
|
|
|
|
I always make sure to dispose of Graphics objects as they have a bad habit of causing leaks if not thrown away after use.
liberal sprinklings of the using keyword don't hurt either.
Russ
|
|
|
|
|
hi ,
i draw alot of rectangles(one at each invalidate at a moment )
i draw in OnPaint but in it i cannot make dispose (what can i do)
also where i can use GC.Collect
Generator
|
|
|
|
|
Hi,
if you create some objects (a Pen, Brush or any other instance of a class that has
a public Dispose() method) inside your OnPaint method, you MUST call Dispose for them.
Additionally, if you need the same object over and over (such as new Pen(myColor)) then
I suggest you create it only once and keep it in a class member (rather than continuously
creating and disposing and collecting them).
And finally, if the objects you need correspond to system objects (such as the ones
available in SystemPens, SystemBrushes,...) I suggest you use these (without new,
without Dispose !).
And I do not recommend you call GC.Collect at all (the gc works fine all by itself),
and certainly not from inside any OnPaint method ! (GC.Collect is expensive and
should not be called by the UI thread, it might ruin the responsiveness).
|
|
|
|
|
Hi,
from debug of my program i noticed that Update();
is making bad effect and may be the reason of memory leak
so can any one tell me what is the disadvantages of it
thanx
Generator
|
|
|
|
|
Hi,
if you mean Control.Update() I dont know why you think you need it. In all the programs
I have ever developed I never felt the need for it: correct program design (including
the use of background threads for longwinding tasks) results in good responsiveness
without problems.
On the other hand, Update does NOT cause memory leaks; only errors in your code
( or in .NET itself, rather unlikely) can do that, e.g. not calling Dispose().
If you want us to be able to help you, you should explain more about your app,
and publish the pieces of code you suspect yourself, rather than using
obscure observations (a very big space of memory, unreachable code, bad effect).
|
|
|
|
|
hi,
ok you are right i am making a packet that moved on a line
when i click a button and stopped when i click stop
so i use thread but the stop button be not responsive and the app hang up
so one here advice me to use BackgroundWorker so i use it but i have a very large consuming of memory when i click the first button i donot no why so i try to solve it hardly
so last i remove update then the memory consuming decreased but not every time i start my application run i work well
some times it work bad so i tried also
so to ask a question if i called a method for a thread or backgroundworker and in this method other method is called
the second method would be run by the program process or by this thread
if by program then i will correct my code
else
i will publish the pieces of code to see(i miss understanding)
thanx alot
Generator
|
|
|
|
|
Hi,
I did not understand very much of what you wrote.
It seems like you want some animation to start when a "Start" button is pressed,
and stopped when a "Stop" button is pressed, and furthermore the "Stop" button does
not (always) work reliably.
There can be several causes for this:
- you might be doing too much stuff in the UI thread
- CPU load might be high at or above the priority of the UI thread
- some logical error in the communication bewteen the "Stop" button handler,
and the thread that does the animation.
Maybe this info will help:
methods are executed on the same thread as the code that calls them (unless you
do very special things, such as calling Invoke). Some asynchronous stuff is executed
on another method automatically (e.g. a tick handler for all timers, except a
Windows.Forms.Timer).
if you try to modify the user interface (i.e. add/remove/modify a Control) from
a thread other than the UI thread (the one you have at start) then:
- under .NET 1.1 your program may freeze, or behave unpredictably
- under .NET 2.0/3.0 you will get some "CrossThread" Exception (you could set some
flag false to skip the checks, but then the app may freeze again).
You may want to create your own background thread, so you can fully control it
(set priority, kill/abort it, etc).
You may want to find and read an MSDN article entitled
"How to: Use a Background Thread to Search for Files "
which probably explains more on InvokeRequired, Invoke, threads, etc.
|
|
|
|
|
private void btn_simulate_Click(object sender, EventArgs e)<br />
{<br />
this.btn_simulate.Enabled = false;<br />
this.btn_stop.Enabled = true;<br />
closeThreadRequested = false;<br />
if (!packetsMovingThread.IsBusy)<br />
packetsMovingThread.RunWorkerAsync();<br />
<br />
}<br />
private void btn_stop_Click(object sender, EventArgs e)<br />
{<br />
closeThreadRequested = true;<br />
this.btn_stop.Enabled = false;<br />
this.btn_simulate.Enabled = true;<br />
if (packetsMovingThread.IsBusy)<br />
packetsMovingThread.CancelAsync();<br />
}<br />
private void packetsMovingThread_DoWork(object sender, DoWorkEventArgs e)<br />
{<br />
BackgroundWorker worker = sender as BackgroundWorker;<br />
if (worker.CancellationPending)<br />
e.Cancel = true;<br />
else<br />
dataPacket.simulate(lineArray, closeThreadRequested, ref source_pnt,<br />
ref destination_pnt, packetsMovingThread);<br />
}<br />
private void packetsMovingThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)<br />
{<br />
if (e.Error != null)<br />
MessageBox.Show(e.Error.Message);<br />
else if (e.Cancelled)<br />
MessageBox.Show("Cancelled");<br />
}<br />
private void packetsMovingThread_ProgressChanged(object sender, ProgressChangedEventArgs e)<br />
{<br />
if (closeThreadRequested == false && !(source_pnt.IsEmpty))<br />
{<br />
dataPacket.drawPacket(this.CreateGraphics(), source_pnt);<br />
}<br />
Invalidate(drawingFrame);<br />
}
public void drawPacket(Graphics grfx, Point location)<br />
{<br />
Pen pen = new Pen(type);<br />
SolidBrush solidBrush = new SolidBrush(type);<br />
grfx.DrawRectangle(pen, location.X, location.Y, 4, 4);<br />
grfx.FillRectangle(solidBrush, location.X, location.Y, 4, 4);<br />
pen.Dispose();<br />
solidBrush.Dispose();<br />
}
movePacket method compute next point and call
packetMovingThread(declared object).ReportProgress(100);
Generator
|
|
|
|
|
Hi,
your code contains this.CreateGraphics() which CREATES a Graphics, and hence
requires a matching Dispose() which is not present. I expect this is your main problem.
I dont see how exactly your backgroundworker is working, how iterations are achieved,
and how the drawing is handled by the UI thread; so there might be additional
causes for problems.
ADDED: drawing a packet should be part of your paint handler (so it gets called
whenever anything needs to be repainted, i.e. also when something that was temporarily
hiding your form gets removed, or when your app gets restored from being minimized);
your animation should just modify the parameters that control the drawing
(such as coordinates) and then make sure a repaint gets ordered (typiucally by
calling Invalidate).
-- modified at 17:48 Sunday 8th April, 2007
|
|
|
|
|
hi Luc Pattyn,
actually i wanna to thank u for ur efforts to help me.
first i think the problem is that i donot understand
BackgroundWorker well (it is very complicated)
so i tried before using it to use normal thread but i failed
but now i knew my error and i fix it in the normal thread not the BackgroundWorker Thread and now no(unreachable code,memory leak ...)these things are not found now
any way thanx to u very much
bye
Generator
|
|
|
|
|
|
Is there a way to access each of the controls in a form by name programmatically?
i.e. without having to loop through each one
Tia.
I am a SysAdmin, I battle my own daemons.
|
|
|
|
|
Yes,
use the Controls Property of a Contailer like Form or Panel:
<br />
Control c = Controls.Find("name", true);
Control c = panelXYZ.Controls.Find("name", true);
|
|
|
|
|
If I have a class with an event, and a class that wants to attach to the handler:
class A
{
public event EventHandler<SomeEventArgs> SomeEvent;
}
class B
{
public void AttachToHandler(A a)
{
a.SomeEvent += ***;
}
void SomeEventHandler(object sender, SomeEventArgs e) {... }
}
The VC# Express IDE ("press TAB to insert..") suggests:
a.SomeEvent += new EventHandler<SomeEventArgs>(SomeEventHandler);
However, the following also seems to work:
a.SomeEvent += SomeEventHandler;
Now the questions:
Is there a difference between the two? A "preferred style"?
|
|
|
|
|
I've always used the first method (using the New operator to instantiate a new delegate that will handle the event).
You can alway declare a variable that holds the reference to the event handler in advance and then use that as your handler like so:
<br />
<pre>
EventHandler eh;
eh = New EventHandler(SomeEventHandler);
a.SomeEvent += eh;
</pre><br />
<br />
this way you could later detach the event handler too, like so:<br />
<br />
<code lang="C#"><br />
<pre>
a.SomeEvent -= eh;
</pre><br />
<br />
The second way seems to be a shortcut for the first (in which the compiler automatically generates the code that instantiated the delegate and than passes it your function).<br />
<br />
Hope this helps, but your best bet is to do some reading on delegates and event handlers in C#. <br />
<br />
<br />
<br />
<div class="ForumSig">----<br />
<a href="www.digitalGetto.com">www.digitalGetto.com</a></div>
|
|
|
|
|
Hi,
I would like to have a ComboBox with multi-select, which is similar to ListBox (but ListBox has no DropDown). Could someone here give me some help? Thanks in advance.
BTW, I use Microsoft Visual Studio .Net 2005.
Regards,
Amatista
|
|
|
|
|
Why would you want to do this? A ComboBox can only show a single selection at a time, when the list is not dropped, so therefore, a MultiSelect ComboBox makes no sense; which is why it's not an option ...
|
|
|
|
|
Thanks for your reply.
I am in charge of designing a full-text searching tool, a windows application, and the ComboBox is used for user to select file types for searching. I think it would be better if the user can select more than one file types. For the selected file types, if more than one item, they should be displayed, either comma or semicolon separated. Or is there another tool instead of ComboBox that I can use?
Does anyone have any suggestions to solve this issue?
|
|
|
|
|
You should either be using a group of check box controls or a CheckedListBox. What you're wanting to do, goes against the standard design for that control.
|
|
|
|
|
Thanks for the information.
|
|
|
|
|
I have a program which has a bunch of user controls. (Talking in the hundreds here) You can basically consider these controls as a bunch of buttons, with 2 images taken from the hard drive as Up and Pressed states. (The images are stored in memory when the control is first created, so there isn't any delay getting them from the hard drive.)
My problem is that when the program is minimized and then maximized, all the user controls redraw themselves. Is there any way to stop this?
Now, bearing in mind that RAM is not an issue, (well as long as it doesn't use more than 300MB or so of it anyway :P ) so if the only way to fix this is to use lots of memory, I don't mind.
Any suggestions?
PS. I'm assuming that the Windows GUI is discarding the conrol graphics as soon as they're off screen to improve performance. If this is true, then a way to stop this would be appreciated. (Also making it all get painted at the same time on startup would be appreciated. A slight delay while loading up the program would not matter)
Note: Using UserPaint, AllPaintingInWmPaint and DoubleBuffer doesnt help.
- Munty
|
|
|
|
|