|
Greetings All, I have a problem. I am doing an operation (importing excel data) using the BackgroundWorker in C#. During the OnDoWork() execution I need to ask the user for some values to enter (actually I am performing a spell check which pops up a spell check form for the user to correct poorly spelt values). In the mean time a progress dialog is displayed for the operations status. While I am aware it is not easy to show a modal dialog on a separate thread, how can I approach this scenario given that sometimes in the middle of the operation, the spell check form appears but is not activated (it is behind the application main form). I have tried to set the TopMost property but when a user Clicks on a button (say Options) on the spell check form, the Options form stays behind it which forces the user to move them to visibility. So, what is the best way to approach thios problem? Any help is very welcome. TIA. bughatta.
- Moved byOmegaManMVPFriday, June 26, 2009 1:09 PM (From:Visual C# General)
-
|
| bughata Thursday, June 25, 2009 4:26 PM |
This should be correct, modify it as you need:
delegate string InvokeSpellCheckDialogDelegate(string misspelled);
public string InvokeSpellCheckDialog(string misspelled)
{
if (InvokeRequired)
return (string)this.Invoke(new InvokeSpellCheckDialogDelegate(InvokeSpellCheckDialog), misspelled);
else
{
var form = new MySpellCheckerQuestionForm();
form.MisspelledWord = misspelled;
if (form.ShowDialog() == DialogResult.OK)
return form.CorrectedSpelling;
return null;
}
}
- Marked As Answer bybughata Friday, June 26, 2009 2:40 PM
- Unmarked As Answer bybughata Friday, June 26, 2009 2:43 PM
- Marked As Answer byAland LiMSFT, ModeratorMonday, June 29, 2009 12:03 PM
-
|
| Tergiver Friday, June 26, 2009 1:37 PM |
You need to put that method on your main UI form. That's the whole point. To invoke the dialog on the UI thread. - Marked As Answer byAland LiMSFT, ModeratorMonday, June 29, 2009 12:03 PM
-
|
| Tergiver Friday, June 26, 2009 3:33 PM |
Don't show a dialog on a separate thread. You can show the dialog inside of the ReportProgress event handler.
Post code for a better answer. No one knows how you are showing the dialog except you. TopMost is treating the sympton, but ignoring the problem. Stop showing a dialog on a separate thread.
Mark the best replies as answers. "Fooling computers since 1971." |
| Rudedog2 Thursday, June 25, 2009 4:42 PM |
I am performing a spell check which pops up a spell check form for the user to correct poorly spelt values
Are you sure you're qualified for this task? ;) Kidding aside, you need to perform all UI tasks on the UI thread. That includes launching a modal dialog. I would simply invoke a method the UI thread that launches the dialog and returns you the result. |
| Tergiver Thursday, June 25, 2009 5:15 PM |
Nice observation Tergiver ;-). I think that is a good approach. Actually the call to spell check is already in a method. So, do you have some code please? Thanks. bughata |
| bughata Friday, June 26, 2009 6:30 AM |
For a sample of the BackgroundWorker, please visit the following link: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspxFor a sample of the Application.DoEvents method, visit the following link: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.doevents.aspx
Mark the best replies as answers. "Fooling computers since 1971." |
| Rudedog2 Friday, June 26, 2009 12:49 PM |
Please disregard Rudedog's well meaning, but inappropriate reference to Application.DoEvents. If you feel you need to call Application.DoEvents, there's something wrong with what you're doing. Never call this method!
Add something like this to your form. This can be called from the BackgroundWorker thread. It is syncronous, so it can return a value directly (I assume you need the worker to pause during this).
*edited : removed the code temporarily as I just realized that it's incorrect.. - Edited byTergiver Friday, June 26, 2009 1:18 PM
-
|
| Tergiver Friday, June 26, 2009 1:15 PM |
This should be correct, modify it as you need:
delegate string InvokeSpellCheckDialogDelegate(string misspelled);
public string InvokeSpellCheckDialog(string misspelled)
{
if (InvokeRequired)
return (string)this.Invoke(new InvokeSpellCheckDialogDelegate(InvokeSpellCheckDialog), misspelled);
else
{
var form = new MySpellCheckerQuestionForm();
form.MisspelledWord = misspelled;
if (form.ShowDialog() == DialogResult.OK)
return form.CorrectedSpelling;
return null;
}
}
- Marked As Answer bybughata Friday, June 26, 2009 2:40 PM
- Unmarked As Answer bybughata Friday, June 26, 2009 2:43 PM
- Marked As Answer byAland LiMSFT, ModeratorMonday, June 29, 2009 12:03 PM
-
|
| Tergiver Friday, June 26, 2009 1:37 PM |
Hi, Thanks Tergiver, your solution looks like the right one and it can actually work. The actual scenario is the BackgroundWorker is declared in a component that does not implement ISynchronizeInvoke. I.e. Component contains BackgroundWorker which calls SpellChecker. So the InvokeRequired is missing. Is there another work around? Cheers, bughata.
|
| bughata Friday, June 26, 2009 3:11 PM |
You need to put that method on your main UI form. That's the whole point. To invoke the dialog on the UI thread. - Marked As Answer byAland LiMSFT, ModeratorMonday, June 29, 2009 12:03 PM
-
|
| Tergiver Friday, June 26, 2009 3:33 PM |