Reputation: 309
I am having trouble catching the double click event in a .net treeview. In the click event, i have a little time consuming logic (i.e. a database call to update a section of the UI). so because of the time delay here, my 2nd click in the case of a double click is ignored, therefore, the normal behavior expected from a treeview (i.e. expand on double click) does not work. My click event is as follow.
private void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//database call
}
}
The database call prevents the double click being fired when the system double click time is set to the fastest. I tried to implement a thread within, but my application prevents me from using any other thread within it. therefore, pls suggest a way in which i can fire double click event (or to get the nodes to expand on double click) without using threads :(
Thank you in advance :)
Upvotes: 1
Views: 246
Reputation: 73442
Don't do any work in main thread which take more than few milliseconds, It is already having so many work to do. let it do those please.
You can make use of TPL here..
var uiContext = TaskScheduler.FromCurrentSynchronizationContext();
var task = Task.Factory.StartNew(() =>
{
//Your database call here, which will be run in threadpool thread
return resultFromDatabase;
}, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
task.ContinueWith(antecedent =>
{
var resultFromDatabase = antecedent.Result;//This runs in main thread
//Update UI
}, CancellationToken.None, TaskContinuationOptions.None, uiContext);
StartNew provides many overloads, to know why I chosen a overload with these many number of parameters you can read it here, same applies for ContinueWith too.
Upvotes: 2
Reputation: 52185
I doubt that you can go around this without having another thread to do the heavy lifting. What is happening in your case is that the DB call, which can be resource intensive, is happening in your event handler, which is being executed by the Event Dispatcher Thread, which is the code which takes care of the GUI (including response to events other than just rendering).
The ideal solution to this would be to use a Background Worker and let it do the heavy lifting. If you need to do some GUI operations, then, you could take a look at this previous SO thread for more information.
That being said, if you really can't use threads, what you could do would be to disable your control before your DB operation and re-enable it afterwards. This should disallow the user from sending you new events through that control, that being said however, the application will most likely become unresponsive for some time.
Upvotes: 0
Reputation: 23793
Well, ideally the database call should be made in another thread, then. You should consider refactoring your application to make that possible.
A possible workaround would be to handle a right button click (or a middle button click) in treeView_NodeMouseClick
, so that treeView_NodeMouseDoubleClick
is still called for a left button double click.
Upvotes: 0