首页 / C# / c# – 延迟加载和多线程
c# – 延迟加载和多线程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 延迟加载和多线程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2494字,纯文字阅读大概需要4分钟。
内容图文
![c# – 延迟加载和多线程](/upload/InfoBanner/zyjiaocheng/791/d056ff65e0634e708196502014b912a2.jpg)
我正在尝试使用iCarousel库进行单点操作.我成功地移植了库,一切都运行得很好但是如果你输入太多带有图像的UIImageViews应用程序会崩溃,这是正常的,因为iCarousel就像一个UIScrollView.
我必须从辅助线程以某种方式使用延迟加载系统,一次只显示3-4张图像,但我不知道如何使这项工作顺利进行.
此时,我在iCarousel代表中设置了这个:
bool threadsAlive = true;
public cDelegate()
{
ThreadPool.QueueUserWorkItem( delegate { refresh_visible(); } );
}
public override void DidScroll (iCarousel carousel)
{
scrolling = true;
}
public override void DidEndScrollingAnimation (iCarousel carousel)
{
scrolling = false;
//show images that are currently on the screen
ThreadPool.QueueUserWorkItem( delegate { ShowCurrent(); } );
//hides images that are not on the screen
ThreadPool.QueueUserWorkItem( delegate { hideInvisibleImages(); } );
}
void refresh_visible()
{
while( threadsAlive )
{
while( scrolling )
{
ShowCurrent();
}
}
}
void refresh_hidden()
{
while( threadsAlive )
{
while( scrolling )
{
hideInvisibleImages();
}
}
}
public void ShowCurrent()
{
var ds = _carousel.DataSource as cDataSource;
var left_index = _carousel.CurrentItemIndex - 1;
var right_index = _carousel.CurrentItemIndex + 2;
if( left_index < 0 ) left_index = 0;
if( right_index >= ds.Lista.Count ) right_index = ds.Lista.Count - 1;
//
for( var i = left_index; i < right_index ; i++ )
{
var img = ds.Lista[i];
if( img.Image == null )
{
BeginInvokeOnMainThread( delegate{
img.Image = UIImage.FromFile( img.UserObject.ToString() );
});
}
}
}
void hideInvisibleImages()
{
Console.WriteLine("ascund!");
var ds = _carousel.DataSource as cDataSource;
var left_index = _carousel.CurrentItemIndex - 1;
var right_index = _carousel.CurrentItemIndex + 2;
if( left_index < 0 ) left_index = 0;
if( right_index >= ds.Lista.Count ) right_index = ds.Lista.Count - 1;
//
for( var i=0; i<left_index; i++ )
{
var img = ds.Lista[i];
if( img.Image != null )
{
img.Image.Dispose();
img.Image = null;
}
}
for( var i=right_index; i<ds.Lista.Count; i++ )
{
var img = ds.Lista[i];
if( img.Image != null )
{
img.Image.Dispose();
img.Image = null;
}
}
}
代码实际上非常简单:有一个主线程只显示当前索引左侧的1个图像和两个图像,另一个清除所有其他图像的线程隐藏它们.
它工作正常,内存还可以,但它在设备上并不流畅,当我滚动时它会“挂起”一点.还有另一种方法吗?或许我应该改变算法?
解决方法:
您有一个循环,不允许CPU到任何其他线程/进程,并将导致非常高的CPU利用率.滚动时,它会挂起.
尝试在refresh_visible方法中使用Thread.Sleep(1)或小睡眠时间.
内容总结
以上是互联网集市为您收集整理的c# – 延迟加载和多线程全部内容,希望文章能够帮你解决c# – 延迟加载和多线程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。