C# userform in excel pending while user method running - c#

I am a newbie to excel programing, I now have a problem with my excel add-ins code. My add-ins call out a Mainform. Whenever I call my method from this form's button to do some tasks, such as searching huge data, the Mainform is frozen, it's inevitable. But when the method is running, assume it then will call a childform, this child form is also pending, I cannot do anything with it.
My purpose is to call a child form to add in a progress bar and an Abort button to cancel the running method.
How can I solve this?
Thanks in advance for your help.
vuhi

I believe you need to multi-thread to allow that; multi-threading using just VBA cannot be done.
See the answer to this question for more details.

Related

WinForms MdiChildren Selection and Activation

I am a fairly experienced WinForms developer. I have an MdiApplication that used to work well. However, recently the main shell of the application, for which we use ComponentOne RibbonForm, has been updated in a big way. This update did affect some of our other 3rd party components, which we established was due to ComponentOne's use of DoEvents() in their event code. I thought I had cleaned up all of the code causing problems but I now have found another...
When I have multiple MdiChildren open and select one of these in code from an button click event on the ribbon form via
document.Activate();
document.EditorControl.Select();
document.EditorControl.Focus();
the other open MdiChildren documents still have focus, that it the forms are highlighted and input is not set on the document I set in code. Two questions:
How can I ensure that the Form I want to make active is the only one that is active?
Linking to the above; setting one form as active using form.Activate() should deactivate the others MdiChildren, but it is not - how can I deactivate the other windows in code?
Thanks for your time.
[Too long for a comment]
I am sick to the hind teeth with fighting C1. Esp. the Ribbon. I have confirmed with their support that they do use DoEvents() which they use to yield on their Gui threads. I am now going to switch to DevExpress which should be straight forward for my MVC application...
C1's use of DoEvents() messes up the normal flow of your application. DoEvents() is asynchronous which means it terminates before the application has actually processed any outstanding events, so if you're using it in a procedure with many sequential statements, calling DoEvents() causes a huge disturbance whenever it's called. This is what I think we are seeing when we perform our MDI operations, but we can never be sure without the C1 source code.
I hope this helps.

Show form and fetch data on background meanwhile

I'm working on a winforms application that works with database a lot, and 90% of my forms need to load some initial data from database on Form_Load.
I've been told to use threading to show the form, and then go and fetch data and fill the form with data, otherwise the customer can't see the form until the postman goes to the db and comes back with data :)
I've done some simple tasks with threads before, but this case is a bit confusing to me. I have a mainForm which is the MdiParent and other forms are MdiChild like below:
ChildForm child = new ChildForm { MdiParent = Program.mainForm };
and my mainForm (the MdiParent is static in Program.cs)
I don't know whether I should use Thread? BackgroundWorker? Other solutions?
I would be way more than happy if your kind replies could help me through this case and empower my incomplete knowledge. Thanks.
Using a BackgroundWorker is the easiest way to go. It handles the threading issues, catching exceptions and runs the work completion callback on the UI Thread.
If you are using C# 4.5, then await/asych is also a good approach.

Create form not affected by ShowDialog()

I'm currently doing a security assessment of an extremely large C# application. I have wrote a tool to help me do the assessment that also runs in C#. The entire application uses ShowDialog aka modal forms. I'm trying to come up with a way where I can still use my tool (click on it for example) while modal dialog boxes are popped up. I figure, this may require another thread or something else. If anyone has any easy tips on a way to make it so that my form doesn't get blocked by ShowDialog, that would be great.
The entire purpose in using a modal dialog is for it to block it's parent until it is dismissed.

WinForm: Loading form for InitializeComponent method

We have a Windows Form application for our fingerprint scanning for time keeping and it takes too much time for just initializing the control for the finger print scanning, around 5 to 10 minutes. So we need to create a loading form so that users will know that the application doesn't hang and they need to wait, but the cause of the long running process was inside the InitializeComponent() method from the initialization of the form.
My question is, how can I move the InitializeComponent() method to another thread so that I can show a loading form while initializing the form where the biometrics scanner control is, because it creates an error when accessing a UI control from another thread other than the main thread?
My code is in C#, Framework 2.0, build in Visual Studio 2008.
Please help, thanks in advance.
You can remove your control for the finger print scanning from the visual designer and create this control from the code using an additional background thread and writing a progress information during this process.
SO has lot of data about it:
How to update the GUI from another thread in C#?
Accessing UI in a thread
Best Way to Invoke Any Cross-Threaded Code?
you can find a lot more.
If still need help, post a comment.
You can find your InitializeCompoent(); method in yourForm.designer.cs, and from there you can modify it.

Dialog hangs when called from event

Using the CoreScanner Driver provided by Motorola, I'm trying to write a small winforms application that uses a barcode scanner. I'm able to interact with the scanner just fine, and properly register a call back for the OnBarcodeEvent(). In this callback, I have a Dialog that opens and displays a screen that the user needs to fill in. Shortly after the dialog is opened (using Show()), the program hangs. If I use ShowDialog(), the dialog works, but the dialog is blocking the OnBarcodeEvent event.
My guess to what is happening, is that since the dialog is getting created on the event thread, there is a race condition occurring when using Show(). Since Show() is non-blocking, the thread continues on after displaying the dialog and then dies out. Meanwhile my dialog just lost it's parent and locks up? Like i said... my best guess.
How can I remedy the situation? That is, How do I write my dialog so that it can be created within a thread not hang?
In the event, you should launch a different thread that will create your dialog form and show the form on it. You need to block this thread till form is visible - this is possible either by ShowDialog or alternately use one of Application.Run overload.
Yet another option would be to show the form on the UI thread (i.e. main application thread) - to do that, you need to call Invoke method on your main form from the event code. The invoke call should take the delegate that will show your dialog form non modally.
I have some experience with Motorola/Symbol Handheld-Devices (MC9090) and I guess the SDK will be similar.
It's hard to say without seeing the code, but my guesses:
your common not-UI thread problem - so make sure you open your dialog using UI thread
the native Motorola-driver is crashing - no kidding, this happened a lot to me - in my case (compact-framework on a WinCE device) this would not freeze the program but the scanner will not work/receive any messages before restarting the device
In order to fix this you should seperate the showing/handling of user-dialog away from the event and make sure you call this on the UI-thread (Control.InvokeRequired / Control.Invoke).

Categories