static void Main(string[] args) { Console.WriteLine("Hello World\n"); Type.Show(); Statement.Show(); DoFeature.Show(); DoDelegate.Show(); /* 测试json */ //MyData.Test(); //JsonNet.SerializeJSON(); //JsonNet.DeserializeJSON(); //JsonNet.DeserializeAnonymous(); //JsonNet.TestDataSet(); /* 以下为旧的代码 */ //PrintTitle(delegate () { BuildIn.type_of(); }, "typeof"); //printtitle(delegate () { showarraylist.doarraylist(); }, "arraylist"); //printtitle(delegate () { genericclass.openorclose(); }, "泛型分类"); //printtitle(delegate () { showstaticfieldingeneric.show(); }, "泛型的静态字段"); //StaticFieldInGeneric.Show(); //DoForJson.Show(); }
public static void DoFunForFrame(int farmeNumber, DoDelegate doDelegate, object value) { if (instance == null) { InistInstance(); } instance.BeginDelegateForFarme(farmeNumber, doDelegate, value); }
public static void DoFunForTime(float waitTime, DoDelegate doDelegate, object Value) { if (instance == null) { InistInstance(); } instance.AddDelegate(waitTime, doDelegate, Value); }
IEnumerator DoDelegateForFrame(int farmeNumber, DoDelegate doDelegate, object Value) { yield return(null); farmeNumber--; for (int i = 0; i < farmeNumber; i++) { yield return(null); } doDelegate(Value); }
/************************************************************* * Function: downloadOutput(object obj) * Date Created: April 12, 2017 * Date Last Modified: April 13, 2017 * Description: invoke the message * Return: NONE *************************************************************/ private void downloadOutput(object obj) { if (InvokeRequired) { DoDelegate method = new DoDelegate(downloadOutput); Invoke(method, obj); return; } string text = (string)obj; download_textBox.Text += text + "\r\n"; }
/************************************************************* * Function: multiSort() * Date Created: April 12, 2017 * Date Last Modified: April 14, 2017 * Description: multisorting thread * Return: NONE *************************************************************/ private void multiSort() { DoDelegate multisorting = new DoDelegate(sortingOutput); List <Thread> list_threads = new List <Thread>(); DateTime start = DateTime.Now; for (int i = 0; i < 8; i++) { // using ParameterizedThreadStart is a way to reliably get a sorted list Thread t = new Thread(new ParameterizedThreadStart(sorti)); list_threads.Add(t); t.Start(i); } //if put end here is roughly 4 (4 core cpu) time faster than the single. //making interface more reasonable. and it turely sort all list. DateTime end = DateTime.Now; //it reliably do all 8 threads. foreach (Thread t in list_threads) { t.Join(); } //DateTime end = DateTime.Now; //but if put end here the time will increase, since join will pause the main thread. //make sure all thread are done. but it will cause more time to do. and only less item may //unsorted at this point. we may assume the list already sorted. //determine when 8 threads complete int count = 0; foreach (var sub in mutil_thread_list) { count++; for (int i = 0; i < sub.Count - 1; i++) { if (sub[i] > sub[i + 1]) { multisorting.Invoke("list " + count); multisorting.Invoke("not sorted " + sub[i] + " -> " + sub[i + 1]); } } } string MultiThread = "Multi-threaded time: " + (int)timeDiff(start, end) + " ms"; multisorting.Invoke(MultiThread); isEnabled_sort(true); }
/************************************************************* * Function: sortingOutput(object obj) * Date Created: April 12, 2017 * Date Last Modified: April 13, 2017 * Description: invoke sorting output * Return: NONE *************************************************************/ private void sortingOutput(object obj) { if (InvokeRequired) { DoDelegate method = new DoDelegate(sortingOutput); Invoke(method, obj); return; } string text = obj.ToString(); sorting_textbox.Text += text + "\r\n"; }
/************************************************************* * Function: sortingList(object o) * Date Created: April 12, 2017 * Date Last Modified: April 14, 2017 * Description: do the sorting thread * Return: NONE *************************************************************/ void sortingList(object o) { DoDelegate sort = new DoDelegate(sortingOutput); Random randnum = new Random(); for (int x = 0; x < 8; x++) { List <int> sublist = new List <int>(); List <int> sublist2 = new List <int>(); for (int y = 0; y < 1000000; y++) { sublist.Add(randnum.Next(0, 1000000)); sublist2.Add(randnum.Next(0, 1000000)); } single_thread_list.Add(sublist); mutil_thread_list.Add(sublist2); } DateTime start = DateTime.Now; foreach (var sub in single_thread_list) { sub.Sort(); } DateTime end = DateTime.Now; string singleThread = "Single-threaded time: " + (int)timeDiff(start, end) + " ms"; sort.Invoke(singleThread); Thread multisorting = new Thread(multiSort); multisorting.Start(); }
IEnumerator Dofun(float waitTime, DoDelegate doDelegate, object Value) { yield return(new WaitForSeconds(waitTime)); doDelegate(Value); }
void BeginDelegateForFarme(int farmeNumber, DoDelegate doDelegate, object value) { StartCoroutine(DoDelegateForFrame(farmeNumber, doDelegate, value)); }
void AddDelegate(float waitTime, DoDelegate doDelegate, object Value) { StartCoroutine(Dofun(waitTime, doDelegate, Value)); }
/// <summary> /// Starts given action on new thread. Does not wait for or notify when action ends. /// </summary> public static void Start(DoDelegate action) { /*WaitCallback waitCallBack = delegate(object state) { action(); }; ThreadPool.QueueUserWorkItem(waitCallBack);*/ // background worker supports exception handling BackgroundWorker bw = new BackgroundWorker(); bw.DoWork += (object sender, DoWorkEventArgs e) => action(); bw.RunWorkerCompleted += (object sender, RunWorkerCompletedEventArgs e) => { if (e.Error != null) { throw e.Error; } }; bw.RunWorkerAsync(); }