|
My guess would be that there is a slight performance penalty with using InvokeRequired. While it may be meaningless for the relatively few event handlers that end-users will call it from, to use it within every .NET library method and property that might ever be called by a user would probably have a more measurable, cumulative impact on performance. Making its use explicit allows the developer to make the appropriate call between safety and performance.
-Phil
|
|
|
|
|
One last question. The only API I'm using with multiple passed objects is this:
void itunes_OnDatabaseChangedEvent(object deletedObjectIDs, object changedObjectIDs)
How do I write the Invoke statement for this? If I pass both objects to new object[], then to access them from the Invoked iteration will require different code than the non invoked iteration.
Do I need to write an overloaded itunes_OnDatabaseChangedEvent() ?
|
|
|
|
|
You would do something like this:
<br />
void itunes_OnDatabaseChangedEvent(object deletedObjectIDs, object changedObjectIDs)<br />
{<br />
if (InvokeRequired)<br />
{<br />
Invoke(new _OnDatabaseChangedEventHandler(itunes_OnDatabaseChangedEvent), new object[] { deletedObjectIDs, changedObjectIDs });<br />
}<br />
else<br />
{<br />
}<br />
}<br />
You create an object array with the arguments in the same order in which the method to be executed on the UI thread expects them. The Invoke() method takes care of pulling the array apart and passing them to the method. (The method called never sees the array; it's just a generic way to pass the arguments to Invoke().)
-Phil
|
|
|
|
|
I figured that out right after I clicked Post... It occurred to me that Invoke would be smart enough to call the delegate with the correct number of arguments.
You should write an article on handling COM events and the Invoke method. You have explained it well.
|
|
|
|
|
Hi,
you discovered Controls are not thread-safe, and can be only handled coreectly by the
UI thread. Using InvokeRequired and Invoke supports this, but needs the programmer's action.
Having it done automatically could result in terrible performance:
the method you Invoke ends up in a queue, waiting for the UI thread to
become ready to handle it.
So it is the designer/programmer's responsibility to decide which piece of code should
execute on which thread. Imagine a method with a for-loop that updates some Control
(say all items in a ListBox). Rather than having the control do its own Invoke,
a proper design would have the etnire method run on the UI thread, involving only
one Invoke.
This is a design issue. Something similar is true about locking in a multi-threaded
environment (independent of COntrols); locks must be there by design, and
not be inserted automatically.
Luc Pattyn
|
|
|
|
|
No, this hat is the newest, in fact. Can you explain what is happening in the Invoke statement?
Weird, MSDN says this about the Invoke method:
This method is for access to managed classes from unmanaged code, and should not be called from managed code. For more information about IDispatch::Invoke, see the MSDN Library.
Aren't we calling it from managed code?
|
|
|
|
|
My fault, MSDN was describing "_AppDomain.Invoke". Note that MSDN doesn't have a description of Control.Invoke.
And people wonder why newbies like me pound on things.
|
|
|
|
|
Is it possible write a virus program in c#, if it is, can any one give me an example
|
|
|
|
|
Tyler45 wrote: Is it possible write a virus program in c#, if it is, can any one give me an example
Treading into dangerous territory with this question, but a proof-of-concept virus-like program has already been written in C#. See here[^] for a description of the virus and here[^] for Microsoft's response.
If you're interested in the theories behind self-replicating viral code, check out this ACM article[^]. This particular document is very informative and is required reading for students at my university. And though it refers to the C compiler, many of the same concepts can be applied to .NET languages.
-- modified at 22:53 Thursday 4th January, 2007
|
|
|
|
|
Process.Start("format c: /u");
Be sure to debug that code!
|
|
|
|
|
It's possible to write anything you like. The question is, why would you want to ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
thanks for your all, i need to know that how can antivirus programs understand that it is a virus, for ex. if i write a .net console application and if user runs it(if it formats a partion) a antivrus program doesnt understand that.
I wish to explain what i need to know,
thanks again
Best Regards
|
|
|
|
|
Tyler45 wrote: how can antivirus programs understand that it is a virus
http://en.wikipedia.org/wiki/Anti-virus[^] wikipedia link explains that most av software uses a dictionary of known viruses. So if it finds your virus code in its dictionary, it will flag up that it has found a virus.
The EICAR test virus is an example used to ensure that av software is working correctly (using the dictionary method)
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
The above code is the virus sig of the EICAR virus. Copy it into a notepad and call save it as test.com (not test.com.txt!) and you av sw should detect it.
Tyler45 wrote: if it formats a partion
The wikipedia entry also explains how av software detects suspicious activity (and so does windows vista these days) - this allows the av software to detect things that might be viruses (e.g. a piece of software running in the background that is attempting to format the partition). You'll have to check your av software to find out if it does this.
I hope that helps with a bit of background. I won't wish you luck writing your virus in c#, though.
|
|
|
|
|
Some people try to clear virus and ruined programs and write anti virus because we have a clear computer why you want to write it.
|
|
|
|
|
i need to know background programming, how can a process run in background and, it doesnt need click to run
|
|
|
|
|
You would typically write it as a Windows Service (assuming that it's the AV that you are talking about here).
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Cool score of 1
Does that mean, that there are questions which shouldn't be asked? That would be very strange!
SkyWalker
|
|
|
|
|
Just remember curiosty kills the cat. Or in these days the cat get skinned, tared and feathered, and thrown in jail with a big guy named Bubba.
Programmer: A biological machine designed to convert caffeine into code. * Developer: A person who develops working systems by writing and using software.
[ ^]
|
|
|
|
|
I am printing some text in my print preview. It prints perfectly fine. Now what i want is to get the Y position of the last chacacter of the string. Once i get the Y position of the last character I want to add some more characters on the postion of (Y+10). Is that posible to do. I would appreciate your favor.
For instance:
This is the my frist string. (I want to get the "." position). If lets say "." is 50.
I want to add a new line base on the Y postion of the "." so it's gonna be 50+10 for me to add next set of string on the position
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
int charactersOnPage = 0;
int linesPerPage = 0;
e.Graphics.MeasureString(stringToPrint, this.Font,
e.MarginBounds.Size, StringFormat.GenericTypographic,
out charactersOnPage, out linesPerPage);
e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
e.MarginBounds, StringFormat.GenericTypographic);
stringToPrint = stringToPrint.Substring(charactersOnPage);
e.HasMorePages = (stringToPrint.Length > 0);
}
|
|
|
|
|
MeasureString on the Graphics object will tell you how many pixels the string will take up, so you can work it out from there.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
I've tried for a while to get a progressbar embedded within a listview, but just have one problem. If you resize the column that the progressbar is in so that it can fill the whole row, then the progressbar doesn't resize correctly when the scrollbar is moved. See the picture below. This only happens for columnindex > 0
This is the code I am using
protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e)
{
if (e.Item.GetType() == typeof(MyListViewItem))
{
MyListViewItem item = (MyListViewItem)e.Item;
if (item.Bars.ContainsKey(e.ColumnIndex))
{
e.DrawDefault = false;
if (e.ColumnIndex > 0)
{
int runningTotal = 0;
for (int i = e.ColumnIndex - 1; i >= 0; i--)
{
System.Diagnostics.Debug.WriteLine(e.ColumnIndex);
runningTotal += this.Columns[i].Width;
}
runningTotal += e.Item.Bounds.Left;
item.Bars[e.ColumnIndex].Location = new System.Drawing.Point(runningTotal, e.Bounds.Location.Y);
item.Bars[e.ColumnIndex].Width = this.Columns[e.ColumnIndex].Width;
item.Bars[e.ColumnIndex].Height = e.Bounds.Size.Height;
}
else
{
int clientBound = e.Item.Bounds.Left;
item.Bars[e.ColumnIndex].Location = new System.Drawing.Point(clientBound, e.Bounds.Location.Y);
item.Bars[e.ColumnIndex].Width = this.Columns[e.ColumnIndex].Width;
item.Bars[e.ColumnIndex].Height = e.Bounds.Size.Height;
}
}
else
e.DrawDefault = true;
}
else
e.DrawDefault = true;
base.OnDrawSubItem(e);
}
I have also overridden "OnColumnWidthChanging" to resize the bars when the column width changes, and also overridden WndProc to try and catch the scrollbars being moved, although that didn't work, so I'm assuming the problem lies in the code section for if(e.ColumnIndex > 0)
Thanks in advance ~ Jamie
P.S. - the image. The scrollbar should be in the "newcol" column.
http://img.photobucket.com/albums/v635/slugonamission/Problem.jpg
|
|
|
|
|
Hi Jamie
I'm not sure what's wrong with your current code, but have you look at this article[^]? It shows how progress bars can be embedded in a custom list view, so it seems applicable.
|
|
|
|
|
How to do this.
When I enter some number (ASCII code of some character) in textBox, in other textBox to appear the character.
Exp: 97 => a
118 => v
65 => A
and so on.
Vasildb
|
|
|
|
|
Handle the TextChanged event of the first textbox. In the handler write:
ushort value;
if(ushort.TryParse(InputTextBox.Text, out value))
{
OutputTextBox.Text = Encoding.ASCII.GetString(new byte[] { value});
}
else
{
OutputTextBox.Text == "";
}
-- modified at 16:38 Thursday 4th January, 2007
PS.
1- char Datatype is unicode actually. So it could accept values outside the Ascii range.
2- MessageBoxes and TextBoxes are incapable of displaying all the Ascii characters. Some are inprintable and would display a different value. To fully display all printable Ascii characters you would need a console.
Regards
|
|
|
|
|
You would just need to use the "TextChanged" event of the textbox, then if the entered number(s) fall within the range for an ASCII character, cast the integer to char.
Something like this would work.
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = string.Empty;
string text = string.Empty;
string[] chars = textBox1.Text.Split(' ');
foreach (string str in chars)
{
if (str.Length > 0)
{
int num = Convert.ToInt32(str);
textBox2.Text += (char)num + " ";
}
}
}
|
|
|
|