|
Thanks for your help,
there is something even more strange : before opening the StreamWriter that throws the exception, if I try to delete the file, there's no error. It can be deleted, but can't be opened for writing.
|
|
|
|
|
Member 4052498 wrote: It can be deleted, but can't be opened for writing.
That doesn't make sense to me.
If that's correct, I would say something is very wrong.
I suggest you delete the file, reboot the system, rebuild the app, and try again.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
I'm a newbie in computer programming so please have this in mind.
I have a class called Pinger which does basic pinging of hosts on a network. I'm currently implementing a network scanner so I create an array (254) of Pinger's. The problem occurs when I try to dispose the array after I've done the scan. I dispose every Pinger in the array - the Pinger class contains Dispose methods (GC.SuppressFinalize).
Still, the array of Pinger's still uses a lot of rescources on my system and I can clearly see the memory beeing freed when I close my application.
Does anyone have any idea on how to correctly get rid of the rescources being used (after I'm finished using them)?
I have the complete project, but couldn't find a way to upload it so if anyone want's to see it let me know how I can send you the project.....
private void AllPingsFinished()
{
counterForInsert = 0;
foreach (Pinger str in _pinger)
{
if (str != null)
{
str.Dispose();
}
}
}pre>
|
|
|
|
|
I don't think that you can remove object from Collection while it's looping
In Other word
<br />
<br />
foreach ( string str in MyList)<br />
{<br />
MyList.remove(str);<br />
}
List<string> ListForDetele = new List<string>();<br />
foreach (string str in MyList)<br />
{<br />
ListForDelete.Add(str);<br />
}<br />
<br />
foreach(string str in ListForDelete)<br />
{<br />
MyList.Remove(str);<br />
}
Have a good day ,
I know nothing , I know nothing ...
|
|
|
|
|
I will try this!
Thanks!!
|
|
|
|
|
Hi,
it is wise to call Dispose() on an object you no longer need, so it can release its unmanaged resources in a deterministic way. However that does not take care of the managed resources, i.e. the object is still alive (as your array is alive and holds a reference to it).
e.g. if each Pinger holds a thread, then that thread (and its stack memory) are still allocated (unless you cleaned them too in Dispose).
so it would also be good to remove the reference from the array as soon as you don't need them.
Kaare Tragethon wrote: I can clearly see
do you? how? Task Manager is not the right place to get accurate information on this subject. It shows the
working set, not how much memory the app NEEDS.
The differences are:
- the app may need to hold that amount of data;
- or the app may have asked that amount of memory because that was the fastest way to get things done, but in reality lots of memory are unused INSIDE the app (instead of inside the Window memory pool).
Test: minimize your main form. Does that dratically reduce the "memory requirement"? if yes, point proven.
More comments:
- why do you need 254 Pingers all at once? do they really all work in parallel? do you have that many threads?
AFAIK TCP/IP will choke. I tend to use say 8 threads and let each of them ping the addresses one after another (one job list, each thread picks the next job in turn).
- I tend to use a plural noun for a collection, so I would write foreach(Pinger pinger in pingers)...
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Thanks a LOT to both of you!
I will definetively try a combination of both your answers (max 8 pingers, and add them to a list)!
Best regards
|
|
|
|
|
Hi , have a good day
I have the Class
class MyClass
{
private int _myvar;
public int MyVar
{
get
{
return _myvar;
}
set
{
_myvar = value;
}
}
public void ChangeMyVar(int NewValue)
{
_myvar = NewValue;
MyVar = NewValue;
}
}
Which method is the standard One ?
Method 1 , or Method 2 ?
thank you
I know nothing , I know nothing ...
|
|
|
|
|
Why would you need ChangeMyVar(int) if you have your public int MyVar property? That would be used to interface with the private _myvar, you woulnd't need the method ChangeMyVar(int).
|
|
|
|
|
Thank you for your replay ...
I made a simple DAL class , and it's used to Change the myvar variable inside it's Class
I know nothing , I know nothing ...
|
|
|
|
|
Stark DaFixzer wrote: Which method is the standard One ?
It depends. Like most things in programming.
Suppose that your code looked like this:
class MyClass
{
private int _myvar;
public int MyVar
{
get
{
return _myvar;
}
set
{
_myvar = value;
GoOffAndDoSomethingThatIsDoneWhenMyVarChanges(value);
}
}
public void ChangeMyVar(int NewValue)
{
_myvar = NewValue;
MyVar = NewValue;
}
GoOffAndDoSomethingThatIsDoneWhenMyVarChanges(int newValue)
{
}
}
Which method you use from within the class would depend on whether you wanted the 'side effect' to happen ('side effects' are usually, though not always, events).
From outside the class there is no choice you must use Method2.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Thank you , that is exactly what I need to understand ....
I know nothing , I know nothing ...
|
|
|
|
|
So I hope this has a Simple answer.
I have a program with 6 Options, they can be either RadioButtons or CheckBoxs, But I need to know how to limit the options to 3 or less?
Is there an easy way to do this with out checking how many are true and building a checker?
modified on Thursday, October 15, 2009 11:15 AM
|
|
|
|
|
Hi,
I have no idea what you mean by ControlButtons, did you mean CheckBoxes?
Anyway, the only way I know would be to attach this same handler to each of the relevant Controls; then either have a collection that holds the relevant controls or tag them somehow so you can easily enumerate them:
void CheckedChanged(object sender, EventArgs e) {
int count=0;
foreach(Control c in relevantControls) {
RadioButton rb=c as RadioButton;
if (rb!=null) count++;
}
if (count>MAX) {
RadioButton rb=sender as RadioButton;
if (rb!=null) rb.Checked=false;
}
}
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Yea I meant CheckBoxs or RadioButtons sorry, and yea I was looking for a way other than that.
|
|
|
|
|
You have your answers, But let me tell you there is big difrance betwen CheckBox and RadioBox
You can have a lot of checkBoxex. Each check box can be true or false
If you have more than 1 RadioBox, you can set it to true to ONLY ONE. To seperate betwen radioboxex (aka Grouping) place it in a panel
Edit: You can't check it without programming it. You need to check how many has ben set to true. There is no build in functions for your request
|
|
|
|
|
I have a Form called frmMain
In a differant class withing the same namespace I have a method (snippet):
frmMain Form1 = new frmMain();
Results(Form1.Label1);
I had expected to get the Labels name, instead I get the Labels' text. I tried Form1.Label1.Name, but that passes the name as a string versus a Label.
I am using an Event as explaind by DaveyM69 in a previous post of mine.
void Results (Label lbl_1)
{
OnUpdateLabels(new LabelsEventArgs(lbl_1));
}
Everything works and is passed over to the Forms methods, but since I am not passing a Lable name nothing happens. What I am trying to do is change the ForeColor of a lable. There are multiple labels that could be called to change. I thought about changeing everything to a string and passing the Form1.Label1.Name but I couldn't figure out, once in the Forms method how to conver the string into a Label.
modified on Thursday, October 15, 2009 11:13 AM
|
|
|
|
|
Hi,
from this and yesterday's posts, it seems to me you don't really have a clue about objects and Controls.
There is no "label's name" involved at all; if you want to operate on a Label, all you need is a reference to that Label; and I would guess Form1.Label1 returns exactly that.
I suggest you buy and study a book on C# or "Windows programming using C#"; in the time you have spent already you could have learned a lot by reading a structured book or course.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
I concede to the fact that I do not know enough about what I am doing. Which is why I am asking questions. I have tried to research and find proper solutions. But when I get stuck I have to ask.
"I would guess Form1.Label1 returns exactly that" is what I thought, so when it didn't work i got to checking things. I added a Messagebox, and Converted the variable being passed to a string and found I was getting this: Systems.Windows.Form Label, Text: Text on my Lable. Now this may be because I converted it to a string, but I think "Text on my Lable" is what is being passed versus Label1.
|
|
|
|
|
FYI: when you do string s="my label is "+label; then the compiler will add .ToString() to it, so string concatenation can be performed; and each class can override ToString() to return whatever string it likes to return; a label returns its class_name and its text. And not it's name.
As I said, variable names most often are irrelevant at run-time, references are the heart of the matter in object-oriented programs.
yogi_bear_79 wrote: Which is why I am asking questions.
Forums like these exist to provide an opinion on a general "how should I ..." type of question, or a pointer on a very specific question ("here is my code, I get 'A' instead of the expected 'B', what's up?"). In both cases the poster is expected to spend some time and effort in acquiring basic knowledge first. Hence: study a book so you know the basics of C#, the basics of OO principles, and the basics of Windows programming.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
("here is my code, I get 'A' instead of the expected 'B', what's up?").
First off, I am not trying to offend. My task was to convert a VB program to C#. I have completed everything except this one task. I have written the code (with help from the forumn, books and the Web). I am completey self-taught, and as a Network/Server administrator rarely do I delve into codeing. Alas, I will always be a rookie in this area. In an effort to keep the posts minimal I have only included snippets that I thought relevant. While I am sure my program could be written better by someone with more experience it does indeed work. I continually learn new/better ways to improve my codeing.
I belive my code should be passing the labels name via an event back to the form. Below I have provided everything I think is needed to see my attempt, maybe someone can see what I am doing wrong.
namespace VDP
{
public partial class frmMain : Form
{
private Poker poker;
public frmMain()
{
InitializeComponent();
poker = new Poker();
poker.UpdateLabels += new EventHandler<LablesEventArgs>(poker_UpdateLabels);
}
void poker_UpdateLabels(object sender, LablesEventArgs e)
{
UpdateLbls(e.Lbl);
}
public void UpdateLbls(Label lbl)
{
lbl.ForeColor = Color.Yellow;
}
}
}
namespace VDP
{
public class LablesEventArgs : EventArgs
{
public LablesEventArgs(Label lbl)
{
Lbl = lbl;
}
public Label Lbl
{
get;
private set;
}
}
}
namespace VDP
{
class Poker{
public event EventHandler<LablesEventArgs> UpdateLabels;
void Results(Label lbl_1)
{
OnUpdateLabels(new LablesEventArgs(lbl_1));
}
protected virtual void OnUpdateLabels(LablesEventArgs e)
{
EventHandler<LablesEventArgs> eh = UpdateLabels;
if (eh != null)
eh(this, e);
}
public void Hand_Analyzer()
{
frmMain Form1 = new frmMain();
Results(Form1.Label1);
}
}
}
|
|
|
|
|
this smells like the classic double Form mistake, although not enough code is available to be certain:
- Initially something is calling new frmMain() , probably your static Main method inside file program.cs
- this creates a new Poker()
- I'll be assuming somehow Hand_Analyzer() gets called on this poker
- now Hand_Analyzer() creates a new frmMain() which is NOT the original form, just a twin, and it is it's Label1 that gets set (although this second frmMain never gets shown on screen).
There are many solutions in this case; the one I prefer is:
- don't pass a GUI component to class Poker, it deals with cards and hands (that is "the business logic") and NOT with the user interaction;
- what you want to pass to the user is a string, hence come up with a StringEventArgs
- and modify UpdateLbls to UpdateLbls(string text)
Result:
1. it works
2. you can change the GUI (say use a RichTextBox or a ListBox instead of a Label) WITHOUT CHANGING class Poker.
That is "separation of concerns", important to get quality and reusability.
BTW: Did you notice you started in the completely wrong way (subject: Passing Label's Name...") as you didn't need the name of the Label, you did not even need the Label itself.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Ok, I tried this before I posted and got stuck as well. I just went through and changed everything to String. Now I am passing a Sting (i.e. "Label1") to public void UpdateLbls(String lbl). This is where I got stuck before. How do I convert the string into a Label so I can set the color?
public void UpdateLbls(String lbl){
lbl.ForeColor = Color.Yellow;
}
|
|
|
|
|
You should pass the result from the business logic to the GUI, so
if the result is a number, then put the number in your delegate parameter;
if, I repeat, if the result is a string (e.g. some text to show, NOT the name of a Control), then pass a string;
if the result is a color, then pass a color.
However, unless you are creating a paint program, I don't expect a color is the result of a calculation.
If you want to indicate a state, then the proper way to do it is by defining an enum holding all possible states, and then to output one of those enum values.
You DO need to study a book and start to understand what it is you do; I will not go on and type one page of such books at a time here.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
This is where I am confused I do want to pass the name of a control. My frmMain has a panel with labels on it. Each label indicates a winning hand. If a hand is won (i.e. Full House) then the label's forecolor should change from White to Yellow. Thus the hand analyzer decides if you have a full house. Then it passes the Label's Name that it wants the forecolor changed on.
Originally I thought I could use Form1.Label1 to pass the Labels name as a variable. That didn't work. So i tried Form1.Label1.Name, that passed the label's name, but as a string. I eventually settled on simply passing a string "Label1". All of these work to the point where the name is passed back to the Forms method UpdateLbls. However it is at this point that I need to be able to tell Label1.Forecolor what to do. But you cannot impliclty convert a String to Label. My book, nor the web has helped me on this matter.
|
|
|
|
|