- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 2192
using System;
using System.Threading;
using System.Threading.Tasks;
namespace AwaitAsyncExample
{
class Program
{
static void Main(string[] args)
{
Task.Run(async () =>
{
await MultipleAwaits();
});
Console.ReadKey();
}
static async Task MultipleAwaits()
{
Task task1 = Task.Run(() =>
{
Thread.Sleep(2_000);
Console.WriteLine("Done with first task!");
});
Task task2 = Task.Run(() =>
{
Thread.Sleep(1_000);
Console.WriteLine("Done with second task!");
});
Task task3 = Task.Run(() =>
{
Thread.Sleep(1_000);
Console.WriteLine("Done with third task!");
});
await Task.WhenAll(task1, task2, task3);
}
}
}
Another example:
private static readonly Object ThisLock = new Object();
static void Main(string[] args)
{
Task.Run(async () =>
{
await MultipleAwaits();
});
Console.ReadKey();
}
static async Task MultipleAwaits()
{
Task task1 = Task.Run(() =>
{
lock (ThisLock)
{
File.AppendAllText("test.txt", "test 1");
}
});
Task task2 = Task.Run(() =>
{
lock (ThisLock)
{
File.AppendAllText("test.txt", "test 2");
}
});
Task task3 = Task.Run(() =>
{
lock (ThisLock)
{
File.AppendAllText("test.txt", "test 3");
}
});
await Task.WhenAll(task1, task2, task3);
}
Notice:
lock (ThisLock)
{
File.AppendAllText("test.txt", "test 2");
}
Without lock exception "The process cannot access the file 'test.txt' because it is being used by another process." will be raised.
One more example in Windows forms:
namespace AsyncTest;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
btnStart.Click += DoSomeWork;
}
async void DoSomeWork(object sender, EventArgs e)
{
label1.Text = "start";
string text = await DoWait();
label1.Text = text;
}
private Task<string> DoWait()
{
Task<string> task1 = Task.Run(() =>
{
Thread.Sleep(2_000);
return Task.FromResult("did it");
});
return task1;
}
}
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 3024
Create table with code like:
DataTable dtMaster = new DataTable("Master");
In this example I will add Columns without specifying type:
dtMaster.Columns.Add("Id");
Create new row:
DataRow drMaster = dtMaster.NewRow(); drMaster["Id"] = 1;Add row to DataTable:
dtMaster.Rows.Add(drMaster);Now details table:
DataTable dtDetail = new DataTable("Details");
dtDetail.Columns.Add("Id");
dtDetail.Columns.Add("FkDetailMasterId");
DataRow drDetail = dtDetail.NewRow();
drDetail["Id"] = 1;
drDetail["FkDetailMasterId"] = 1;
dtDetail.Rows.Add(drDetail);
Create DataSet and add relation:
DataSet ds = new DataSet();
ds.Tables.Add(dtMaster);
ds.Tables.Add(dtDetail);
DataRelation relMasterDetail = new DataRelation("MyRelation"
, ds.Tables["Master"].Columns["Id"]
, ds.Tables["Details"].Columns["FkDetailMasterId"]
);
ds.Relations.Add(relMasterDetail);
Example download from here.
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 2652
MethodInfoClass mic = new MethodInfoClass();
const BindingFlags bindingFlags = BindingFlags.InvokeMethod |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Static |
BindingFlags.Instance;
System.Reflection.MethodInfo privateTestMethod = typeof(MethodInfoClass).GetMethod("PrivateTestMethod", bindingFlags);
if (privateTestMethod is not null)
{
string returnFromPrivateTestMethod = (string)privateTestMethod.Invoke(mic, null);
Console.WriteLine($"privateTestMethod: {returnFromPrivateTestMethod}");
}
Example project download from here.
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 2455
using System;
using System.Collections;
using System.Linq;
namespace IEnumarable
{
class Program
{
static void Main(string[] args)
{
MyIEnumerable myIEnumerable = new MyIEnumerable();
foreach (string test in myIEnumerable.Cast<string>().ToList())
{
Console.WriteLine(test);
}
}
class MyIEnumerable : IEnumerable
{
private string _names = "stanko, milosev, elizabeta, lazarevic";
public IEnumerator GetEnumerator()
{
string[] aryNames = _names.Split();
foreach (string name in aryNames)
{
yield return name;
}
}
}
}
}
POI:
myIEnumerable.Cast