小编Ber*_*ine的帖子

使用Thread.Sleep()总是坏的吗?

我为随机时间Random执行Action(void委托)的类创建了一个扩展方法:

public static class RandomExtension
{
    private static bool _isAlive;
    private static Task _executer;

    public static void ExecuteRandomAsync(this Random random, int min, int max, int minDuration, Action action)
    {
        Task outerTask = Task.Factory.StartNew(() =>
        {
            _isAlive = true;
            _executer = Task.Factory.StartNew(() => { ExecuteRandom(min, max, action); });
            Thread.Sleep(minDuration);
            StopExecuter();
        });
    }

    private static void StopExecuter()
    {
        _isAlive = false;
        _executer.Wait();

        _executer.Dispose();
        _executer = null;
    }

    private static void ExecuteRandom(int min, int max, Action action)
    {
        Random …
Run Code Online (Sandbox Code Playgroud)

c# multithreading sleep

35
推荐指数
3
解决办法
4864
查看次数

有没有一种快速的方法来操作和缓冲Windows窗体中的屏幕?

我正在开发一个用于学习目的的游戏,我想只使用.NET-Framework和C#中的Windows Forms项目.

我希望得到'屏幕'(可以在窗口上显示的东西)作为int[].修改数组并以缓冲方式将更改的数组重新应用到"屏幕"(这样它不会闪烁).

我现在使用的一个Panel,这是我画BitmapGraphics.将Bitmap被转换为int[]我然后可以修改并重新应用到Bitmap和重绘.它很有效,但速度很慢,特别是因为我必须每帧都放大图像因为我的游戏只有300x160而屏幕是900x500.

建立:

    // Renders 1 frame
    private void Render()
    {    
        // Buffer setup
        _bufferedContext = BufferedGraphicsManager.Current;
        _buffer = _bufferedContext.Allocate(panel_canvas.CreateGraphics(), new Rectangle(0, 0, _scaledWidth, _scaledHeight));

        _screen.clear();

        // Get position of player on map
        _xScroll = _player._xMap - _screen._width / 2;
        _yScroll = _player._yMap - _screen._height / 2;

        // Indirectly modifies the int[] '_pixels'
        _level.render(_xScroll, _yScroll, _screen);
        _player.render(_screen);

        // Converts the int[] into a Bitmap …
Run Code Online (Sandbox Code Playgroud)

c# buffer winforms

7
推荐指数
1
解决办法
1241
查看次数

标签 统计

c# ×2

buffer ×1

multithreading ×1

sleep ×1

winforms ×1