.NET

Reply
*Carlos
Message 1 of 13 (261 Views)

Multi-threading question

261 Views, 12 Replies
08-10-2005 10:31 AM
Hi,

I've got a database that I access through C# web services. I need to update
an AutoCAD drawing if anything in that database changes. I've created a
thread that regularly asks the database if there is anything new. If there
is, I'd like to somehow make some changes in AutoCAD. Is this possible?
Can I trigger an event in AutoCAD from a seperate thread?

Here's how I make the thread:

[CommandMethod("StartPollingForEvents")]
public static void StartPollingForEvents()
{

// Define the thread's target method

PollingManager pollingManager = new PollingManager();
Thread t = new Thread(new ThreadStart
(pollingManager.ExecuteThread));

// Start the thread

t.Start();
t.Abort();
while (!t.IsAlive);
Thread.Sleep(100);
t.Join();
}


Here's the thread's target method:

public void ExecuteThread()
{
CommandLinePrompts.Message("\n ExecuteThread method entered.");

while(true)
{
CommandLinePrompts.Message("\n Output from thread...");
}
}

I get no output to the command line when I run this code.

Thanks very much for any help or ideas.

-Carlos
*Tony Tanzillo
Message 2 of 13 (261 Views)

Re: Multi-threading question

08-10-2005 12:30 PM in reply to: *Carlos
Why not ditch the thread, and query the database
for changes from the EnteringQuiescentState event
of the Editor? That's the only time that it would be
appropriate to take control of the editor away from
the user anyway.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Carlos" wrote in message news:4924984@discussion.autodesk.com...
Hi,

I've got a database that I access through C# web services. I need to update
an AutoCAD drawing if anything in that database changes. I've created a
thread that regularly asks the database if there is anything new. If there
is, I'd like to somehow make some changes in AutoCAD. Is this possible?
Can I trigger an event in AutoCAD from a seperate thread?

Here's how I make the thread:

[CommandMethod("StartPollingForEvents")]
public static void StartPollingForEvents()
{

// Define the thread's target method

PollingManager pollingManager = new PollingManager();
Thread t = new Thread(new ThreadStart
(pollingManager.ExecuteThread));

// Start the thread

t.Start();
t.Abort();
while (!t.IsAlive);
Thread.Sleep(100);
t.Join();
}


Here's the thread's target method:

public void ExecuteThread()
{
CommandLinePrompts.Message("\n ExecuteThread method entered.");

while(true)
{
CommandLinePrompts.Message("\n Output from thread...");
}
}

I get no output to the command line when I run this code.

Thanks very much for any help or ideas.

-Carlos
*Carlos
Message 3 of 13 (261 Views)

Re: Multi-threading question

08-10-2005 02:39 PM in reply to: *Carlos
Hi,

I'm using AutoCAD 2005 and VS 2003, and I can't find anything about the
EnteringQuiescentState event. Is that only in 2006?

