Asked by:
Parallel tasks and multiple Task.Run() throwing exception after first run

Question
-
Hello everyone,
as the title suggests im trying to multitask and also use parallel tasks, however its throwing me this unhandled exception after the first time :
#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION UnhandledException += (sender, e) => { if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break(); }; #endif
from the app.g.i.cs file..
the first time i click the button in order to run the tasks it completes successfully but when i click the button the second time after it finishes the tasks then it throws me that error..this is just a sample app that im using for testing purposes, i remember that before i upgraded to Windows 8.1 the app was working perfectly, does Windows 8.1 restricts the cpu usage or something like that? another detail is that when i first built this app i was running it in another machine which i havent tested it again using Windows 8.1 but this machine where i am receiving this exception is a first generation corei5...not sure if that might be the root cause of the problem..
anyways that said i hope someone can help me out with this and by the way here it is the code :public sealed partial class GraphWindow : Page { // Reduce pixelWidth and pixelHeight if there is insufficient memory available private int pixelWidth = 12000; private int pixelHeight = 8000; private WriteableBitmap graphBitmap = null; private int bytesPerPixel = 4; private byte[] data; private byte redValue, greenValue, blueValue; private CancellationTokenSource tokenSource = null; public GraphWindow() { this.InitializeComponent(); Window.Current.SizeChanged += App.WindowSizeChanged; int dataSize = bytesPerPixel * pixelWidth * pixelHeight; data = new byte[dataSize]; graphBitmap = new WriteableBitmap(pixelWidth, pixelHeight); } private void cancelButton_Click(object sender, RoutedEventArgs e) { if (tokenSource != null) { tokenSource.Cancel(); } } private async void plotButton_Click(object sender, RoutedEventArgs e) { Random rand = new Random(); redValue = (byte)rand.Next(0xFF); greenValue = (byte)rand.Next(0xFF); blueValue = (byte)rand.Next(0xFF); tokenSource = new CancellationTokenSource(); CancellationToken token = tokenSource.Token; Stopwatch watch = Stopwatch.StartNew(); try { await generateGraphDataAsync(data, 0, pixelWidth / 2, token); duration.Text = string.Format("Duration (ms): {0}", watch.ElapsedMilliseconds); } catch (OperationCanceledException oce) { duration.Text = oce.Message; } Stream pixelStream = graphBitmap.PixelBuffer.AsStream(); pixelStream.Seek(0, SeekOrigin.Begin); pixelStream.Write(data, 0, data.Length); graphBitmap.Invalidate(); graphImage.Source = graphBitmap; } private async Task generateGraphDataAsync(byte[] data, int partitionStart, int partitionEnd, CancellationToken token) { //Task task = Task.Run(() => generateGraphData(data, partitionStart, partitionEnd, token)); Task first = Task.Run(() => generateGraphData(data, 0, pixelWidth / 4, token), token); Task second = Task.Run(() => generateGraphData(data, pixelWidth / 4, pixelWidth / 2, token), token); await first; await second; } private void generateGraphData(byte[] data, int partitionStart, int partitionEnd, CancellationToken token) { int a = pixelWidth / 2; int b = a * a; int c = pixelHeight / 2; for (int x = partitionStart; x < partitionEnd; x++) { int s = x * x; double p = Math.Sqrt(b - s); for (double i = -p; i < p; i += 3) { token.ThrowIfCancellationRequested(); double r = Math.Sqrt(s + i * i) / a; double q = (r - 1) * Math.Sin(24 * r); double y = i / 3 + (q * c); plotXY(data, (int)(-x + (pixelWidth / 2)), (int)(y + (pixelHeight / 2))); plotXY(data, (int)(x + (pixelWidth / 2)), (int)(y + (pixelHeight / 2))); } } } private void plotXY(byte[] data, int x, int y) { int pixelIndex = (x + y * pixelWidth) * bytesPerPixel; data[pixelIndex] = blueValue; data[pixelIndex + 1] = greenValue; data[pixelIndex + 2] = redValue; data[pixelIndex + 3] = 0xBF; } }
so this is not the parallel sample just the Task.Run() but both gives me the same error and the code is pretty similar..
any help is greatly appreciated!
Kind regards,
c# user
Tuesday, February 11, 2014 6:41 PM
All replies
-
I tried running your code but it's missing some referenced objects. Can you post a complete project?
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.Tuesday, February 11, 2014 9:30 PMModerator -
oh yeah sure!
here it is:
https://skydrive.live.com/redir?resid=A50FCD6614464ACF%21267
thanks in advance for any help!- Edited by MrDebugging Wednesday, February 12, 2014 1:19 AM
Wednesday, February 12, 2014 1:19 AM -
It's crashing on this line of code. I'm not sure why just yet.
graphImage.Source = graphBitmap;
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.Friday, February 14, 2014 6:14 PMModerator -
sorry for the delay..i will try to see whats wrong with that later, weird cause it used to work on my Windows 8 machine..
thanks anyways!Kind regards,
C# user
Wednesday, February 19, 2014 1:54 AM