gpt4 book ai didi

c# - NSAutoReleasePool 和异步函数

转载 作者:行者123 更新时间:2023-12-03 17:25:37 26 4
gpt4 key购买 nike

我的程序中有一个函数可以创建新的小部件来表示数据,但是每当创建小部件时,我都会收到很多“AutoRelease with no NSAutoReleasePool in place”错误消息。由于 NSAutoReleasePool 应该在主线程上自动创建,我有一种感觉,这些错误消息的出现是因为异步函数可能会创建我的线程......

该函数被调用来创建代表最新信息的小部件。这个函数经常被调用:

    private void CreateAndDisplayTvShowWidget (TvShow show)
{
var Widget = new TvShowWidgetController (show);
Widget.OnRemoveWidget += ConfirmRemoveTvShow;

Widget.View.SetFrameOrigin (new PointF (0, -150));
Widget.View.SetFrameSize (new SizeF (ContentView.Frame.Width, 150));

ContentView.AddSubview (Widget.View);
show.ShowWidget = Widget;
}

该函数通常在异步函数返回时被调用:

    private static void WebRequestCallback (IAsyncResult result)
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse (result);

StreamReader responseStream = new StreamReader (response.GetResponseStream ());
string responseString = responseStream.ReadToEnd ();
responseStream.Close ();

ProcessResponse (responseString, request);
}

ProcessResponse(responseString,请求)如下所示:

    private static void ProcessResponse (string responseString, HttpWebRequest request)
{
string requestUrl = request.Address.ToString ();

if (requestUrl.Contains (ShowSearchTag)) {
List<TvShow> searchResults = TvDbParser.ParseTvShowSearchResults (responseString);
TvShowSearchTimeoutClock.Enabled = false;
OnTvShowSearchComplete (searchResults);
} else if (requestUrl.Contains (MirrorListTag)) {
MirrorList = TvDbParser.ParseMirrorList (responseString);
SendRequestsOnHold ();
} else if (requestUrl.Contains (TvShowBaseTag)) {
TvShowBase showBase = TvDbParser.ParseTvShowBase (responseString);
OnTvShowBaseRecieved (showBase);
} else if (requestUrl.Contains (ImagePathReqTag)) {
string showID = GetShowIDFromImagePathRequest (requestUrl);
TvShowImagePath imagePath = TvDbParser.ParseTvShowImagePath (showID, responseString);
OnTvShowImagePathRecieved (imagePath);
}
}

CreateAndDisplayTvShowWidget (TvShow show) 在调用事件 OnTvShowBaseRecieved (TvShow) 时被调用,此时我收到大量有关 NSAutoReleasePool 的错误消息...

最后两个函数是跨平台程序集的一部分,因此我不能在其中包含任何 MonoMac 特定的代码...

我从来没有为我的小部件调用任何自动释放或释放代码,所以我假设 MonoMac 绑定(bind)会自动执行此操作作为其垃圾收集的一部分?

最佳答案

您可以在调用堆栈中的某个位置创建自动释放池,甚至可以在同一调用堆栈中创建多个嵌套的自动释放池。因此,您应该能够在异步入口函数中创建自动释放池。

关于c# - NSAutoReleasePool 和异步函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5980612/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com