A day with .Net

My day to day experince in .net

Archive for May, 2012

Exception handling in Task Parallel Library

Posted by vivekcek on May 31, 2012

The below code shows how to handle exception from parallel thread. In this case we handle exception from only one thread.

 var task1 = Task.Factory.StartNew(() =>;
                {
                    throw new Exception("throwing unhandled exception");

                }
                       );
                try
                {
                    task1.Wait();
                }
                catch (AggregateException ag)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (Exception ex in ag.InnerExceptions)
                    {
                        sb.AppendLine(ex.Message);
                    }
                    MessageBox.Show(sb.ToString());
                }
                finally
                {
                    task1.Dispose();
                }
Advertisements

Posted in c#.net | Tagged: , | Leave a Comment »

Cross-thread operation not valid task parallel library

Posted by vivekcek on May 31, 2012

I have 2 drop down list in a windows form application. On the selected index change event of first i want to add an item to second drop down. I just used Task Parallel Library and wrote the below code. It give me an error like “Cross-thread operation not valid: Control ‘ddlRqType’ accessed from a thread other than the thread it was created on.”

My Initial Code

 private void ddlCMType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCMType.SelectedItem.ToString() == "Portal")
            {
                Task.Factory.StartNew(() =>

                    {
                        ddlRqType.Items.Add("hai");
                    }
                    );
            }
        }


Then i corrected it using Invoke and delegate

  private void ddlCMType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCMType.SelectedItem.ToString() == "Portal")
            {
                Task.Factory.StartNew(() =>

                    {
                        this.Invoke(new MethodInvoker(delegate { ddlRqType.Items.Add("hai"); }));
                    }
                    );
            }
        }

Posted in c#.net | Tagged: , | Leave a Comment »

Datatable to XML using LINQ

Posted by vivekcek on May 29, 2012

If you want to create an XML like this below


<Root name="root" xmlns="http://www.google.com/namespace">
<Child id="1" name="vivek" />
<Child id="2" name="chanthu" />
</Root>

From the table

 

The below code will be useful


DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("name");

DataRow dr = dt.NewRow();
dr["id"] = "1";
dr["name"] = "vivek";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["id"] = "2";
dr["name"] = "chanthu";
dt.Rows.Add(dr);

XNamespace x1 = "http://www.google.com/namespace";

XElement xt = new XElement(x1 + "Root", new XAttribute("name", "root"),
from p in dt.AsEnumerable()
select new XElement(x1 + "Child", new XAttribute("id", p["id"]),
new XAttribute("name", p["name"])));

Posted in c#.net | Tagged: , , | Leave a Comment »

Convert String to Stream c#

Posted by vivekcek on May 28, 2012

string response = string.Empty;
byte[] byteArray = Encoding.UTF8.GetBytes(response); 
MemoryStream respstream = new MemoryStream(byteArray);

Posted in c#.net | Tagged: | Leave a Comment »