Reputation: 282
I have an application with some tooltipped buttons. One of these buttons (henceforth known as the Run button) starts firing processes outside of my application when it is pressed. Because my application needs to get data from output taken from these processes, I am having it pause until all the processes have finished running, then grab the data without further user intervention. Of course, this means my application is hung while the external processes are running, but it's no big deal because the user can click away and work on other stuff outside my application; my application falls to the background. Unfortunately, the tooltip associated with the Run button does not fall to the background, and it does not go away until my code stops executing.
The following code has helped slightly:
private void RunButton_Click(object sender, RoutedEventArgs e)
{
/*Gets the tooltip out of the way while running, otherwise it will stay in front of everything until the optimizers finish.*/
ToolTipService.SetShowDuration(runButton, 0);
RunTooltip.IsOpen = false;//Doesn't close fast enough. It fades a little then hangs there until the code stop running. googling has been remarkably unhelpful.
Run();
ToolTipService.SetShowDuration(runButton, 5000);
}
RunTooltip is the name of the ToolTip in question. Setting the duration to 0 or setting isOpen to false have the same behavior: cause the tooltip to become translucent (but still above every other window no matter what) until Run() finishes.
Its seems there was a way to make tooltip fading happen instantly with Windows.Forms.Tooltips (see http://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.usefading.aspx) but that no longer seems to be the case with the WPF tooltip (http://msdn.microsoft.com/en-us/library/ms617634.aspx)
How do I keep this tooltip from getting in the way of whatever the user wants to do while waiting for my application to finish, apart from removing this button's tooltip entirely? Thanks
Upvotes: 0
Views: 1255
Reputation: 28708
You could potentially change the Visibility of the ToolTip's Popup component in your OnClicked event, but you'd have to use the VisualTreeHelper to find the ToolTip and it's all a bit messy. The UI will still be frozen and it's not the best way to do this.
Here's a rule of thumb: if you are freezing the UI, you are doing it wrong.
You probably want a busy indicator to wrap your controls (there's a great one in the WPF Toolkit on codeplex). WPF works best (i.e. cleanest) using the MVVM pattern, but if you're working directly in the code-behind, something like this:
void MyButton_Click(object sender, EventArgs e)
{
var backgroundThread = new Thread(DoWork);
this.MyBusyIndicator.IsBusy = true;
backgroundThread.Start();
}
And then after the work is done, set the BusyIndicator to no longer be Busy.
Upvotes: 0
Reputation: 45096
Can use a BackgroundWorker and then you load the UI on the CallBack.
If you want to report progress link process X of Y can user ReportsProgress. And you can cancel.
Upvotes: 1
Reputation: 494
Correct me if I'm wrong, because I might be misunderstanding you, but it sounds like you're doing some sort of computationally expensive stuff in the thread that's handling the UI. A simpler solution to this problem might be to create a worker thread like so:
private void button1_Click(object sender, EventArgs e)
{
Thread workerThread = new Thread(ExpensiveMethod);
btnShow.Visible = false;
workerThread.Start();
}
private void ExpensiveMethod()
{
for (int i = 0; i < int.MaxValue; i++)
{
}
btnShow.Visible = true;
}
This means that the thread handling the UI (and thus the tooltip) can proceed as normal while the computationally expensive stuff is being handled by the worker thread.
Let me know if I've misunderstood you!
Upvotes: 0