First experiment calling asynch method - Do I need ALL this code? - c#

Never tried asynch calling with Windows Form. I cannot use the new aynch/await because I don't own recent Visual Studio/.NET. What I need is "execute an operation that request long time (populate an IList)" and, when it has finished, write on a TextBox the result of that list.
Searching on Internet I found this example that seems work, but TOO comply in my opinion (maybe there are somethings fast and simple):
private void button1_Click(object sender, EventArgs e)
{
MyTaskAsync();
}
private void MyTaskWorker()
{
// here I populate the list. I emulate this with a sleep of 3 seconds
Thread.Sleep(3000);
}
private delegate void MyTaskWorkerDelegate();
public void MyTaskAsync()
{
MyTaskWorkerDelegate worker = new MyTaskWorkerDelegate(MyTaskWorker);
AsyncCallback completedCallback = new AsyncCallback(MyTaskCompletedCallback);
AsyncOperation async = AsyncOperationManager.CreateOperation(null);
worker.BeginInvoke(completedCallback, async);
}
private void MyTaskCompletedCallback(IAsyncResult ar)
{
MyTaskWorkerDelegate worker = (MyTaskWorkerDelegate)((AsyncResult)ar).AsyncDelegate;
AsyncOperation async = (AsyncOperation)ar.AsyncState;
worker.EndInvoke(ar);
AsyncCompletedEventArgs completedArgs = new AsyncCompletedEventArgs(null, false, null);
async.PostOperationCompleted(delegate(object e) { OnMyTaskCompleted((AsyncCompletedEventArgs)e); }, completedArgs);
}
public event AsyncCompletedEventHandler MyTaskCompleted;
protected virtual void OnMyTaskCompleted(AsyncCompletedEventArgs e)
{
if (MyTaskCompleted != null)
MyTaskCompleted(this, e);
// here I'll populate the textbox
textBox1.Text = "... content of the Iteration on the List...";
}
really I NEED somethings like 50 lines of code for this easy operation? Or I can remove some stuff? I just need a simple asynch call->callback when finished.
No lock, no concurrency at all...

You can use the TPL with C# 4.0 like so:
private void button1_Click(object sender, EventArgs e)
{
Task.Factory.StartNew(() => DoWork())
.ContinueWith(t => UpdateUIWithResults(t.Result)
, CancellationToken.None
, TaskContinuationOptions.None
, TaskScheduler.FromCurrentSynchronizationContext());
}
This starts DoWork in a thread pool thread, allowing it to do processing out of the UI thread, then runs UpdateUIWithResults in a UI thread, passing it the results of DoWork.

You can use Task.Factory.StartNew to push work onto the thread pool. Task.ContinueWith will give you a "completed callback".
private void button1_Click(object sender, EventArgs e)
{
var ui = TaskScheduler.FromCurrentSynchronizationContext();
Task<List<T>> task = Task.Factory.StartNew(() => MyTaskWorker());
task.ContinueWith(t => OnMyTaskCompleted(t), ui);
}
private List<T> MyTaskWorker()
{
// here I populate the list. I emulate this with a sleep of 3 seconds
Thread.Sleep(3000);
return ...;
}
protected virtual void OnMyTaskCompleted(Task t)
{
// here I'll populate the textbox with t.Result
textBox1.Text = "... content of the Iteration on the List...";
}

Related

Update the ItemSource of a Datagrid from another thread

