scolty's Profile User Rating: -----

Reputation: 3 Apprentice
Group:
Active Members
Active Posts:
259 (0.34 per day)
Joined:
27-April 11
Profile Views:
4,395
Last Active:
User is offline Nov 10 2012 09:30 AM
Currently:
Offline

Previous Fields

Country:
Who Cares
OS Preference:
Who Cares
Favorite Browser:
Who Cares
Favorite Processor:
Who Cares
Favorite Gaming Platform:
Who Cares
Your Car:
Who Cares
Dream Kudos:
0
Icon   scolty has not set their status

Posts I've Made

  1. In Topic: Object.InvokeRequired issue

    Posted 9 Mar 2012

    Maybe i didnt word it very well. All the thread is doing is raising an event. In the method subscribed to the event, i check which thread is actually executing the method (im not aware of a way to raise an event and not have the same thread execute it) and if its the non-GUI one, it forces the GUI thread to execute the method.

    here is the code which raises the event. The subscribed method is in the picture above.

    protected virtual void OnUpdateProgressBar(object sender, ProgressBarEventArgs e)
    {
        EventHandler<ProgressBarEventArgs> TempHandler = UpdateProgressBar;
    
        //Avoids possible race condition.
        if (TempHandler != null)
        {
            TempHandler(this,e);
        }
    }
    
    


    Thanks for your help
  2. In Topic: Task Cancellation token

    Posted 1 Mar 2012

    ahh ok, so i should just place in a couple other conditions ie This || that and it should be sorted?

    Cheers bro
  3. In Topic: Task Cancellation token

    Posted 1 Mar 2012

    Hey Adam, i changed the finally block to that + !=null but it doesnt work. The elements of the array all show RanToCompletion instead of IsCanceled, or have i misunderstood you?

    Thanks for your help bro

    finally
                    {
                        foreach (Task t in TaskArray)
                        {
                            if (t!=null && t.IsCanceled)
                            {
                                try
                                {
                                    t.Dispose();
                                }
                                catch (OperationCanceledException checkex)
                                {
    
                                }
                            }
                        }
    
    
  4. In Topic: Thread/Task exception handling

    Posted 28 Feb 2012

    Alright, ive made a small program up and messed with it a little. If i leave the try/catch block in the taskbody() method, they catch the exception. Having them there also allows the UI thread to start executing UIThreadDelayer().

    BUT

    If i place the try catch block after the creation of the task and have tsk.Wait(); in there, the UI thread waits. Im therefore a little confused as to why i would bother catching the aggregate exception when i could just catch the exception in the code especially since i want the UI thread to be available to update the GUI and i want to use a for loop to execute several tasks.

    Im assuming since this doesnt seem to work that what im doing is another amazing example of poor programming practice??

    Thanks for your help guys



    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace Exception_handling_with_Tasks
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                //Create new task.
                Task tsk = Task.Factory.StartNew(() =>
                    {
                        TaskBody();
                    });
                try
                {
                    tsk.Wait();
                }
                catch (AggregateException ex)
                {
                    foreach (Exception einner in ex.InnerExceptions)
                    {
                        MessageBox.Show(einner.Message.ToString());
                    }
                }
                
                
                //Call a method to keep UI thread busy.
                UIThreadDelayer();
            }
    
            public void UIThreadDelayer()
            {
                for (int i = 0; i < 20; i++)
                {
                    textBox1.Text += i.ToString();
                }
            }
    
            public void TaskBody()
            {
                for(int i = 0;i<20;i++)
                {
                    //try
                    {
                        if (i == 5)
                        {
                            throw new SyntaxErrorException();
                        }
    
                        if (i == 10)
                        {
                            throw new OutOfMemoryException();
                        }
                    }
                    /*catch (SyntaxErrorException ex1)
                    {
                        MessageBox.Show(ex1.Message.ToString());
                    }
    
                    catch (OutOfMemoryException ex2)
                    {
                        MessageBox.Show(ex2.Message.ToString());
                    }*/
                }
            }
        }
    }
    
    
  5. In Topic: Thread/Task exception handling

    Posted 27 Feb 2012

    ok cool, i was under the impression i might as well have not bothered writting the "normal" exception handling code as it wouldnt be used. Glad to see i was mistaken!

    Thanks for clarifying that.

My Information

Member Title:
D.I.C Regular
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Click here to e-mail me

Comments

scolty has no profile comments yet. Why not say hello?