|
Yo Mik ,
Its working perfect the only small problem i got left is when i want to add the recieved stuff to a listbox i get the following error :
Additional information: Cross-thread operation not valid: Control 'lbReceived' accessed from a thread other than the thread it was created on.
Any idea how i can fix this ?
Greetings ,
Jacco
|
|
|
|
|
hehe this is where it gets complicated. i haven't done much with threads at all but i also came across this problem when writing a "popup" class. as you may have noticed you were starting another thread for the listening method. the thread where all the controls remain to is the one where you started the second thread from. sounds complicated but it isn't.
now you have to communicate from one thread to another. you can do this by using the invoke procedure of the control. i just will give you the sourcecode and you will see what will happen
instead of using the regular add method of the listbox you have to use the listboxs "invoke" method.
Put this code somewhere inside your class:
<br />
private delegate void AddTextInvoker(string textToAdd);<br />
Now replace the line of code where you want to add an item to the listbox with these lines:
<br />
object [] invokeParams = {"TextOrItemToAdd"};<br />
lbReceived.Invoke(new AddTextInvoker(AddTextToListbox), invokeParams);<br />
finally add the "AddTextToListbox" method to your class and make it add an item to the listbox:
<br />
private void AddTextToListbox(string textToAdd)<br />
{<br />
lbReceived.Add(textToAdd);<br />
}<br />
but be careful with invoking anything. it happened to me that one thread disposed a control and another one tried to change on of its properties - that will result in an exception. you should try to make yourself more familiar with threads since my knowledge ends here too. maybe someone else could tell you WHY its not possible to do a cross-thread operation and stuff - for now you should be able to solve your problem :P
// edit: sorry code snippet did not work because it just was wrong - now it should work :P
-- modified at 9:00 Tuesday 10th October, 2006
|
|
|
|
|
What will be the effect on the performence of the Operating System, when the garbage collector is periodically called (GC.collect()) ?
Mandar Kulkarni
|
|
|
|
|
The garbage collector wont affect the Operating system. It may affect a little with a slow processor but with fast processors it wont affect.
By the way u don't need to call the Garbage collector cause it is been called automatically when the processor has enough time to do that work.
Jamil Abou Khalil
|
|
|
|
|
The Garbage Collector runs on a per process basis, so the worst it can do to the OS is what a badly behaving program can do. Why do you want to explicitly call GC.Collect ? I'm sure you know that the GC runs automatically whenever it senses memory pressure and explicitly calling GC.Collect is not going to free any more objects than the next GC cycle is going to collect.
|
|
|
|
|
Hello
i have a simple windows form with a TabControl and several TabPages. I've added Event Handlers for the TabPage's Enter/Leave/Validating events.
The Events never fire! The same code build with VS2005 works!
How can i get the Events to work with VS2003 and .Net 1.1 ?
Michael
|
|
|
|
|
You have to be aware that VS 2005 differs a lot from the old ones. Unlike the VS2003 and .Net 1.1 that are much similar to each other. In VS2005 many of the namespaces have changed and many were combined with some other and many new where added.U can't expect for the same coding to work for VS2005 and VS2003.
Maybe this is not the case. Maybe you are not fire the events. If you are just copy and pate the source code then you wouldn't be initalizing the events. You should go to each tab seperatly and fire the events.
Hope it will benefit you.
Jamil abou khalil
|
|
|
|
|
Thanks but that does not help me
I simply created a new C# windows app, dragged a TabControl from the Toolbox and added two Tabpages.
Then I made to following changes to the resulting code:
public Form1 () {
InitializeComponent ();
this.tabPage1.Leave += new EventHandler ( tabPage1_Leave );
this.tabPage1.Enter += new EventHandler ( tabPage1_Enter );
}
void tabPage1_Enter ( object sender, EventArgs e ) {
Debug.WriteLine ( "tabPage1_Enter" );
}
void tabPage1_Leave ( object sender, EventArgs e ) {
Debug.WriteLine ( "tabPage1_Leave" );
}
I expect the Events to fire when the user selects a tabpage. That does not happen.
The same code imported with VS2005 works!
What can I do?
Michael
|
|
|
|
|
Hi,
i don't know when those events are fired (they are not the same as MouseLeave and MouseEnter) but if you want to fire an event when switching/selecting another tabpage you could use the TabControls "TabIndexChanged" event.
Make sure you REALLY fire the events. (maybe try mouseenter and mouseleave instead just to make sure that it works at all!)
|
|
|
|
|
mikone wrote: TabControls "TabIndexChanged" event.
The problem here is that the event is raised after the new tab is drawn. I can reactivate the previous page but then i see the page flicker.
What i want to do is prevent the page change.
My current solution is to override the OnSelectedIndexChanged(EventArgs e) method. But that looks awfully cumbersome given the fact that I should be able to page enter / page leave events. Only it does not work because there is a bug in .Net 1.1 that has been fixed in version 2.0
Cheers
Michael
|
|
|
|
|
how can we use isnumeric in c#
thanks
|
|
|
|
|
you could youse double.TryParse or int.TryParse - those methods take two arguments.
The first one is the value to parse.
The second one is the variable to write to (i guess its always passed as 'out' - look at the method header for further information)
The method will return true if the value could be parsed successfully.
|
|
|
|
|
but these meathods through exceptions which is a lag in performance
It is Good to be Important but!
it is more Important to be Good
|
|
|
|
|
Hello,
Amar Chaudhary wrote: these meathods through exceptions
No, thats not true.
That's the differents between 'Parse' and 'Convert.ToInt32'.
If it's possible for you you should use 'TryParse' in an 'if' == true statement.
All the best,
Martin
|
|
|
|
|
I really doubt MS would implement it as such but I would really like to see documentation on the way Int32.TryParse was implemented just so I can see for myself. I suppose, if I wasn't so lazy I could just look at the MSIL ... actually I think I am about to do that.
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage
|
|
|
|
|
No try catch. I would post a snippet but it is way long. Int32.TryParse traces all the way up to Number.ParseNumber.
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage
|
|
|
|
|
You can use this method as there is no built in methods.
private bool IsNumeric(object numberString)
{
string strVal = numberString.ToString();
char [] ca = strVal.ToCharArray();
for (int i = 0; i < ca.Length;i++)
{
if (ca[i] > 57 || ca[i] < 48)
{
if(ca[i] != 46 )
return false;
}
}
return true;
}
Ranjith Stephen
|
|
|
|
|
nice and should include 43 and 45 also
It is Good to be Important but!
it is more Important to be Good
|
|
|
|
|
u can try
private bool IsNumeric(object numberString)
{
string strVal = numberString.ToString();
char [] ca = strVal.ToCharArray();
for (int i = 0; i < ca.Length;i++)
{
if (Char.IsNumber(ca[i]))
return false;
else
return true;
}
}
rahul
|
|
|
|
|
Ithink you just check ca[0]
|
|
|
|
|
gr8 Rahul
Ranjith Stephen
|
|
|
|
|
ranjithlogics wrote: gr8 Rahul
Yes "gr8" indeed as in reading this makes me want to gr8 my forehead with a cheese gr8er.
Specifically
if (Char.IsNumber(ca[i]))<br />
return false;
within a method called IsNumeric - whose purpose is to return true when a string is numeric.
|
|
|
|
|
rah_sin wrote: for (int i = 0; i < ca.Length;i++)
{
if (Char.IsNumber(ca[i]))
return false;
else
return true;
}
This logic is so badly flawed..
1) false is returned if a character is numeric - the purpose of the method was supposed to be returning true if a caracter is numeric
2) It always returns on the first character, rather than checking the whole string is made up of numbers... basically the for loop is useless.
|
|
|
|
|
my aim was not to write codes for u rather to suggest the method
Char.IsNumber();
now its upto u that how can u use it for ur purpose
i think u people are smart enough to write ur ownn logics.
rahul
|
|
|
|
|
As has been hinted, rather badly, by previous posters the way to write an isnumeric method is to iterate over each character in a string checking whether Char.IsNumber is true or char == '.' for every character in the string.
You may want to remove formatting from the number, as often numbers may be separated with a comma (1,000,000) and sometimes numbers are a representation of currency (£100) - you may or may not want these two cases to be treated as valid numerics.
|
|
|
|