Avatar of Jimbo99999
Jimbo99999Flag for United States of America

asked on 

VB.Net - Cancel Long Running Process or Selection User Didn't Want

Good Day Experts!

I have a Vb.Net application that our User community uses on a daily basis.  It has a tab control on it with over 25 tabs.  They are interested in being able to cancel a long running process or a process they didn't want to start(even after the "Are you sure?" message boxes.  We would want it to apply for all the tabs so I would only have to code it once instead of on each tab.  

Is there a way to cancel the current running process for the User without knowing what was running?  

Thanks for being patient and understanding,
jimbo99999
.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
Scott McDaniel (EE MVE )
ASKER CERTIFIED SOLUTION
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Jimbo99999
Jimbo99999
Flag of United States of America image

ASKER

Thank you for your reply.   Since my project is in production already, how do I find out what the User clicked on so I can put it in the  DoWork method of that BGW? Or is this not possible?

Thanks for your help,
jimbo99999
Avatar of it_saige
it_saige
Flag of United States of America image

Better yet, if you are using .NET 4 or higher, you can use task's with a CancellationToken

Microsofts Examples -

Single Task Cancellation - https://msdn.microsoft.com/en-us/library/dd997396(v=vs.110).aspx
Task and Children Cancellation - https://msdn.microsoft.com/en-us/library/dd537607(v=vs.110).aspx

-saige-
Avatar of Jimbo99999
Jimbo99999
Flag of United States of America image

ASKER

Thanks for responding.  Unfortunately I am not at 4 or higher.
how do I find out what the User clicked on so I can put it in the  DoWork method of that BGW? Or is this not possible?
It's possible, but it's not really possible for us to determine. You'd have to first define the process you want to move to that background worker, then define when/where the user could cancel.

For example, I have a DataGridView that takes quite a while to fill (it pulls a lot of data, and perform quite a few processes on each line). I moved my "LoadData" event to the BackgroundWorker, and now when the user loads data, they can continue using the machine while the DGV is loaded in the background. If they want to cancel, I included a Cancel button, and use code like I showed earlier to cancel the process.
Avatar of Jimbo99999
Jimbo99999
Flag of United States of America image

ASKER

Hello Scott

Thank you for your reply.  That is very similar to my issue. The bulk of the problems stem from when I am exporting from the DataGridView. I have processes that retrieve additional detail data for the records in the grid.  That combined with record volume in the grid causes me headaches.  I will try one of my grid exports to excel in the background.  This is my first time using this setup.  

Thanks again,
jimbo99999
I have much the same with an export from one of my datagrids. My DGVs are based on Datatables, so when I need to export I declare the datatable at Form level and then call my background worker to perform the export. The BGW uses that form-level DataTable to export, and I report back to the user when it's finished.
Avatar of Jimbo99999
Jimbo99999
Flag of United States of America image

ASKER

Hello There Scott

I am trying to call the button click that is long running for the User.  I am getting that cross-thread operation not valid message.  I am trying to figure out the ProcessChanged you mentioned.  If I back up a bit here...Is it even possible to call the button click as the process?
If it is not possible, can I "convert" the existing code behind the button to run in the background?

Thanks,
jimbo99999
Avatar of Ark
Ark
Flag of Russian Federation image

BackgroundWorker.CancelAsync stops async thread execution but it doesn't stop 'heavy' SQL command. Use SQLCommand.Cancel method to cancel SQLCommand execution.
Is it even possible to call the button click as the process
I'm not sure, but I'd think you could not. If you have code that needs to be run from the background worker, then move that code to a function or sub, and call that function/sub.
cross-thread operation not valid
That often means you're trying to work with UI components (like a Textbox, perhaps) in the background worker. For example, you could not change the value of a Form's Label in the RunWork function.
Avatar of Jimbo99999
Jimbo99999
Flag of United States of America image

ASKER

I adjusted a couple things to not work with the UI and avoid the error.  It is working great.  The User is able to move the form and minimize it with no issues.  2 out of 3 things are done. I just have to figure out the cancel part. Poking around it looks like the Cancel is a little more tedious as I have not come to a solution yet for it.
Author seems to have adopted the suggestion solution, and I can verify that it works to move processing away from the UI thread, making the machine responsive during the long running operation
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo