|
True... but the API might contain something similar. I'd look around for a method called Invoke() on objects exposed by it. That mechanism isn't specific to Windows.Forms.Control , but to the System.ComponentModel.ISynchronizeInvoke interface, which is implemented by controls. Chances are that there is something similar available (there should be, if cross-thread calls are an issue).
However... I don't seem to remember the "wrong execution context" error message from inadvertently doing cross-thread method invocations in Windows.Forms. This could be something completely different. If it has anything to do with a System.Threading.ExecutionContext , there is something seriously broken in AutoCAD or its API. In the code presented, the OnIPCMessage() method shouldn't care less about from which thread it is invoked, and even if it did, the ExecutionContext should be just fine (it gets copied when the managed thread is started).
Or could it be that the code causing the problem isn't included in the post?
Nite!
--
Peter
|
|
|
|
|
How can you read USB device names and know the according com-port?
Thanks,
Kristof
|
|
|
|
|
|
I've been playing around with the StringBuilder class, and found something confusing...
The code is as follows:
static void Main(string[] args)
{
Stopwatch sw1 = new Stopwatch();
Console.WriteLine("Press enter to being the test.");
Console.ReadLine();
Console.WriteLine("Test 1 is addind 10 characters 1,000,000 times.");
Console.WriteLine("Test 1 Starting");
string smallWord = "1234567890";
StringBuilder longWord = new StringBuilder();
sw1.Start();
for (int j = 0; j < 1000000; j++)
{
longWord.Append(smallWord);
}
sw1.Stop();
Console.WriteLine("Test 1 Complete in " + sw1.ElapsedMilliseconds + "ms");
Console.WriteLine("-");
Console.WriteLine("Test 2 is removing 10 characters 500 times.");
Console.WriteLine("Test 2 Starting (Will take several seconds)");
sw1.Reset();
sw1.Start();
for (int k = 0; k < 500; k++)
{
longWord.Remove(0, 10);
}
sw1.Stop();
Console.WriteLine("Test 2 Complete in " + sw1.ElapsedMilliseconds + "ms");
Console.WriteLine("-");
Console.WriteLine("Press Enter to Quit.");
Console.ReadLine();
}
My question:
How come it is EXTREMELY fast(91ms) to ADD 10,000,000 characters, yet takes SO long (+- 6 seconds) to remove a total of 5000 characters?
Is there anyway to optimise the above code to make it any faster?
Any help would be appreciated
-= Reelix =-
|
|
|
|
|
The StringBuilder uses an internal buffer that normally has some unused space at the end, so when you add characters to it, they are just placed in this unused space. It's only when it runs out of unused space that it has to allocate a new buffer an copy all data to it. The new buffer is twice the size of the original buffer, so it only has to do this 24 times to accomodate 10000000 characters.
If you remove some characters at the beginning of the buffer, all the remaining characters have to be moved. Doing this 500 times should take somewhere around 100 times as long as creating the string. Which is does.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
sounds interesting. another point is, that a test 3 (removing backwards) will run in 0ms (?!):
sw1.Start();
for (int k = 0; k < 500; k++)
{
longWord.Remove(longWord.Length - 10, 10);
}
sw1.Stop();
|
|
|
|
|
buchstaben wrote: another point is, that a test 3 (removing backwards) will run in 0ms (?!):
That is because the internal length of the string is being updated only. The memory is not being released. Therefore all you are doing is updating an int 500 times - That is a very cheap operation.
|
|
|
|
|
Interesting...
In my ACTUAL case, I'm working with a string 50,000,000 characters long, with breakpoints ("-") at specific intervals.
I was wanting to iterate through, using my SubString Method
static class ExtMethod
{
public static StringBuilder SubString(this StringBuilder sr1, int startPos, int length)
{
StringBuilder randomString = new StringBuilder();
char[] charArray = new char[length];
sr1.CopyTo(startPos, charArray, 0, length);
randomString.Append(charArray);
return randomString;
}
}
(Which has quick speeds at high volume)
Add to a separate list, remove, and repeat.
Working backwards will have the same desired effect, so I shall use that instead.
Many thanks for all the assistance
Edit:
For those who are attempting my StringBuilder.SubString(int startPos, int len) method, it will only work in .NET 3.5 (Possibly 3.0) as it is an extension method
-= Reelix =-
|
|
|
|
|
I don't think you need the charArray. I'd write it:
StringBuilder output = new StringBuilder(length);
for ( int i = 0; i < length; ++i )
{
output.Append( sr1.Chars[i] );
}
return output; Untested, YMMV, etc.
DoEvents: Generating unexpected recursion since 1991
|
|
|
|
|
hi!
I need to read the sql messages returned from a incode "ScriptDiagram2.ExecuteNonQuery();" call without changing the script returning this messages.
a pic explaining what i mean:
http://shrani.si/f/2X/12w/3xylbIqA/untitled.jpg[^]
is this possible? how could i read this.
Thanks for any help!
|
|
|
|
|
This isn't specific to C#, but anyway: write an event handler for the InfoMessage event of the connection (see the docs for details). It will fire for PRINT statements and errors with a severity of 10 or lower. Also see the documentation of the FireInfoMessageEventOnUserErrors property - it alters the behaviour of the InfoMessage event.
An example of what you want to do is here: http://devio.wordpress.com/2008/05/15/displaying-printed-sql-output-in-aspnet/[^] (Google for "sqlcommand print" - no quotes - and there are other examples as well).
Later,
--
Peter
|
|
|
|
|
hi
i am writing a new UerControl (inherites from MaskTextBox), and i want to override some events like OnKeyDown, like this :
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
if (this.SelectionStart < 4)
{
string text = this.Text;
this.strYear = text.Substring(0, 4);
int num = int.Parse(this.strYear) + 1;
this.strYear = num.ToString();
this.strDate = string.Format("{0}/{1}/{2}", this.strYear, this.strMonth, this.strDay);
this.Text = this.strDate;
e.Handled = false;
}
}
base.OnKeyDown(e);
}
as u can see, when user press Up button, the year increament 1 unit, it works correctly, but i want to cancel event to prevent change focus character, while the focus character changed when user press Up button, i'm also use e.Handled = false; to prevent this, but it's not affects, how to solved my problem ?
thanks
|
|
|
|
|
If you want to cancel the key press you need to set Handled property to true.
Giorgi Dalakishvili
#region signature
my articles
#endregion
|
|
|
|
|
thanks Giorgi
but when i comment this code, your suggestion works correctly :
if (this.SelectionStart < 4)
{
e.Handled = true;
}
otherWise doesn't works correctly.
please test my code.
thanks
|
|
|
|
|
I am using IFilter got the property id and property value of docx file but can't get the property name.
But I am unable to get the property name. So please help me.
|
|
|
|
|
I have to chop up some seriously large TIFF files into small tiles in png format.
The source files are approxx 8000x8000 pixels, and the output tiles will be about 100x100 pixels.
I've written code to do this with GDI+ :- open the source image, and loop through creating target images, and getting Graphics.FromImage etc to draw them.
but it's very very very slow. I've tried multithreading to use my multiple cores, but the threads don't seem to schedule simultaneously - seems that something in GDI+ is stopping that working.
Anyone know of any suitable image processing componenets that can do this task more effiecently?
Thanks
Jon
|
|
|
|
|
Have you tried scanning through your source image using an unsafe region?
public static bool Extract(Bitmap b)
{
BitmapData bmData = b.LockBits(new Rectangle(0,0, b.Width, b.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
IntPtr scan0 = bmData.Scan0;
unsafe
{
byte *p = (byte*)(void*)scan0;
}
} The code you are using to extract your image portions would obviously go after the byte* p section.
|
|
|
|
|
I created one custom control and would like to use this control in other application. Usually by using dll of custom control in application we can use that control.
But I used _paint event in my class to create the control, which is not possible to create the same in class library project.
Is there any other way to include the custom control dll in my application. Thanks in advance.
|
|
|
|
|
Krish Agn wrote: But I used _paint event in my class to create the control, which is not possible to create the same in class library project.
What makes you think this ?
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
My custom control is: One Textbox control is merged in a cirle, So the shape of textbox looks round instead of square/rectangle.
Is there anyway to use textbox control in classLibrary project?
|
|
|
|
|
Try importing the System.Windows.Forms DLL?
Will make your class library ALOT bigger, yet it will work
I had the same issue whilst attempting to create custom Paint events in my DLL
-= Reelix =-
|
|
|
|
|
Say i have a server running and several client connected to this server. How to program
1)to detect when a client on purpose power down his PC without closing the connection
2)the client's pc hang causing connection to server be disconnected
Thanks in advance
|
|
|
|
|
One possible way is to keep some table to track the last activity time of each client. If it is more than some acceptable window we could assume there is something went wrong with the client.
Thanks
Laddie
Kindly rate if the answer was helpful
|
|
|
|
|
what will be the best timing to consider a connection has broken
|
|
|
|
|
It is a matter of what your application does...You will have to figure it out but taking into consideration the the frequencey in which the client communicates to the server.
Thanks
Laddie
Kindly rate if the answer was helpful
|
|
|
|