Cancel button wait for BackgroundWorker to finish (ASP.NET C#) - c#

I'm working with C# ASP.NET Web Application. There is a problem that is difficult to track in the code.
Let's say I have two buttons: Upload (which uploads some data from DataTable to Database) and Cancel (which cancels the uploading process). The problem is that uploading process runs in the BackgroundWorker. When user clicks Upload and uploading starts and then he clicks Cancel the Cancel_Button_Click event does not fire until BackgroundWorker is uploading data.
I have code like this:
protected void Page_Load(object sender, EventArgs e)
{
backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
}
protected void Upload_Button_Click(object sender, EventArgs e)
{ backgroundWorker1.RunWorkerAsync(); }
protected void Cancel_Button_Click(object sender, EventArgs e)
{ backgroundWorker1.CancelAsync(); }
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{ };
So what really happens is when you click Upload and Cancel is method private void backgroundWorker1_ProgressChanged runs many times (one time for every row of data, I suppose) and ONLY THEN method Cancel_Button_Click runs.
What to do, if I want to stop data uploading right after Cancel button is clicked?
Thaaaanks! :)
P.S. I tried searching on the net but I don't even know how to describe my problem in one sentence.

Related

What event is for when app is already open?

I have a Page_Load event which contains the code I want executing every time the user opens the application. However, when a user clicks the back button on their Windows device, the application is still open, so when they go onto the application the Page_Load event is not called.
I've also tried an OnNavigatedTo event:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
InitialStartOfApp();
}
But the InitialStartOfApp() doesn't get called. I know it doesn't get called because I try debugging the line, however it isn't executed.
Does anyone know any events that would resolve this or any ideas?
The Page_Load event is this:
private void Page_Loaded(object sender, RoutedEventArgs e)
{
InitialStartOfApp();
}
You need handle the Application.Resuming event that is raised when the apps continues after a previous suspension.
Application app = Application.Current;
app.Resuming += App_Resuming;
...
private void App_Resuming(object sender, object e) {
InitialStartOfApp();
}

Start process and Stop process buttons

I spent many days in this problem but have not found a solution.
I have a button that the user uses to starts a process that can take several minutes. This process does some calculations and at the end I have to see the end result in a textbox.
There must be another button to stop the process. If the user stops the process before the process is finished, I have to see the partial result in the textbox.
How can I do?
I tried to use a timer and an updatepanel but I could not find a solution.
protected void buttonStart_Click(object sender, EventArgs e)
{
for (j=0;j<1000;j++) //it lasts some minutes...
{
textbox1.text = j //it's only an example to understand what I need...
if (stopped==true) return;
}
}
protected void buttonStop_Click(object sender, EventArgs e)
{
stopped=true;
}

Timer to refresh charts in c#

I have created a chart which receives its data from a SQL Server database. Whenever I save new data into the database, all the reports get updated, but the chart does not update until I exit the application and log in again. I decided to use Timer in C# to automatically refresh the chart every 5 seconds.
//this is to invoke the timer as soon as the application launches.
public MDIParent2()
{
InitializeComponent();
myTimer.Enabled = true;
myTimer.Start();
}
//this is my timer event
private void myTimer_Tick(object sender, EventArgs e)
{
this.Refresh();
MessageBox.Show("Refreshed!");//this was added to determine if the timer is working
}
//this lets me stop the timer
private void button1_Click(object sender, EventArgs e)
{
myTimer.Stop();
}
//this lets me resume the timer
private void button2_Click(object sender, EventArgs e)
{
myTimer.Start();
}
I get the message "Refreshed!" every 5 seconds, but chart is still not refreshed. Can someone please assist?
replace this.refresh() with [Chart Object].refresh()

Process.Start to open multiple web pages

I am currently using a simple button to open a webpage.
void ReportingClick(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.google.ca");
}
What I want to do is get it to open 3 pages at once with the one click and I am having a hard time getting it to work. I have tried multiple Process.start lines
void ReportingClick(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.google.ca");
System.Diagnostics.Process.Start("http://www.gmail.com");
System.Diagnostics.Process.Start("http://www.stackoverflow.com");
}
and even adding multiple pages into the handler.
void ReportingClick(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.google.ca","http://www.gmail.com","http://www.s tackoverflow.com")
}
It will only open the last page in the list in both cases. Any ideas?
If IE is open, your code works fine and opens each link in a new tab, if not, I was able to make it work by making the app wait for 1 sec before calling the second page to open:
void ReportingClick(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.google.ca");
System.Threading.Thread.Sleep(1000);
System.Diagnostics.Process.Start("http://www.gmail.com");
System.Threading.Thread.Sleep(1000);
System.Diagnostics.Process.Start("http://www.stackoverflow.com");
}

ShowDialog, PropertyGrid and Timer problem

I have a strange bug, please, let me know if you have any clues about the reason.
I have a Timer (System.Windows.Forms.Timer) on my main form, which fires some updates, which also eventually update the main form UI. Then I have an editor, which is opened from the main form using the ShowDialog() method. On this editor I have a PropertyGrid (System.Windows.Forms.PropertyGrid).
I am unable to reproduce it everytime, but pretty often, when I use dropdowns on that property grid in editor it gets stuck, that is OK/Cancel buttons don't close the form, property grid becomes not usable, Close button in the form header doesn't work.
There are no exceptions in the background, and if I break the process I see that the app is doing some calculations related to the updates I mentioned in the beginning.
What can you recommend? Any ideas are welcome.
What's happening is that the thread timer's Tick method doesn't execute on a different thread, so it's locking everything else until it's done. I made a test winforms app that had a timer and 2 buttons on it whose events did this:
private void timer1_Tick(object sender, EventArgs e)
{
Thread.Sleep(6000);
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
frmShow show = new frmShow();
show.ShowDialog(); // frmShow just has some controls on it to fiddle with
}
and indeed it blocked as you described. The following solved it:
private void timer1_Tick(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(DoStuff);
}
private void DoStuff(object something)
{
Thread.Sleep(6000);
}

Categories