第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

控制臺main函數(shù)中發(fā)起一個異步線程:

控制臺main函數(shù)中發(fā)起一個異步線程:

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

2 回答

?
夢里花落0921

TA貢獻1772條經(jīng)驗 獲得超6個贊

回調(diào)函數(shù)本來就是在調(diào)用方的線程中被執(zhí)行的。

若真想在主線程中得到異步的結(jié)果,應(yīng)該用消息(SendMessage)而非回調(diào)。

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

TA貢獻1812條經(jīng)驗 獲得超5個贊

可以通過線程池ThreadPool來解決,使用ThreadPool.QueueUserWorkItem(回調(diào)函數(shù),object),將參數(shù)封裝在一個類的對象中,傳給回調(diào)函數(shù)去執(zhí)行。
TheadPool的用法:
1、創(chuàng)建一個ManualResetEvent的對象,就像一個信號燈,指示線程的掛起和執(zhí)行;
2、ManualResetEvent對象創(chuàng)建時,可以指定默認狀態(tài):true為有信號,false為無信號;
3、調(diào)用Reset()方法重置狀態(tài);
4、調(diào)用WaitOne()方法,使線程處于等待狀態(tài);
5、調(diào)用Set()方法設(shè)置狀態(tài)。
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();
}
}
}

 


查看完整回答
反對 回復(fù) 2023-01-10
  • 2 回答
  • 0 關(guān)注
  • 131 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號