|
Hi All,
i have to show the starttime and the endtime of a activity in a progressbar. the bar has to peform one step every minute.
Can someone explain to me how to that with some code?
Thanx.
|
|
|
|
|
|
I have a problem while trying to execute a delegate in a different thread.
I have my application and autocad that has loaded my .net-dll.
I created a named-pipe-server (in an own thread in the dll for autocad) and send messages to it from my application. The thread with the server watis for the messages and executes a delegate on receiption. The method in my class (in the dll) is called and I can show it with a messagebox.
Till here everything is fine.
But now I want to open a new file for autocad and I try to use the regarding method-call (AcadApp.Application.DocumentManager.Open(file)). But here is the problem. I get an exception about internal error or wrong context. I'm still in the thread-context of my listening server and I think that is the problem. But how can I execute a function in the autocad-thread? My initialization-function that creates the listening-thread is executed in the autocad-thread-context so I can get Thread-object or whatever is needed.
|
|
|
|
|
You can try using MethodInvoker inside your thread to invloke the function in another thread.
Thanks
Laddie
Kindly rate if the answer was helpful
|
|
|
|
|
How does it work? Can you give me a sample? As you see I tried something but it doesn't work.
my code:
class IPCMsgHandler
{
private Thread listenThread = new Thread(ListeningThreadFunc);
...
public void Start()
{
listenThread.Start(this);
}
private static void ListeningThreadFunc(object data)
{
IPCMsgHandler helper = data as IPCMsgHandler;
...
MethodInvoker mi = new MethodInvoker(helper.OnMessage);
mi.Invoke();
...
}
}
public class AutoCADApplication : IExtensionApplication
{
IPCMsgHandler helper = null;
public void Initialize()
{
this.helper = new IPCMsgHandler("AutoCAD-IPCServer", new IPCMsgHandler.MessageHandler(this.OnIPCMessage));
this.helper.Start();
}
private void OnIPCMessage()
{
return;
}
|
|
|
|
|
Hi,
You need a reference to a control that has a window handle associated with it (something that inherits from System.Windows.Forms.Control) and lives on the Autocad main thread. Possibly you can get that through the Autocad API (with which I'm not at all familiar).
If you have that reference in a variable called control you invoke your delegate with control.Invoke(mi) instead of mi.Invoke() .
This routes the invocation through the event queue on the UI thread, which should solve your problem. Assuming you can get your hands on a suitable Control object of course...
--
Peter
|
|
|
|
|
That would work for a windows forms based API, and although I don't know the Autocad API either I wouldn't expect it to be Forms based. A better bet might be to implement a producer/consumer type of architecture[^].
Standards are great! Everybody should have one!
|
|
|
|
|
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.
|
|
|
|