C# Multi Threading Session 4 (Exception handling)

preview_player
Показать описание
Exception handling in multi threaded program
Рекомендации по теме
Комментарии
Автор

Thank you so much Santosh Sir.
since long time, i have been looking for such series on multithreading in youtube.
You made it for all of us.

tanveerkit
Автор

Is this a typical best practice pattern to handle thread exceptions from calling threads (ie marshaling / storing an exception in a field and return it if present)? btw thanks for your videos series

amnesia
Автор

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CreateThread
{
class Program
{
static void Main(string[] args)
{
try
{
Thread th = new Thread(PerformWork);
th.Start();
}
catch (Exception ex)
{
Console.WriteLine("caught exception in calling thread created with Thread " + ex.Message);
}
try
{
MyThread thread = new MyThread();
thread.Start();

}
catch (Exception ex)
{
Console.WriteLine("caught exception in calling thread created with MyThread " + ex.Message);
}
try
{
Task<string> t = new
t.Start();
Console.WriteLine(t.Result);
Thread.Sleep(1000);
}
catch (Exception ex)
{
Console.WriteLine("caught exception in calling thread created with Task " + ex.Message);
Console.WriteLine("inner exception " + ex.InnerException.Message);
}
try
{
Func<string> func = PerformWorkReturn;
IAsyncResult res = func.BeginInvoke(null, null);
+ " received asynchronously");
Thread.Sleep(1000);
}
catch (Exception ex)
{
Console.WriteLine("caught exception in calling thread created with Func " + ex.Message);
}
try
{
Func<string> func = PerformWorkReturn;
func.BeginInvoke(Done, func);
Thread.Sleep(1000);
}
catch (Exception ex)
{
Console.WriteLine("caught exception in calling thread created with Func with callback " + ex.Message);
}
 
Console.ReadLine();
}
static void PerformWork()
{
try
{
int i = 0;
int j = 0;
int k = i / j;
}
catch (Exception ex)
{
Console.WriteLine("exception caught in thread in which it occurred " + ex.Message);
}
}
class MyThread
{
string result;
Exception exreturn = null;
Thread thread;
public string Result
{
get
{
thread.Join();
if (exreturn != null)
throw exreturn;
return result;
}
}
public void Start()
{
thread = new Thread(this.PerformWork);
thread.Start();
}
public void PerformWork()
{
try
{
int i = 0;
int j = 0;
int k = i / j;
}
catch (Exception ex)
{
Console.WriteLine("exception caught in thread in which it occurred " + ex.Message);
exreturn = ex;
return;
}
result = "Success result from instance method on thread";
}
}
static string PerformWorkReturn()
{
try
{
int i = 0;
int j = 0;
int k = i / j;
}
catch (Exception ex)
{
Console.WriteLine("exception caught in thread in which it occurred " + ex.Message);
throw ex;
}
return "success";
}
static void Done(IAsyncResult res)
{
try
{
Func<string> func = (Func<string>)res.AsyncState;
+ " printed by cllback");
}
catch (Exception ex)
{
Console.WriteLine("caught exception in callback " + ex.Message);
}
}
}
}

santoshKoolkarni
Автор

hello please help me Threading concept.

In class library file in side class file

Private static Thread StaticThread=null;

after complete the all the conditions.
Thread star;
it will assign the value to staticThread ;

i want the staticThread value in website in another page i am getting null value.


Same condition i will maintain the web service in StaticThread value web reference on the that service on my website on the time getting that thread value.

satyanarayana