I've seen many of these types of questions asked and answered here but none of those seems to solve my problem.
I have a Page that retrieves and shows a list of data from a database. my initial code looked like this
private void HistoryPage_OnLoaded(object sender, RoutedEventArgs e)
{
//''''''
_invoices = Invoice.GetAll(); // returns a list of invoices
InvoiceList = new PagingCollection<Invoice>(_invoices, _itemsPerPage);
DgInvoices.ItemsSource = InvoiceList.CurrentItems;
//'''''''''
}
this worked ok until the data list got bigger. now it takes about 6-8 seconds for this operation.
then I tried to fetch data from a different thread and update the Datagrid ( DGInvoices ) from there.
private void HistoryPage_OnLoaded(object sender, RoutedEventArgs e)
{
//''''''''
new Thread(() =>
{
_invoices = Invoice.GetAll();
InvoiceList = new PagingCollection<Invoice>(_invoices, _itemsPerPage);
DgInvoices.ItemsSource = InvoiceList.CurrentItems;
}).Start();
}
which throws this exception
The Calling thread cannot access this object because a different thread owns it
After searching around, I found that the Dispatcher is the way to go about this. but I cannot get it to work.
private void HistoryPage_OnLoaded(object sender, RoutedEventArgs e)
{
//''''''''
new Thread(() =>
{
_invoices = Invoice.GetAll();
InvoiceList = new PagingCollection<Invoice>(_invoices, _itemsPerPage);
Dispatcher.Invoke(() =>
{
DgInvoices.ItemsSource = InvoiceList.CurrentItems;
});
}).Start();
}
this still throws the above exception.
can you recommend a way to get this working?
I personally think a BackgroundWorker would be the best option. Dispatcher may work, but it's a more "forced" operation in WPF and it can sometimes present a litany of other problems. With a BackgroundWorker you can do your data work in the background, and then do your UI work on the main thread upon its completion.
As an example:
BackgroundWorker bw = new BackgroundWorker();
public MainWindow()
{
InitializeComponent();
//Subscribe to the events
bw.DoWork += Bw_DoWork;
bw.RunWorkerCompleted += Bw_RunWorkerCompleted;
}
private void HistoryPage_OnLoaded(object sender, RoutedEventArgs e)
{
//Start background worker on page load
bw.RunWorkerAsync(); //This is the DoWork function
}
//Background worker executes on separate thread
private void Bw_DoWork(object sender, DoWorkEventArgs e)
{
//Do long running operations
_invoices = Invoice.GetAll();
}
//Fires when the DoWork operation finishes. Executes on the main UI thread
private void Bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//Update UI when the worker completes on the main thread
InvoiceList = new PagingCollection<Invoice>(_invoices, _itemsPerPage);
DgInvoices.ItemsSource = InvoiceList.CurrentItems;
}
If your operation gets really long you can even tap into the BackgrounWorker.ReportProgess operation and give status updates to the UI. It's a great tool for loading operations that you can use to avoid locking the UI.
Why are you using Dispatcher within new thread?
You can simply use Dipatcher outside of the new thread.
Like this:
Dispatcher.Invoke(() =>
{
DgInvoices.ItemsSource = InvoiceList.CurrentItems;
});
So you can invoke on the main thread and not on the new thread
Don't update DgInvoices.ItemsSource directly inside the thread.
Instead bind ItemSource to a property and update the property in the thread.
Upon to your last edit, to get this working you must move
InvoiceList = new PagingCollection<Invoice>(_invoices, _itemsPerPage) to the Dispatcher too because its child CurrentItems is assigned to DitaGrid's items source. Thus, you cannot modify InvoiceList from other than main UI thread.
Additionally I suggest using Task instead of Thread because creating thread is too expensive operation and Task may reuse already created threads and save your time and PCs resources. Task is a smart wrapper of the Thread.
private void HistoryPage_OnLoaded(object sender, RoutedEventArgs e)
{
//''''''''
Task.Run(() =>
{
_invoices = Invoice.GetAll();
Dispatcher.Invoke(() =>
{
InvoiceList = new PagingCollection<Invoice>(_invoices, _itemsPerPage);
DgInvoices.ItemsSource = InvoiceList.CurrentItems;
});
});
}
Or in case your API have async method to get the data you may use asynchronuous approach. But i dont's know if such awaitable method exists.
private async void HistoryPage_OnLoaded(object sender, RoutedEventArgs e)
{
//''''''''
await _invoices = Invoice.GetAllAsync();
InvoiceList = new PagingCollection<Invoice>(_invoices, _itemsPerPage);
DgInvoices.ItemsSource = InvoiceList.CurrentItems;
}

wpf c# background worker works when execution finished

I have a wpf application where I want to start a loading indicator before a task and end after task done. But the indicator starts after the task executes.
What I am trying is as follows.
private void RunAllScriptsChildwdwBtnOK_Click(object sender, RoutedEventArgs e)
{
worker.RunWorkerAsync(); // this supposed to start progress bar
_RunAllScripts_Click();
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
this.Dispatcher.Invoke(() =>
{
... Start loading indicator
});
}
private void worker_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
... End loading indicator
}
But loading indicator starts and ends (as supposed in worker events) only after
_RunAllScripts_Click(); method execution is complete.
(I found that after unsubscribing from worker_RunWorkerCompleted event, progress bar starts and stays as is because no code to end it).
Also I want to add that, breakpoint hits worker_DoWork method before the execution, but UI updates after execution as I indicated above.
Thanks for all help you will be able to provide.
If i was you i would use the async + await keyword for this
private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
// this is where you would enable your indicator
Button.IsEnabled = false;
await Task.Run(
() =>
{
// this is where you put your work, which should be executed in the background thread.
Thread.Sleep(2000);
});
// this is where you would disable it
Button.IsEnabled = true;
}
Using async/await will work. The await keyword will allow you to run work without affecting/blocking the UI thread (allowing message pumping to still occur). Once the work has finished, any code after the await keyword will execute.
Note that I have also wrapped the await work in an InvokeAsync call, as it appears that additional work you are doing required UI thread access.
private async void RunAllScriptsChildwdwBtnOK_Click(object sender, RoutedEventArgs e)
{
//TODO ... Start loading indicator
await Task.Run(async ()=>
{
await Application.Current.Dispatcher.InvokeAsync(()=>
{
_RunAllScripts_Click();
});
});
//TODO ... End loading indicator
}
Dear kind people helping me about this subject, thank you all.
This works for me, hope it works for all.
BackgroundWorker bwTestAll = new BackgroundWorker() { WorkerReportsProgress = true };
bwTestAll.DoWork += new DoWorkEventHandler(TestAll);
bwTestAll.RunWorkerCompleted += TestAll_RunWorkerCompleted;
//this is where I initialize my loading ring and other stuff and marshall background
//worker to do the main work
Dispatcher.Invoke(new Action(() =>
{
EnableLoading = true;
RunAllScriptsTest.IsEnabled = false;
}), DispatcherPriority.ContextIdle);
bwTestAll.RunWorkerAsync();
//this is my main work
void TestAll(object sender, DoWorkEventArgs e)
{
presenter.RunAllScripts(true);
}
//this is where I do my post-work stuff
private void TestAll_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
Dispatcher.Invoke(new Action(() =>
{
/
EnableLoading = false;
RunAllScriptsTest.IsEnabled = true;
DbExecGrid = this.ExecutionResults;
ShowOrHideExecGrid(this.EnableOrDisableGrid);
}), DispatcherPriority.ContextIdle);
}
*Please Notice that Dispatcher with "DispatcherPriority.ContextIdle" works for me.

