pkario
pkario

Reputation: 2220

DoEvents In a DLL

Do you have any ideas how to call DoEvents from a C# DLL

Upvotes: 1

Views: 4645

Answers (4)

Marc Gravell
Marc Gravell

Reputation: 1062650

I'd echo the "don't" (either from a dll or a UI project). There are several things you can do to make library code play nicely with a UI,including the same threading tricks (with events and/or callbacks) that you might use from the UI. The simplest approach is for the library code to simply execute "as is", and if the UI happens to spawn it on a worker thread, then it is the UI's job to handle any events and marshal them (Control.Invoke/BeginInvoke) to the UI thread for UI updates.

For more complex scenarios, you can use the sync-context (SynchronizationContext.Current) to post messages to the UI thread (this is actually how Control.Invoke etc work, but it is implementation independent - so a WPF sync-context can go through the WPF dispatcher).

Can you add more context on what the scenario is? There are many things that can be done...

Upvotes: 2

RS Conley
RS Conley

Reputation: 7196

Write a interface for the EXE and have your main form or main class implement it. Then register that object implementing the interface with the DLL. Assign it to a variable of the that interface type Make a Subroutine that is visible throughout the DLL. In the Subroutine check to see if the variable is nothing if isn't then the subroutine that fires method you created to fire DoEvents. Anytime you need to do a DoEvents then call the Subroutine.

If you are using a three tier organization for your application put the subroutine or variable on the object representing your entire application. Have the form register itself with the Application object.

If you have re-entrancy problems you can add status variable and other helper functions to safely check what the application is doing. Note that is far far easier to implements this if you are using some type of multi-tier design.

To other looking at this re-entrancy is a problem however so is a non-responsive UI. In a complex application there are circumstances where you have to let the event loop fire.

Upvotes: 0

configurator
configurator

Reputation: 41620

Do you mean System.Windows.Forms.Application.DoEvents()?

Upvotes: 3

Shog9
Shog9

Reputation: 159610

Don't. It's sketchy enough when you are the app controlling the event loop; pumping messages from a DLL just adds to the risk that you'll end up hitting code a naive programmer didn't make safe for re-entrancy.

Upvotes: 15

Related Questions