Here is first simple example:
using System;
using System.Threading;
namespace ThreadExample1
{
class Program
{
static void Main(string[] args)
{
MyListThreadClass myListThreadClass = new MyListThreadClass();
Thread backgroundThread = new Thread(new ThreadStart(myListThreadClass.AddToMyList));
backgroundThread.Name = "Secondary";
backgroundThread.Start();
myListThreadClass.AddToMyList();
}
}
public class MyListThreadClass
{
public void AddToMyList()
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine($"i: {i}, Thread.CurrentThread.ManagedThreadId: {Thread.CurrentThread.ManagedThreadId}");
}
}
}
}
First example with parameters:
using System;
using System.Threading;
namespace ThreadExampleWithParameters
{
class Program
{
static void Main(string[] args)
{
MyListThreadClass myListThreadClass = new MyListThreadClass();
Thread backgroundThread = new Thread(new ParameterizedThreadStart(myListThreadClass.AddToMyList));
backgroundThread.Name = "Secondary";
backgroundThread.Start("milosev");
myListThreadClass.AddToMyList("stanko");
}
}
public class MyListThreadClass
{
public void AddToMyList(object myName)
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine($"i: {myName}{i}, Thread.CurrentThread.ManagedThreadId: {Thread.CurrentThread.ManagedThreadId}");
}
}
}
}
Here notice line:
myListThreadClass.AddToMyList("stanko");
and method signature:
public void AddToMyList(object myName)
object
Second example with parameters:
using System;
using System.Threading;
namespace ThreadExampleWithParameters
{
class Program
{
static void Main(string[] args)
{
MyListThreadClass myListThreadClass = new MyListThreadClass();
Thread backgroundThread = new Thread(delegate() { myListThreadClass.AddToMyList("milosev"); });
backgroundThread.Name = "Secondary";
backgroundThread.Start();
myListThreadClass.AddToMyList("stanko");
}
}
public class MyListThreadClass
{
public void AddToMyList(string myName)
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine($"i: {myName}{i}, Thread.CurrentThread.ManagedThreadId: {Thread.CurrentThread.ManagedThreadId}");
}
}
}
}
Here notice how I define thread:
Thread backgroundThread = new Thread(delegate() { myListThreadClass.AddToMyList("milosev"); });
and method signature:
public void AddToMyList(string myName)
string
In next example, every time when something is added to list, I want to notify a method, where I will display new item added to list:
#nullable enable
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Threading;
namespace ThreadExampleWithObservable
{
class Program
{
static void Main(string[] args)
{
MyListThreadClass<List<string>> myListThreadClass = new MyListThreadClass<List<string>>();
ObservableCollection<List<string>> myListThread = new ObservableCollection<List<string>>();
myListThread.CollectionChanged += myListThreadClass.ReadFromList;
Thread backgroundWriteThread = new Thread(myListThreadClass.AddToMyList)
{
Name = "Secondary"
};
backgroundWriteThread.Start(myListThread);
myListThreadClass.AddToMyList(myListThread);
}
}
public class MyListThreadClass<T> where T : List<string>
{
int _callTimes = 0;
public void AddToMyList(object myListThread)
{
for (int i = 0; i < 100; i++)
{
((ObservableCollection<List<string>>)myListThread).Add(new List<string> { $"test.AddToMyList: {i}" });
}
}
public void ReadFromList(object? sender, NotifyCollectionChangedEventArgs e)
{
_callTimes++;
if (e.NewItems != null)
{
Console.WriteLine($"e.NewItems.Count: {e.NewItems.Count}");
foreach (List<string> myItem in e.NewItems)
{
Console.WriteLine($"myItem.Count: {myItem.Count}");
foreach (string itemString in myItem)
{
Console.WriteLine(
$"itemString: {itemString}, callTimes: {_callTimes}, Thread.CurrentThread.ManagedThreadId: {Thread.CurrentThread.ManagedThreadId}");
}
}
}
}
}
}
Here notice line:
public void AddToMyList(object myListThread)
as well as that method signature is not type safe.