The database (not the AutoCAD database, but a different one external to
AutoCAD, accessed by my DLL via C# web services) that I mentioned stores
important alerts. Somehow, I need to check this database (about once per
second) and send the user a message in AutoCAD if there are any new alerts.
Ideally, this would occur even if the user was busy working on something
else in the drawing (so it's busy running one function again and again to
check for alerts while the user is doing other stuff and calling other
functions).

It would be alright with me if the alerts database was queried only when the
user wasn't doing anything, but it would still have to occur frequently
while the user idles. These queries would then have to be interrupted if
the user started doing something.

Is this sort of thing possible?

Thank you very much for your help. I really appreciate it.

-Carlos

"Tony Tanzillo" wrote in message
news:4925222@discussion.autodesk.com...
Why not ditch the thread, and query the database
for changes from the EnteringQuiescentState event
of the Editor? That's the only time that it would be
appropriate to take control of the editor away from
the user anyway.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Carlos" wrote in message
news:4924984@discussion.autodesk.com...
Hi,

I've got a database that I access through C# web services. I need to update
an AutoCAD drawing if anything in that database changes. I've created a
thread that regularly asks the database if there is anything new. If there
is, I'd like to somehow make some changes in AutoCAD. Is this possible?
Can I trigger an event in AutoCAD from a seperate thread?

Here's how I make the thread:

[CommandMethod("StartPollingForEvents")]
public static void StartPollingForEvents()
{

// Define the thread's target method

PollingManager pollingManager = new PollingManager();
Thread t = new Thread(new ThreadStart
(pollingManager.ExecuteThread));

// Start the thread

t.Start();
t.Abort();
while (!t.IsAlive);
Thread.Sleep(100);
t.Join();
}


Here's the thread's target method:

public void ExecuteThread()
{
CommandLinePrompts.Message("\n ExecuteThread method entered.");

while(true)
{
CommandLinePrompts.Message("\n Output from thread...");
}
}

I get no output to the command line when I run this code.

Thanks very much for any help or ideas.

-Carlos
*Tony Tanzillo
Message 4 of 13 (261 Views)

Re: Multi-threading question

08-10-2005 06:13 PM in reply to: *Carlos
The event I mentioned is only available in 2006
(and later, assuming they don't change things
again). My advice to anyone regarding .NET in
2005, is to not invest any major effort in that
release of the API.

Regardless of that, I am not sure I understand
what the intent of your code is. It makes no sense
to start a thread; then abort it; then enter a loop
that executes infinitely and does nothing but calls
Sleep(). Sleep() will cause the calling thread to
block for the specified time. Since the thread you
aborted is not running when you get to the while()
loop, IsAlive will always return false and the loop
will execute infinitely.

I don't think I understand what the intent is there.

Lastly, you call Join() on a thread that is not
running to begin with (because you aborted it
with the call to Abort()).

Does the CommandMethod you show actually
return control to the Command: line when you
execute it? From the looks of the code, that is
not going to happen, and all that command is
going to do is block the document's thread,
causing AutoCAD to not respond to mouse or
keyboard input.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Carlos" wrote in message news:4925444@discussion.autodesk.com...
Hi,

I'm using AutoCAD 2005 and VS 2003, and I can't find anything about the
EnteringQuiescentState event. Is that only in 2006?

The database (not the AutoCAD database, but a different one external to
AutoCAD, accessed by my DLL via C# web services) that I mentioned stores
important alerts. Somehow, I need to check this database (about once per
second) and send the user a message in AutoCAD if there are any new alerts.
Ideally, this would occur even if the user was busy working on something
else in the drawing (so it's busy running one function again and again to
check for alerts while the user is doing other stuff and calling other
functions).

It would be alright with me if the alerts database was queried only when the
user wasn't doing anything, but it would still have to occur frequently
while the user idles. These queries would then have to be interrupted if
the user started doing something.

Is this sort of thing possible?

Thank you very much for your help. I really appreciate it.

-Carlos

"Tony Tanzillo" wrote in message
news:4925222@discussion.autodesk.com...
Why not ditch the thread, and query the database
for changes from the EnteringQuiescentState event
of the Editor? That's the only time that it would be
appropriate to take control of the editor away from
the user anyway.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Carlos" wrote in message
news:4924984@discussion.autodesk.com...
Hi,

I've got a database that I access through C# web services. I need to update
an AutoCAD drawing if anything in that database changes. I've created a
thread that regularly asks the database if there is anything new. If there
is, I'd like to somehow make some changes in AutoCAD. Is this possible?
Can I trigger an event in AutoCAD from a seperate thread?

Here's how I make the thread:

[CommandMethod("StartPollingForEvents")]
public static void StartPollingForEvents()
{

// Define the thread's target method

PollingManager pollingManager = new PollingManager();
Thread t = new Thread(new ThreadStart
(pollingManager.ExecuteThread));

// Start the thread

t.Start();
t.Abort();
while (!t.IsAlive);
Thread.Sleep(100);
t.Join();
}


Here's the thread's target method:

public void ExecuteThread()
{
CommandLinePrompts.Message("\n ExecuteThread method entered.");

while(true)
{
CommandLinePrompts.Message("\n Output from thread...");
}
}

I get no output to the command line when I run this code.

Thanks very much for any help or ideas.

-Carlos
*Carlos
Message 5 of 13 (261 Views)

Re: Multi-threading question

08-11-2005 11:42 AM in reply to: *Carlos
Yeah, this 2005 stuff is really a bummer. So is the boss's reluctance to
spend money.

I had some of those lines out of order (it was my first introduction to
threads). I think my second attempt is a little better.

When I type the command, AutoCAD pauses for about ten seconds and returns
control to the command line. However, the value of UserI1 doesn't get
changed. Can I get access to AutoCAD from the other thread?

Thanks so much for your help.

Here's my code:

[CommandMethod("StartPollingForEvents")]
public static void StartPollingForEvents()
{
prompt.Message("\n About to start polling for events.");

PollingManager pm = new PollingManager();
Thread t = new Thread(new ThreadStart(pm.Start));

t.Start();
while (!t.IsAlive);
Thread.Sleep(10000);
t.Abort();
t.Join();

Database db =
Application.DocumentManager.MdiActiveDocument.Database;
AcadDocument doc = (AcadDocument)
Application.DocumentManager.MdiActiveDocument.AcadDocument;
string userI = doc.GetVariable("UserI1").ToString();

prompt.Message("\n The thread has finished.");
prompt.Message(" UserI1: " + userI);
}

public void Start()
{
int i=0;
while (true)
{
Database db =
Application.DocumentManager.MdiActiveDocument.Database;
AcadDocument doc = (AcadDocument)
Application.DocumentManager.MdiActiveDocument.AcadDocument;

doc.SetVariable("UserI1", i);
i++;
}
}



"Tony Tanzillo" wrote in message
news:4925561@discussion.autodesk.com...
The event I mentioned is only available in 2006
(and later, assuming they don't change things
again). My advice to anyone regarding .NET in
2005, is to not invest any major effort in that
release of the API.

Regardless of that, I am not sure I understand
what the intent of your code is. It makes no sense
to start a thread; then abort it; then enter a loop
that executes infinitely and does nothing but calls
Sleep(). Sleep() will cause the calling thread to
block for the specified time. Since the thread you
aborted is not running when you get to the while()
loop, IsAlive will always return false and the loop
will execute infinitely.

I don't think I understand what the intent is there.

Lastly, you call Join() on a thread that is not
running to begin with (because you aborted it
with the call to Abort()).

Does the CommandMethod you show actually
return control to the Command: line when you
execute it? From the looks of the code, that is
not going to happen, and all that command is
going to do is block the document's thread,
causing AutoCAD to not respond to mouse or
keyboard input.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Carlos" wrote in message
news:4925444@discussion.autodesk.com...
Hi,

I'm using AutoCAD 2005 and VS 2003, and I can't find anything about the
EnteringQuiescentState event. Is that only in 2006?

The database (not the AutoCAD database, but a different one external to
AutoCAD, accessed by my DLL via C# web services) that I mentioned stores
important alerts. Somehow, I need to check this database (about once per
second) and send the user a message in AutoCAD if there are any new alerts.
Ideally, this would occur even if the user was busy working on something
else in the drawing (so it's busy running one function again and again to
check for alerts while the user is doing other stuff and calling other
functions).

It would be alright with me if the alerts database was queried only when the
user wasn't doing anything, but it would still have to occur frequently
while the user idles. These queries would then have to be interrupted if
the user started doing something.

Is this sort of thing possible?

Thank you very much for your help. I really appreciate it.

-Carlos

"Tony Tanzillo" wrote in message
news:4925222@discussion.autodesk.com...
Why not ditch the thread, and query the database
for changes from the EnteringQuiescentState event
of the Editor? That's the only time that it would be
appropriate to take control of the editor away from
the user anyway.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Carlos" wrote in message
news:4924984@discussion.autodesk.com...
Hi,

I've got a database that I access through C# web services. I need to update
an AutoCAD drawing if anything in that database changes. I've created a
thread that regularly asks the database if there is anything new. If there
is, I'd like to somehow make some changes in AutoCAD. Is this possible?
Can I trigger an event in AutoCAD from a seperate thread?

Here's how I make the thread:

[CommandMethod("StartPollingForEvents")]
public static void StartPollingForEvents()
{

// Define the thread's target method

PollingManager pollingManager = new PollingManager();
Thread t = new Thread(new ThreadStart
(pollingManager.ExecuteThread));

// Start the thread

t.Start();
t.Abort();
while (!t.IsAlive);
Thread.Sleep(100);
t.Join();
}


Here's the thread's target method:

public void ExecuteThread()
{
CommandLinePrompts.Message("\n ExecuteThread method entered.");

while(true)
{
CommandLinePrompts.Message("\n Output from thread...");
}
}

I get no output to the command line when I run this code.

Thanks very much for any help or ideas.

-Carlos
*Carlos
Message 6 of 13 (261 Views)

Re: Multi-threading question

08-11-2005 01:23 PM in reply to: *Carlos
Hi again,

If I was to get the AutoCAD 2006 upgrade and react to the
EnteringQuiescentState event as you suggested, do you think I'd be able to
accomplish my goal?

Maybe the method could go something like this:

-React to EnteringQuiescentState event
-while (true)
{
-Check my database for alerts
-If there's an alert, make a change in AutoCAD
-Check to see if we are not in a quiescent state any more
-If we are not, break out of while loop
}


The main thing is that the code should continue checking for alerts again
and again. If the user wants to do something, it could stop and give
control back to the user.

It's not enough to just check the database once each time the quiescent
state is entered. In case the user sits around and leaves AutoCAD running
for a long time without doing anything, I want them to be notified of an
alert as soon as possible.

Do you think this would work in AutoCAD 2006?

Thanks.
-Carlos


"Tony Tanzillo" wrote in message
news:4925561@discussion.autodesk.com...
The event I mentioned is only available in 2006
(and later, assuming they don't change things
again). My advice to anyone regarding .NET in
2005, is to not invest any major effort in that
release of the API.

Regardless of that, I am not sure I understand
what the intent of your code is. It makes no sense
to start a thread; then abort it; then enter a loop
that executes infinitely and does nothing but calls
Sleep(). Sleep() will cause the calling thread to
block for the specified time. Since the thread you
aborted is not running when you get to the while()
loop, IsAlive will always return false and the loop
will execute infinitely.

I don't think I understand what the intent is there.

Lastly, you call Join() on a thread that is not
running to begin with (because you aborted it
with the call to Abort()).

Does the CommandMethod you show actually
return control to the Command: line when you
execute it? From the looks of the code, that is
not going to happen, and all that command is
going to do is block the document's thread,
causing AutoCAD to not respond to mouse or
keyboard input.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Carlos" wrote in message
news:4925444@discussion.autodesk.com...
Hi,

I'm using AutoCAD 2005 and VS 2003, and I can't find anything about the
EnteringQuiescentState event. Is that only in 2006?

The database (not the AutoCAD database, but a different one external to
AutoCAD, accessed by my DLL via C# web services) that I mentioned stores
important alerts. Somehow, I need to check this database (about once per
second) and send the user a message in AutoCAD if there are any new alerts.
Ideally, this would occur even if the user was busy working on something
else in the drawing (so it's busy running one function again and again to
check for alerts while the user is doing other stuff and calling other
functions).

It would be alright with me if the alerts database was queried only when the
user wasn't doing anything, but it would still have to occur frequently
while the user idles. These queries would then have to be interrupted if
the user started doing something.

Is this sort of thing possible?

Thank you very much for your help. I really appreciate it.

-Carlos

"Tony Tanzillo" wrote in message
news:4925222@discussion.autodesk.com...
Why not ditch the thread, and query the database
for changes from the EnteringQuiescentState event
of the Editor? That's the only time that it would be
appropriate to take control of the editor away from
the user anyway.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Carlos" wrote in message
news:4924984@discussion.autodesk.com...
Hi,

I've got a database that I access through C# web services. I need to update
an AutoCAD drawing if anything in that database changes. I've created a
thread that regularly asks the database if there is anything new. If there
is, I'd like to somehow make some changes in AutoCAD. Is this possible?
Can I trigger an event in AutoCAD from a seperate thread?

Here's how I make the thread:

[CommandMethod("StartPollingForEvents")]
public static void StartPollingForEvents()
{

// Define the thread's target method

PollingManager pollingManager = new PollingManager();
Thread t = new Thread(new ThreadStart
(pollingManager.ExecuteThread));

// Start the thread

t.Start();
t.Abort();
while (!t.IsAlive);
Thread.Sleep(100);
t.Join();
}


Here's the thread's target method:

public void ExecuteThread()
{
CommandLinePrompts.Message("\n ExecuteThread method entered.");

while(true)
{
CommandLinePrompts.Message("\n Output from thread...");
}
}

I get no output to the command line when I run this code.

Thanks very much for any help or ideas.

-Carlos
*Tony Tanzillo
Message 7 of 13 (261 Views)

Re: Multi-threading question

08-11-2005 03:02 PM in reply to: *Carlos
"Carlos" wrote

>> Yeah, this 2005 stuff is really a bummer.
>> So is the boss's reluctance to spend money.

Perhaps if the boss were made to see how it
could ultimatley cost more to stick with and
target 2005 for development, he might be a
bit more receptive to the idea of upgrading.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com
*Tony Tanzillo
Message 8 of 13 (261 Views)

Re: Multi-threading question

08-11-2005 07:52 PM in reply to: *Carlos
"Carlos" write:

>> If I was to get the AutoCAD 2006 upgrade and react to the
EnteringQuiescentState event as you suggested, do you think I'd be able to accomplish my goal? <<

Based on what little I know about your specifics, my
guess would be yes, if it is done correctly.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com
Member
dubinskyv
Posts: 4
Registered: ‎12-09-2005
Message 9 of 13 (261 Views)

Re: Multi-threading question

12-09-2005 01:46 PM in reply to: *Carlos
Hi. My name is Vadim and I have taken over the work started by Carlos (same project) I came accross this thread and thought I'd revive it. We are now using EnteringQuiescentState as Tony suggested, and that works great except that in order for our code to EnterQuiescentState the user has to first interact a little bit with the drawing, like clicking somewhere on the drawing. Instead what is desireable is for our database checking code that's meant to run as a background thread to run even if user takes no action with the drawing. As it is now if the user starts ACAD with our particular drawing and then does nothing at all, EnteringQuiescentState doesn't happen.

So is there some other way to run this background work? Thanks so much.
*Tony Tanzillo
Message 10 of 13 (261 Views)

Re: Multi-threading question

12-09-2005 09:10 PM in reply to: *Carlos
Sorry, I'm not sure I follow you.

I can't think of a scenario where someone starts
AutoCAD, and does nothing at all with it.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

wrote in message news:5034685@discussion.autodesk.com...
Hi. My name is Vadim and I have taken over the work started by Carlos (same project) I came accross this thread and thought I'd revive it. We are now using EnteringQuiescentState as Tony suggested, and that works great except that in order for our code to EnterQuiescentState the user has to first interact a little bit with the drawing, like clicking somewhere on the drawing. Instead what is desireable is for our database checking code that's meant to run as a background thread to run even if user takes no action with the drawing. As it is now if the user starts ACAD with our particular drawing and then does nothing at all, EnteringQuiescentState doesn't happen.

So is there some other way to run this background work? Thanks so much.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.