2

I have a CAD plugin application. In my CAD application program, CAD may have restrictions about which thread can be used to modify the CAD body. Here is my requirements:

  • Since COM call are received on different threads, then the plug-in must ensure that all construction calls are made in the right thread (the main one).
  • Also, since the kernel can call the plug-in while the CAD is busy, the plug-in must ensure that it can safely call the constuction thread without creating a deadlock.

A valid solution could be to check if the Main (construction) thread is busy before trying to use it.

So my question is: How to check If main thread is busy or not?

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Ganesh Chavan
  • 57
  • 1
  • 1
  • 6
  • 1
    if main thread is busy then anyway you will be freezed – Rahul Oct 10 '17 at 10:03
  • 1
    What threading model do you support. Usually for such application an STA is used and this threading model accepts calls only on one thread. – xMRi Oct 10 '17 at 10:06
  • @Rahul How to check programmatically If main thread is busy or not? – Ganesh Chavan Oct 10 '17 at 10:08
  • This is the other way around. You don't have to check anything, you should use the proper SynchronizationContext (depends on your app, Winforms, WPF, no UI, etc. and your code), that should do the work for you: https://stackoverflow.com/questions/18097471/what-does-synchronizationcontext-do – Simon Mourier Oct 10 '17 at 10:16
  • How I ensure that the main thread is available before invoking any method on it – Ganesh Chavan Oct 10 '17 at 10:38
  • Again. What threading model do you have. In an STA you can execute anything from extern, if the thread doesn't execute PeekMessage or GetMessage. Again: Give us more information! – xMRi Oct 10 '17 at 11:08
  • threading model is Apartment Threaded – Ganesh Chavan Oct 10 '17 at 15:16
  • ◾A valid solution could be a timeout on the call to another thread. ◾Another one would be to check if the construciton thread is busy before trying to use it. ◾A combination of both solutions could be even better.Can any one tell how to check is main thread is busy or not? – Ganesh Chavan Oct 26 '17 at 10:43

1 Answers1

0

I had a similar issue and solved it in the following way:

ManualResetEvent waiter = new ManualResetEvent(false);
control.BeginInvoke((Action)(() => waiter.Set()));
bool threadIdle = waiter.WaitOne(timeout);

Set timeout to what ever wait-time you want (500ms for instance)

Romout
  • 188
  • 2
  • 8