.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Multi-threading question

12 REPLIES 12
Reply
Message 1 of 13
Anonymous
867 Views, 12 Replies

Multi-threading question

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
12 REPLIES 12
Message 2 of 13
Anonymous
in reply to: Anonymous

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
Message 3 of 13
Anonymous
in reply to: Anonymous

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
Message 4 of 13
Anonymous
in reply to: Anonymous

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
Message 5 of 13
Anonymous
in reply to: Anonymous

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
Message 6 of 13
Anonymous
in reply to: Anonymous

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
Message 7 of 13
Anonymous
in reply to: Anonymous

"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
Message 8 of 13
Anonymous
in reply to: Anonymous

"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
Message 9 of 13
dubinskyv
in reply to: Anonymous

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.
Message 10 of 13
Anonymous
in reply to: Anonymous

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.
Message 11 of 13
Anonymous
in reply to: Anonymous

Dean Saadallah.

"Tony Tanzillo" wrote in message
news:5034803@discussion.autodesk.com...
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
Message 12 of 13
dubinskyv
in reply to: Anonymous

Imagine an application that is used to monitor something. This something is drawn in ACAD, so ACAD functions as a user interface.

When this application is used for monitoring the user simply starts it and then just sits there looking at this thing that's drawn in ACAD. At some point some external event may happen that will be detected and must be shown (alert) to the user as either a dialog or some attribute of the drawing, but the point is that the user has no idea when the event will happen or even if the event will happen and as a monitoring app the user should not have to do anything beyond starting the app. Hope this explains it somewhat. THanks.
Message 13 of 13
Anonymous
in reply to: Anonymous

What about having your app do a SendStringToExecute at startup and just do a regen...without testing this myself I assuming this would mimic the user actually doing something unless the event knows the call is coming from your app.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost