【发布时间】:2021-05-02 02:46:51
【问题描述】:
我最近发现了如何在后台执行任务,并尝试在 WPF 中使用这些进行测试。
我尝试测试的是在图片框中创建图片轮播。
为此,我阅读了this、this 和this,这就是我所拥有的:
public partial class Page2 : Page
{
public Thread backgroundcaroussel;
public Page2()
{
InitializeComponent();
backgroundcaroussel = new Thread(ImgFlip);
backgroundcaroussel.IsBackground = true;
backgroundcaroussel.Start();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
backgroundcaroussel.Abort();
MainWindow.Fenetre.Content = MainWindow.pgUn;
}
private void ImgFlip()
{
Again:
this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (ThreadStart)delegate ()
{
BitmapSource btmSrc1 = Imaging.CreateBitmapSourceFromHBitmap(Properties.Resources._1080p_1.GetHbitmap(),
IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
img_moins.Source = btmSrc1;
});
Thread.Sleep(2000);
this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,(ThreadStart)delegate ()
{
BitmapSource btmSrc2 = Imaging.CreateBitmapSourceFromHBitmap(Properties.Resources._1080p_2.GetHbitmap(),
IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
img_moins.Source = btmSrc2;
});
Thread.Sleep(2000);
this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (ThreadStart)delegate ()
{
BitmapSource btmSrc3 = Imaging.CreateBitmapSourceFromHBitmap(Properties.Resources._1080p_3.GetHbitmap(),
IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
img_moins.Source = btmSrc3;
});
Thread.Sleep(2000);
goto Again;
}
}
当我使用此代码时,内存使用量不会停止增加并达到 1 或 2 Go(在我停止之前)。 我不认为这是正常的:)
我还阅读了this、this 和this 来解决问题,但不清楚该怎么做。
如何解决这种内存消耗? 我是否使用了正确的方法?
【问题讨论】:
-
在一个大部分时间都在休眠的线程中执行此操作是可怕的。请改用计时器,最好是 DispatcherTimer。但是感谢您向我们展示
goto仍然存在。您应该阅读一本介绍性 C# 的书,然后可能是一本关于 WPF 的书。 -
请注意,尽管在 UI 线程中调用了 DispatcherTimer 的 Tick 事件,但您仍然可以声明 Tick 处理程序方法
async并运行创建 BitmapSource 的后台任务。 -
您经常调用Bitmap.GetHbitmap 方法。如果你看一下文档,你可以找到一个小注释:“YOU是负责调用GDI DeleteObject方法来释放GDI位图对象使用的内存”
-
另外说明,您通常不会从
Properties.Resources中的System.Drawing.Bitmap资源加载位图。相反,将图像文件添加到 Visual Studio 项目,将其构建操作设置为资源并从程序集资源包 URI 加载 BitmapImages。参见例如这里:stackoverflow.com/a/22957974/1136211 -
请不要使用goto,除非你绝对必须。在你的情况下,你不必。它和
while(true){}一样,只是更臭。
标签: c# wpf multithreading memory