Implementing queued tasks that start based on conditions

I am looking for the ideal/thread safe implementation of a task queue that will start each task based on an internal condition (which i also only want to be checked at a timed interval), but also check that there aren't too many other tasks running. I also want a way to return progress of each task in the queue.
To give some background, I have a FileSystemWatcher that sends an event on a new folder being made. I then want to put this into a queue to process the folder, when it is no longer locked. But at the same time I want to guard against too many instances of the Process task from running.
Heres some pseudo-code of what I am after.
private static void EventToQueue(object sender, EventArgs e)
{
ProcessQueue.Enqueue(Task, condition);
}
private static async void NewQueueObject(object sender, QueueObject e)
{
if (e.Condition && ProcessQueue.ActiveTask < 4)
{
var progress = new Progress<int>();
progress.ProgressChanged += ( s, e ) =>
{
UpdateProgress(e.Value);
};
await LongProcess(e, progress);
}
else
{
e.Delay(30);
}
}

Calling an async method with c#5.0

I do some tests with the new asynchronous pattern of C# 5.0 (async/await) I have a problem with understanding how the asynchronous methods are called.
Considering this code :
private async Task<string> DownloadAsync()
{
progress.ProgressChanged += (s, e) =>
{
progressBar1.Value = e.value;
};
return await DownloadSomething(myurl, progress);
}
private async void CallDownloadAsync()
{
string text = await DownloadAsync();
progressBar1.Value = 0;
label1.Text = "Done!";
}
private void button4_Click(object sender, EventArgs e)
{
CallDownloadAsync();
}
So, this code works very well. When I clic the "button4" a downloading task begins and my ProgressBar is updated correctly.
But, I'd like to compact my code a little bit more by removing CallDownloadAsync() method like this :
private void button4_Click(object sender, EventArgs e)
{
new Action(async () =>
{
string result = await Task.Run<string>(() => DownloadAsync());
}).Invoke();
label1.Text = "Running...";
}
So here, I want to directly initiate an action which calls the DownloadAsync method but when I hit my Button4 I have a Cross-thread operation not valid on the progressBar. So I do not understand what is the main difference between the Action() and the call of my CallDownloadAsync() method.
You may find my async/await intro helpful. In particular, an async method does not run on a background thread; Task.Run is used to run something on a background thread, hence the difference in your code.
In general, you should avoid async void, unless you're writing an async event handler. Like this:
private async void button4_Click(object sender, EventArgs e)
{
label1.Text = "Running...";
string result = await DownloadAsync();
progressBar1.Value = 0;
label1.Text = "Done!";
}
The difference is that in former case you call CallDownloadAsync() from UI thread (context).
In the latter case, DownloadAsync() is called from the initiated Task which is generally executed in a different thread created by TPL (Task Parallel Library) out of UI thread or threads created from it.
In WPF, UI components can be accessed only by a dedicated UI thread or (its children) threads created from under it (i.e with the same UI context).

How to avoid UI hanging with Invoke?

The following code makes UI thread hanging.
private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(Func);
t.Start();
}
private void Func()
{
this.Invoke((Action)(() =>
{
while (true);
}));
}
I'd like to have Func() invoked in a different working thread without any UI thread freezing every time I click the button.
What would be the best workaround?
With your code, while(true) is running on UI thread, that is the reason which blocks your UI.
Put while(true) out of Invoke method, so whenver you want to change UI, put the block of code changing UI inside Invoke:
private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(Func);
t.Start();
}
private void Func()
{
while(true)
{
this.Invoke((Action)(() =>
{
textBox.Text = "abc";
}));
}
}
The Func() codes does run on a non-UI thread. However, the this.Invoke then executes the Action on the UI thread!.
Try something like this:
void Func()
{
// Do some work.
// Update the UI (must be on UI thread)
this.Invoke(Action) (() =>
{
// Update the UI.
}));
}
I might be better to use the BeginInvoke method. This way the non-UI thread is not waiting around for the UI thread to do the Action.
Also, you have no Exception catching or progress reporting logic. I recommend looking at the BackgroundWorker class; http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx.
void button1_Click(object sender, EventArgs e)
{
var worker = new BackgroundWorker();
worker.DoWork += (s,e) =>
{
// Do some work.
};
worker.RunWorkerCompleted += (s,e) =>
{
// Update the UI.
}
worker.RunWorkerAsync();
}

Categories