1. HttpClient

参考网址:

2. async Task<value> function

Task.Factory.StartNew(() => asyncFunc(data);

private async void asyncFunc(String data) {
}

参考网址: Sign up
How to return a string from async

3. AsyncCallback Delegate

	private delegate int testDelegate(String data);
	testDelegate td = Test;
	
	void someFunction() {
		...
		AsyncCallback callBack = new AsyncCallback(callback);
		td.BeginInvoke(data, callBack, null);
		...
	}
	
	// static function
	private static int Test(String data) {
		// invoke in work thread firstly.
	}
	
        private void callback(IAsyncResult result) {
		// invoke in work thread secondly.
		int ret = td.EndInvoke(result);
	}

参考网址:

4. Update controls in work thread

public partial class Form1 : Form
{
    this.Invoke((MethodInvoker)delegate
    {
        this.label1.Text = "...";
    });
}

参考网址: Update label text in background worker winforms

5. c# invoke c++ dll

参考网址:

6. c# pass struct to c++ dll

c# part: 
    [StructLayout(LayoutKind.Sequential)]
    public struct Info
    {
        public Int32 data1;
        public Double data2;
    }
    
    const string DLL_GENERATOR = "test.dll";
    [DllImport(DLL_GENERATOR, CallingConvention = CallingConvention.Cdecl)]
    public extern static void Test(IntPtr handle, Info info);

c++ part:
#pragma once

#ifdef _SHARED_
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif

#ifdef __cplusplus
extern "C" {
#endif
    typedef struct _Info
    {
		__int32 data1;
        double	data2;
    }Info, *PInfo;
    
    EXPORT void	__cdecl Test(Handle* hd, Info info);
#ifdef __cplusplus
}
#endif

参考网址: Passing structures between C# and C/C++

7. control re-draw(update/repaint)

参考网址: How to have a control redraw the Windows form

8. draw rectangle on control(panel) according to mouse move event (double buffer)

    class MyPanel : Panel
    {
        public MyPanel()
        {
            this.DoubleBuffered = true;
            this.SetStyle(ControlStyles.ResizeRedraw, true);
        }
	
        private Bitmap img = null;
        public void setImage(Bitmap bm)
        {
            img = bm;
        }	

        bool isDown = false;
        Point bgPt = Point.Empty;
        Point edPt = Point.Empty;
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                isDown = true;
                bgPt = PointToClient(Cursor.Position);
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (isDown)
            {
                edPt = PointToClient(Cursor.Position);
                this.Invalidate();
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (isDown)
            {
                isDown = false;
                edPt = PointToClient(Cursor.Position);
                this.Invalidate();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            if (img != null)
            {
                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.InterpolationMode = InterpolationMode.Bilinear;
                g.Clear(this.BackColor);

                g.DrawImage(img, 0, 0, this.Width, this.Height);
                Pen pen = new Pen(Color.Red, 2.0f);
                g.DrawRectangle(pen, bgPt.X, bgPt.Y, edPt.X - bgPt.X, edPt.Y - bgPt.Y);
            }
        }
    }

参考网址:

8. c# parse json

参考网址: c# 解析JSON的几种办法

9. control resize with form

参考网址: C#当窗体大小改变时,窗体中的控件大小也随之改变

10. opentk & opentk.glcontrol

1. add NuGet Package Manager to Visual Studio
2. add opentk.dll and opentk.glcontrol throught NuGet Package Manager Console
3. now in C# project' reference, both dll can be found.
4. implement SomeControl inherit from GLControl like this:
    public partial class MyControl : GLControl
    {
        public MyControl()
            : base(new GraphicsMode(32, 24, 8, 4), 3, 1, GraphicsContextFlags.ForwardCompatible)
        {
            InitializeComponent();
        }
    }
5. add this contrl to Form like this:
partial class MyForm
{
    private void InitializeComponent()
    {
        // 
        // glshow
        //
        this.glshow.BackColor = System.Drawing.Color.Black;
        this.glshow.Location = new System.Drawing.Point(0, 0);
        this.glshow.Name = "gl";
        this.glshow.Size = new System.Drawing.Size(800, 600);
        this.glshow.TabIndex = 0;
        this.glshow.VSync = false;
        this.glshow.Load += new System.EventHandler(this.Show_Load);
        this.glshow.Paint += new System.Windows.Forms.PaintEventHandler(this.Show_Paint);
        this.glshow.Resize += new System.EventHandler(this.Show_Resize);
        
        //
        // MyForm
        //
        this.Controls.Add(this.glshow);
    }

    private MyControl glshow;
}
6. implement function MyForm.Show_Load MyForm.Show_Paint MyForm.Show_Resize using GL language.
7. just remember invoke glhow.SwapBuffers() after opengl render(in Show_Paint and Show_Resize)

参考网址:

11. Timer

	System.Timers.Timer timer = new System.Timers.Timer();
	timer.Elapsed += new ElapsedEventHandler(OnTimer);
	timer.AutoReset = false;
	timer.Interval = 30;
	
        private void OnTimer(object sender, ElapsedEventArgs e)
        {
		...
		playTimer.Enabled = true; // trigger timer once again
	}

参考网址: Threading.Timer with Autoreset or Single Thread?