亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

控制臺main函數中發起一個異步線程:

控制臺main函數中發起一個異步線程:

C#
楊__羊羊 2023-01-06 14:10:01
GetDoubleNumericalHandler handler = new GetDoubleNumericalHandler(getDoubleNumerical.GetData);IAsyncResult result = handler.BeginInvoke(new AsyncCallback(CallbackFunction), "AsycState:OK");CallbackFunction是回調函數但是測試發現回調函數是在異步線程中執行的,請問大神們有沒有什么方法可以將回調函數插入到主線程中?
查看完整描述

2 回答

?
夢里花落0921

TA貢獻1772條經驗 獲得超6個贊

回調函數本來就是在調用方的線程中被執行的。

若真想在主線程中得到異步的結果,應該用消息(SendMessage)而非回調。

查看完整回答
反對 回復 2023-01-10
?
慕雪6442864

TA貢獻1812條經驗 獲得超5個贊

可以通過線程池ThreadPool來解決,使用ThreadPool.QueueUserWorkItem(回調函數,object),將參數封裝在一個類的對象中,傳給回調函數去執行。
TheadPool的用法:
1、創建一個ManualResetEvent的對象,就像一個信號燈,指示線程的掛起和執行;
2、ManualResetEvent對象創建時,可以指定默認狀態:true為有信號,false為無信號;
3、調用Reset()方法重置狀態;
4、調用WaitOne()方法,使線程處于等待狀態;
5、調用Set()方法設置狀態。
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;

namespace Demo
{
public class ParamObject
{
public int number;
public ParamObject (int number)
{
this.number = number;
}
}

public class ThreadClass
{
public Hashtable aHashTable;
public ManualResetEvent aManualResetEvent;
public static int iCount = 0;
public static int iMaxCount = 0;

public ThreadClass(int maxCount)
{
aHashTable = new Hashtable(maxCount);
iMaxCount = maxCount;
}

public void ThreadRun(object aParamObject)
{
Console.WriteLine("HashCode: {0}, Number in Object: {1}", Thread.CurrentThread.GetHashCode(), ((ParamObject)aParamObject).number);
lock (aHashTable)
{
if (!aHashTable.ContainsKey(Thread.CurrentThread.GetHashCode()))
{
aHashTable.Add(Thread.CurrentThread.GetHashCode(), 0);
}
aHashTable[Thread.CurrentThread.GetHashCode()] = (int)aHashTable[Thread.CurrentThread.GetHashCode()] + 1;
}
Thread.Sleep(3000);

Interlocked.Increment(ref iCount);
if (iCount == iMaxCount)
{
Console.WriteLine("Setting aManualResetEvent...");
aManualResetEvent.Set();
}
}
}

class Program
{
public static void Main(string[] args)
{
bool enableThreadPool = false;
int iMaxCount = 20;
ManualResetEvent aManualResetEvent = new ManualResetEvent(false);

Console.WriteLine("Insert {0} items to Thread Pool.", iMaxCount);
ThreadClass aThreadClass = new ThreadClass(iMaxCount);
aThreadClass.aManualResetEvent = aManualResetEvent;

// First, add an item to check if your system supports ThreadPool API function or not.
try
{
ThreadPool.QueueUserWorkItem(new WaitCallback(aThreadClass.ThreadRun), new ParamObject(0));
enableThreadPool = true;
}
catch (NotSupportedException ex)
{
Console.WriteLine("Thread Pool API is not supported in this system.");
enableThreadPool = false;
}

if (enableThreadPool)
{
for (int i = 1; i < iMaxCount; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(aThreadClass.ThreadRun), new ParamObject(i));
}
Console.WriteLine("Waiting for thread pool to drain");
aManualResetEvent.WaitOne(Timeout.Infinite, true);

Console.WriteLine("Thread Pool has been drained.");
Console.WriteLine("Load threads info:");
foreach (object key in aThreadClass.aHashTable.Keys)
{
Console.WriteLine("Key: {0}, Value: {1}", key, aThreadClass.aHashTable[key]);
}
}
Console.ReadLine();
}
}
}

 


查看完整回答
反對 回復 2023-01-10
  • 2 回答
  • 0 關注
  • 124 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號