C#多线程(Winform和WPF)

Winform

Thread othread = new Thread(new ParameterizedThreadStart(getResult));
				othread.IsBackground = true;
				othread.Start((Object)(m));
				Thread.Sleep(50);
private delegate void LstDelegate(int ID);
private async void getResult(object _xStr)
{
   string[] xStr = (string[])_xStr;
   foreach (string j in sArray)
   {
	   
   } 
   LstDelegate D = new LstDelegate(Fun);
   this.Dispatcher.Invoke(D, new object[] {int.Parse(ID)});
}
new Thread((ThreadStart)delegate
	{
		try
		{                  
			this.Dispatcher.Invoke(new Action(() =>
			{
			}));
		}
		catch { }
	}).Start();
new Thread((ThreadStart)delegate
	{
		try
		{                  
			System.Windows.Application.Current.Dispatcher.Invoke(() =>
            {
               // 在主 UI 线程上执行需要的操作
            });
		}
		catch { }
	}).Start();

异步方法

new Thread(async () =>
{
    try
    {
        await Application.Current.Dispatcher.InvokeAsync(() =>
        {
            // 在主 UI 线程上执行需要的操作
        });
    }
    catch { }
}).Start();

Wpf

ThreadPool.QueueUserWorkItem(delegate
    {
		try 
		{
			this.Dispatcher.Invoke((System.Windows.Forms.MethodInvoker)delegate
			{
					
			});
		}
		catch { }
    });
posted @ 2023-08-09 08:04  多见多闻  阅读(115)  评论(0)    收藏  举报