站長123之家http://www.tacshn.com/主機測評_編程開發_網站SEO運營推廣工具門戶網站C#基礎知識-使用XML完成一個小程序(十一)http://kf.icp123.net/sjk/8736.html <p><span style="font-size: 18px">上一篇中講到XML基本的結構,還有增刪改查的方法,這一篇中我們就來利用XML來完成一個簡單的訂單系統,主要是實現一個簡單學生名單的增刪改查,如果想要應用到實際的環境中建議考慮數據量的問題,如果數據量大使用XML的話會比較耗時,使用SQL的性能會好一些</span></p> <p>?</p> <p><span style="font-size: 18px"><strong>這里使用</strong><strong>WinForm</strong><strong>窗體程序,大致界面如下:</strong></span></p> <p>?<img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214656160484321643533.png"></p> <ol> <li>創建了兩個窗體,一個主窗體用來顯示名單數據,當添加或者編輯操作時則使用另外一個窗體。</li> <li>主窗體中放置四個按鈕,以及一個DataGridView控件,另外一個添加、編輯窗體,則按照相應的字段放置好控件。</li> <li>給各控件命好名。</li> </ol> <p><span style="font-size: 18pt"><strong>XML</strong><strong>文檔的格式如下:</strong></span></p> <div class="cnblogs_code"> <pre><span style="color: #008080"> 1</span> <span style="color: #0000ff">&lt;?</span><span style="color: #ff00ff">xml version="1.0" encoding="utf-8"</span><span style="color: #0000ff">?&gt;</span> <span style="color: #008080"> 2</span> <span style="color: #008080"> 3</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">學生名單</span><span style="color: #0000ff">&gt;</span> <span style="color: #008080"> 4</span> <span style="color: #008080"> 5</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">學生 Name</span><span style="color: #0000ff">="李四一"</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="0102"</span><span style="color: #0000ff">&gt;</span> <span style="color: #008080"> 6</span> <span style="color: #008080"> 7</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">性別</span><span style="color: #0000ff">&gt;</span>男<span style="color: #0000ff">&lt;/</span><span style="color: #800000">性別</span><span style="color: #0000ff">&gt;</span> <span style="color: #008080"> 8</span> <span style="color: #008080"> 9</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">生日</span><span style="color: #0000ff">&gt;</span>1999年1月2日<span style="color: #0000ff">&lt;/</span><span style="color: #800000">生日</span><span style="color: #0000ff">&gt;</span> <span style="color: #008080">10</span> <span style="color: #008080">11</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">學號</span><span style="color: #0000ff">&gt;</span>20170<span style="color: #0000ff">&lt;/</span><span style="color: #800000">學號</span><span style="color: #0000ff">&gt;</span> <span style="color: #008080">12</span> <span style="color: #008080">13</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">學生</span><span style="color: #0000ff">&gt;</span> <span style="color: #008080">14</span> <span style="color: #008080">15</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">學生 Name</span><span style="color: #0000ff">="張三三"</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="高一"</span><span style="color: #0000ff">&gt;</span> <span style="color: #008080">16</span> <span style="color: #008080">17</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">性別</span><span style="color: #0000ff">&gt;</span>男<span style="color: #0000ff">&lt;/</span><span style="color: #800000">性別</span><span style="color: #0000ff">&gt;</span> <span style="color: #008080">18</span> <span style="color: #008080">19</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">學號</span><span style="color: #0000ff">&gt;</span>0102<span style="color: #0000ff">&lt;/</span><span style="color: #800000">學號</span><span style="color: #0000ff">&gt;</span> <span style="color: #008080">20</span> <span style="color: #008080">21</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">生日</span><span style="color: #0000ff">&gt;</span>2017年6月18日<span style="color: #0000ff">&lt;/</span><span style="color: #800000">生日</span><span style="color: #0000ff">&gt;</span> <span style="color: #008080">22</span> <span style="color: #008080">23</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">學生</span><span style="color: #0000ff">&gt;</span> <span style="color: #008080">24</span> <span style="color: #008080">25</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">學生名單</span><span style="color: #0000ff">&gt;</span></pre> </div> <p><span style="font-size: 14px; line-height: 1.5">?</span>學生名單為根元素,班級為根元素中的子元素,班級中又嵌套&lt;學生&gt;標記,了解了數據是以怎么的結構儲存那么下面就繼續。</p> <p>?</p> <p><span style="font-size: 14px; line-height: 1.5"><span style="font-size: 18pt"><strong>數據在</strong><strong>DataGridView</strong><strong>中的顯示</strong></span><br>將XML中的數據,順序顯示在DataGridView控件中</span>?</p> <div class="cnblogs_code"> <pre><span style="color: #008080"> 1</span> <span style="color: #008000">//</span><span style="color: #008000">創建一個datatable存儲XML中的數據</span> <span style="color: #008080"> 2</span> <span style="color: #008080"> 3</span> DataTable dt = <span style="color: #0000ff">new</span><span style="color: #000000"> DataTable(); </span><span style="color: #008080"> 4</span> <span style="color: #008080"> 5</span> <span style="color: #008000">//</span><span style="color: #008000">指定每一列的列名,和數據類型</span> <span style="color: #008080"> 6</span> <span style="color: #008080"> 7</span> dt.Columns.Add(<span style="color: #800000">"</span><span style="color: #800000">Id</span><span style="color: #800000">"</span>, (<span style="color: #0000ff">typeof</span><span style="color: #000000">(System.String))); </span><span style="color: #008080"> 8</span> <span style="color: #008080"> 9</span> dt.Columns.Add(<span style="color: #800000">"</span><span style="color: #800000">班級</span><span style="color: #800000">"</span>, (<span style="color: #0000ff">typeof</span><span style="color: #000000">(System.String))); </span><span style="color: #008080">10</span> <span style="color: #008080">11</span> dt.Columns.Add(<span style="color: #800000">"</span><span style="color: #800000">姓名</span><span style="color: #800000">"</span>, (<span style="color: #0000ff">typeof</span><span style="color: #000000">(System.String))); </span><span style="color: #008080">12</span> <span style="color: #008080">13</span> dt.Columns.Add(<span style="color: #800000">"</span><span style="color: #800000">學號</span><span style="color: #800000">"</span>, (<span style="color: #0000ff">typeof</span><span style="color: #000000">(System.String))); </span><span style="color: #008080">14</span> <span style="color: #008080">15</span> dt.Columns.Add(<span style="color: #800000">"</span><span style="color: #800000">生日</span><span style="color: #800000">"</span>, (<span style="color: #0000ff">typeof</span><span style="color: #000000">(System.String))); </span><span style="color: #008080">16</span> <span style="color: #008080">17</span> dt.Columns.Add(<span style="color: #800000">"</span><span style="color: #800000">性別</span><span style="color: #800000">"</span>, (<span style="color: #0000ff">typeof</span><span style="color: #000000">(System.String))); </span><span style="color: #008080">18</span> <span style="color: #008080">19</span> <span style="color: #008000">//</span><span style="color: #008000">創建一個DataRow對象</span> <span style="color: #008080">20</span> <span style="color: #008080">21</span> DataRow dr =<span style="color: #000000"> dt.NewRow(); </span><span style="color: #008080">22</span> <span style="color: #008080">23</span> <span style="color: #008080">24</span> <span style="color: #008080">25</span> XmlDocument doc = <span style="color: #0000ff">new</span><span style="color: #000000"> XmlDocument(); </span><span style="color: #008080">26</span> <span style="color: #008080">27</span> <span style="color: #008000">//</span><span style="color: #008000">為相對路徑表示前二級的目錄</span> <span style="color: #008080">28</span> <span style="color: #008080">29</span> doc.Load(<span style="color: #800000">@"</span><span style="color: #800000">..//..//Students.xml</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080">30</span> <span style="color: #008080">31</span> XmlElement root =<span style="color: #000000"> doc.DocumentElement; </span><span style="color: #008080">32</span> <span style="color: #008080">33</span> XmlNodeList studentNodes = root.GetElementsByTagName(<span style="color: #800000">"</span><span style="color: #800000">學生</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080">34</span> <span style="color: #008080">35</span> <span style="color: #008000">//</span><span style="color: #008000">使用foreach遍歷集合元素</span> <span style="color: #008080">36</span> <span style="color: #008080">37</span> <span style="color: #0000ff">foreach</span>(<span style="color: #0000ff">var</span> item <span style="color: #0000ff">in</span><span style="color: #000000"> studentNodes) </span><span style="color: #008080">38</span> <span style="color: #008080">39</span> <span style="color: #000000">{ </span><span style="color: #008080">40</span> <span style="color: #008080">41</span> dr[<span style="color: #800000">"</span><span style="color: #800000">Id</span><span style="color: #800000">"</span>] = ((XmlElement)item).GetAttribute(<span style="color: #800000">"</span><span style="color: #800000">Id</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080">42</span> <span style="color: #008080">43</span> dr[<span style="color: #800000">"</span><span style="color: #800000">班級</span><span style="color: #800000">"</span>] = ((XmlElement)item).GetAttribute(<span style="color: #800000">"</span><span style="color: #800000">Class</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080">44</span> <span style="color: #008080">45</span> dr[<span style="color: #800000">"</span><span style="color: #800000">姓名</span><span style="color: #800000">"</span>] = ((XmlElement)item).GetAttribute(<span style="color: #800000">"</span><span style="color: #800000">Name</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080">46</span> <span style="color: #008080">47</span> dr[<span style="color: #800000">"</span><span style="color: #800000">學號</span><span style="color: #800000">"</span>] = ((XmlElement)item).GetElementsByTagName(<span style="color: #800000">"</span><span style="color: #800000">學號</span><span style="color: #800000">"</span>)[<span style="color: #800080">0</span><span style="color: #000000">].InnerText; </span><span style="color: #008080">48</span> <span style="color: #008080">49</span> dr[<span style="color: #800000">"</span><span style="color: #800000">生日</span><span style="color: #800000">"</span>] = ((XmlElement)item).GetElementsByTagName(<span style="color: #800000">"</span><span style="color: #800000">生日</span><span style="color: #800000">"</span>)[<span style="color: #800080">0</span><span style="color: #000000">].InnerText; </span><span style="color: #008080">50</span> <span style="color: #008080">51</span> dr[<span style="color: #800000">"</span><span style="color: #800000">性別</span><span style="color: #800000">"</span>] = ((XmlElement)item).GetElementsByTagName(<span style="color: #800000">"</span><span style="color: #800000">性別</span><span style="color: #800000">"</span>)[<span style="color: #800080">0</span><span style="color: #000000">].InnerText; </span><span style="color: #008080">52</span> <span style="color: #008080">53</span> <span style="color: #000000"> dt.Rows.Add(dr); </span><span style="color: #008080">54</span> <span style="color: #008080">55</span> <span style="color: #000000">} </span><span style="color: #008080">56</span> <span style="color: #008080">57</span> <span style="color: #008000">//</span><span style="color: #008000">將DataTable綁定到DataGridView控件</span> <span style="color: #008080">58</span> <span style="color: #008080">59</span> dgvStudents.DataSource = dt;</pre> </div> <p>?</p> <p><span style="font-size: 18pt"><strong>增加記錄</strong></span></p> <p><span style="font-size: 14px; line-height: 1.5">因為添加學生和編輯學生都需要在同一個窗體,那么在打開添加或編輯窗體時使用構造函數來傳值,并判斷是編輯還是添加。</span>?</p> <div class="cnblogs_code"> <img id="code_img_closed_f650205a-0c86-43cb-8cc9-474ab6d53077" class="code_img_closed" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214656160484321631757.gif"><img id="code_img_opened_f650205a-0c86-43cb-8cc9-474ab6d53077" class="code_img_opened" style="display: none" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214657160484321725192.gif"><div id="cnblogs_code_open_f650205a-0c86-43cb-8cc9-474ab6d53077" class="cnblogs_code_hide"> <pre><span style="color: #008080"> 1</span> <span style="color: #000000">添加編輯窗體代碼: </span><span style="color: #008080"> 2</span> <span style="color: #008080"> 3</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">partial</span> <span style="color: #0000ff">class</span><span style="color: #000000"> FrmStudentAddandEdit : Form </span><span style="color: #008080"> 4</span> <span style="color: #008080"> 5</span> <span style="color: #000000">{ </span><span style="color: #008080"> 6</span> <span style="color: #008080"> 7</span> <span style="color: #008000">//</span><span style="color: #008000">全局變量接受傳值</span> <span style="color: #008080"> 8</span> <span style="color: #008080"> 9</span> <span style="color: #0000ff">bool</span><span style="color: #000000"> IsAdd; </span><span style="color: #008080"> 10</span> <span style="color: #008080"> 11</span> <span style="color: #0000ff">string</span><span style="color: #000000"> Name; </span><span style="color: #008080"> 12</span> <span style="color: #008080"> 13</span> <span style="color: #008000">//</span><span style="color: #008000">使用構造函數進行傳值 true為增加,false為編輯</span> <span style="color: #008080"> 14</span> <span style="color: #008080"> 15</span> <span style="color: #0000ff">public</span> FrmStudentAddandEdit(<span style="color: #0000ff">bool</span> isAdd, <span style="color: #0000ff">string</span><span style="color: #000000"> name) </span><span style="color: #008080"> 16</span> <span style="color: #008080"> 17</span> <span style="color: #000000">{ </span><span style="color: #008080"> 18</span> <span style="color: #008080"> 19</span> <span style="color: #0000ff">this</span>.IsAdd =<span style="color: #000000"> isAdd; </span><span style="color: #008080"> 20</span> <span style="color: #008080"> 21</span> <span style="color: #0000ff">this</span>.Name =<span style="color: #000000"> name; </span><span style="color: #008080"> 22</span> <span style="color: #008080"> 23</span> <span style="color: #000000"> InitializeComponent(); </span><span style="color: #008080"> 24</span> <span style="color: #008080"> 25</span> <span style="color: #000000"> } </span><span style="color: #008080"> 26</span> <span style="color: #008080"> 27</span> <span style="color: #000000">} </span><span style="color: #008080"> 28</span> <span style="color: #008080"> 29</span> <span style="color: #008080"> 30</span> <span style="color: #008080"> 31</span> <span style="color: #000000">主窗體添加按鈕單擊事件代碼: </span><span style="color: #008080"> 32</span> <span style="color: #008080"> 33</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> btnAdd_Click(<span style="color: #0000ff">object</span><span style="color: #000000"> sender, EventArgs e) </span><span style="color: #008080"> 34</span> <span style="color: #008080"> 35</span> <span style="color: #000000">{ </span><span style="color: #008080"> 36</span> <span style="color: #008080"> 37</span> <span style="color: #008000">//</span><span style="color: #008000">在創建窗體時傳值</span> <span style="color: #008080"> 38</span> <span style="color: #008080"> 39</span> FrmStudentAddandEdit form = <span style="color: #0000ff">new</span> FrmStudentAddandEdit(<span style="color: #0000ff">true</span><span style="color: #000000">); </span><span style="color: #008080"> 40</span> <span style="color: #008080"> 41</span> <span style="color: #000000"> form.ShowDialog(); </span><span style="color: #008080"> 42</span> <span style="color: #008080"> 43</span> <span style="color: #000000">} </span><span style="color: #008080"> 44</span> <span style="color: #008080"> 45</span> <span style="color: #008080"> 46</span> <span style="color: #008080"> 47</span> <span style="color: #000000">新增記錄代碼: </span><span style="color: #008080"> 48</span> <span style="color: #008080"> 49</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> btnOK_Click(<span style="color: #0000ff">object</span><span style="color: #000000"> sender, EventArgs e) </span><span style="color: #008080"> 50</span> <span style="color: #008080"> 51</span> <span style="color: #000000">{ </span><span style="color: #008080"> 52</span> <span style="color: #008080"> 53</span> XmlDocument doc = <span style="color: #0000ff">new</span><span style="color: #000000"> XmlDocument(); </span><span style="color: #008080"> 54</span> <span style="color: #008080"> 55</span> doc.Load(<span style="color: #800000">@"</span><span style="color: #800000">..//..//Students.xml</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080"> 56</span> <span style="color: #008080"> 57</span> XmlElement root =<span style="color: #000000"> doc.DocumentElement; </span><span style="color: #008080"> 58</span> <span style="color: #008080"> 59</span> <span style="color: #008000">//</span><span style="color: #008000">添加&lt;學生&gt;子節點</span> <span style="color: #008080"> 60</span> <span style="color: #008080"> 61</span> XmlElement Shutdent = doc.CreateElement(<span style="color: #800000">"</span><span style="color: #800000">學生</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080"> 62</span> <span style="color: #008080"> 63</span> <span style="color: #008000">//</span><span style="color: #008000">設置Name屬性和Class屬性</span> <span style="color: #008080"> 64</span> <span style="color: #008080"> 65</span> Shutdent.SetAttribute(<span style="color: #800000">"</span><span style="color: #800000">Name</span><span style="color: #800000">"</span><span style="color: #000000">, txtName.Text.Trim()); </span><span style="color: #008080"> 66</span> <span style="color: #008080"> 67</span> Shutdent.SetAttribute(<span style="color: #800000">"</span><span style="color: #800000">Class</span><span style="color: #800000">"</span><span style="color: #000000">, txtClass.Text.Trim()); </span><span style="color: #008080"> 68</span> <span style="color: #008080"> 69</span> <span style="color: #008000">//</span><span style="color: #008000">創建子節點</span> <span style="color: #008080"> 70</span> <span style="color: #008080"> 71</span> XmlElement eSex = doc.CreateElement(<span style="color: #800000">"</span><span style="color: #800000">性別</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080"> 72</span> <span style="color: #008080"> 73</span> XmlText tSex =<span style="color: #000000"> doc.CreateTextNode(cmbSex.Text.Trim()); </span><span style="color: #008080"> 74</span> <span style="color: #008080"> 75</span> <span style="color: #008000">//</span><span style="color: #008000">子節點添加內容</span> <span style="color: #008080"> 76</span> <span style="color: #008080"> 77</span> <span style="color: #000000"> eSex.AppendChild(tSex); </span><span style="color: #008080"> 78</span> <span style="color: #008080"> 79</span> <span style="color: #008000">//</span><span style="color: #008000">將子節點添加進學生標記</span> <span style="color: #008080"> 80</span> <span style="color: #008080"> 81</span> <span style="color: #000000"> Shutdent.AppendChild(eSex); </span><span style="color: #008080"> 82</span> <span style="color: #008080"> 83</span> <span style="color: #008080"> 84</span> <span style="color: #008080"> 85</span> XmlElement eNum = doc.CreateElement(<span style="color: #800000">"</span><span style="color: #800000">學號</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080"> 86</span> <span style="color: #008080"> 87</span> XmlText tNum =<span style="color: #000000"> doc.CreateTextNode(txtNumber.Text.Trim()); </span><span style="color: #008080"> 88</span> <span style="color: #008080"> 89</span> <span style="color: #000000"> eNum.AppendChild(tNum); </span><span style="color: #008080"> 90</span> <span style="color: #008080"> 91</span> <span style="color: #000000"> Shutdent.AppendChild(eNum); </span><span style="color: #008080"> 92</span> <span style="color: #008080"> 93</span> <span style="color: #008080"> 94</span> <span style="color: #008080"> 95</span> XmlElement eBirthday = doc.CreateElement(<span style="color: #800000">"</span><span style="color: #800000">生日</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080"> 96</span> <span style="color: #008080"> 97</span> XmlText tBirthday = doc.CreateTextNode(dtpBirthday.Text.Trim(<span style="color: #800000">'</span><span style="color: #800000">-</span><span style="color: #800000">'</span><span style="color: #000000">)); </span><span style="color: #008080"> 98</span> <span style="color: #008080"> 99</span> <span style="color: #000000"> eBirthday.AppendChild(tBirthday); </span><span style="color: #008080">100</span> <span style="color: #008080">101</span> <span style="color: #000000"> Shutdent.AppendChild(eBirthday); </span><span style="color: #008080">102</span> <span style="color: #008080">103</span> <span style="color: #008080">104</span> <span style="color: #008080">105</span> <span style="color: #008000">//</span><span style="color: #008000">將&lt;學生&gt;標記添加進根節點</span> <span style="color: #008080">106</span> <span style="color: #008080">107</span> <span style="color: #000000"> root.AppendChild(Shutdent); </span><span style="color: #008080">108</span> <span style="color: #008080">109</span> <span style="color: #008080">110</span> <span style="color: #008080">111</span> doc.Save(<span style="color: #800000">@"</span><span style="color: #800000">..//..//Students.xml</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080">112</span> <span style="color: #008080">113</span> MessageBox.Show(<span style="color: #800000">"</span><span style="color: #800000">添加成功!</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080">114</span> <span style="color: #008080">115</span> <span style="color: #0000ff">this</span><span style="color: #000000">.Close(); </span><span style="color: #008080">116</span> <span style="color: #008080">117</span> }</pre> </div> <span class="cnblogs_code_collapse">View Code</span> </div> <p>?</p> <p><span style="font-size: 18pt"><strong>編輯記錄</strong></span></p> <p><span style="font-size: 14px; line-height: 1.5">由于編輯需要查找到這條記錄然后復制到控件中顯示,那么在我們啟動”添加或編輯窗體”時需要判斷當前是添加狀態還是編輯狀態,并將表格選擇行的姓名字段傳給構造函數,通過這個姓名字段查找相應的值,然后篩選出來再給控件賦值。</span>?</p> <div class="cnblogs_code"> <img id="code_img_closed_61e93e2c-5cd2-4c40-ac48-44a27f800685" class="code_img_closed" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214657160484321747241.gif"><img id="code_img_opened_61e93e2c-5cd2-4c40-ac48-44a27f800685" class="code_img_opened" style="display: none" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214658160484321872232.gif"><div id="cnblogs_code_open_61e93e2c-5cd2-4c40-ac48-44a27f800685" class="cnblogs_code_hide"> <pre><span style="color: #008080"> 1</span> <span style="color: #000000">主窗口編輯按鈕單擊事件: </span><span style="color: #008080"> 2</span> <span style="color: #008080"> 3</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> btnEdit_Click(<span style="color: #0000ff">object</span><span style="color: #000000"> sender, EventArgs e) </span><span style="color: #008080"> 4</span> <span style="color: #008080"> 5</span> <span style="color: #000000">{ </span><span style="color: #008080"> 6</span> <span style="color: #008080"> 7</span> <span style="color: #008000">//</span><span style="color: #008000">獲取當前選擇表格的行索引</span> <span style="color: #008080"> 8</span> <span style="color: #008080"> 9</span> <span style="color: #0000ff">int</span> dgvIndex =<span style="color: #000000"> dgvStudents.CurrentRow.Index; </span><span style="color: #008080"> 10</span> <span style="color: #008080"> 11</span> <span style="color: #008000">//</span><span style="color: #008000">根據行索引獲取單元格的值</span> <span style="color: #008080"> 12</span> <span style="color: #008080"> 13</span> <span style="color: #0000ff">string</span> name = dgvStudents.Rows[dgvIndex].Cells[<span style="color: #800000">"</span><span style="color: #800000">姓名</span><span style="color: #800000">"</span><span style="color: #000000">].Value.ToString(); </span><span style="color: #008080"> 14</span> <span style="color: #008080"> 15</span> FrmStudentAddandEdit form = <span style="color: #0000ff">new</span> FrmStudentAddandEdit(<span style="color: #0000ff">false</span><span style="color: #000000">,name); </span><span style="color: #008080"> 16</span> <span style="color: #008080"> 17</span> <span style="color: #000000"> form.ShowDialog(); </span><span style="color: #008080"> 18</span> <span style="color: #008080"> 19</span> <span style="color: #000000">} </span><span style="color: #008080"> 20</span> <span style="color: #008080"> 21</span> <span style="color: #008080"> 22</span> <span style="color: #008080"> 23</span> <span style="color: #000000">窗體啟動事件進行判斷是添加還是編輯: </span><span style="color: #008080"> 24</span> <span style="color: #008080"> 25</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> FrmStudentAddandEdit_Load(<span style="color: #0000ff">object</span><span style="color: #000000"> sender, EventArgs e) </span><span style="color: #008080"> 26</span> <span style="color: #008080"> 27</span> <span style="color: #000000">{ </span><span style="color: #008080"> 28</span> <span style="color: #008080"> 29</span> <span style="color: #0000ff">if</span><span style="color: #000000"> (IsAdd) </span><span style="color: #008080"> 30</span> <span style="color: #008080"> 31</span> <span style="color: #000000"> { </span><span style="color: #008080"> 32</span> <span style="color: #008080"> 33</span> <span style="color: #0000ff">this</span>.Text = <span style="color: #800000">"</span><span style="color: #800000">添加學生</span><span style="color: #800000">"</span><span style="color: #000000">; </span><span style="color: #008080"> 34</span> <span style="color: #008080"> 35</span> <span style="color: #000000"> } </span><span style="color: #008080"> 36</span> <span style="color: #008080"> 37</span> <span style="color: #0000ff">else</span> <span style="color: #008080"> 38</span> <span style="color: #008080"> 39</span> <span style="color: #000000"> { </span><span style="color: #008080"> 40</span> <span style="color: #008080"> 41</span> <span style="color: #0000ff">this</span>.Text = <span style="color: #800000">"</span><span style="color: #800000">編輯學生名單</span><span style="color: #800000">"</span><span style="color: #000000">; </span><span style="color: #008080"> 42</span> <span style="color: #008080"> 43</span> XmlDocument doc = <span style="color: #0000ff">new</span><span style="color: #000000"> XmlDocument(); </span><span style="color: #008080"> 44</span> <span style="color: #008080"> 45</span> doc.Load(<span style="color: #800000">@"</span><span style="color: #800000">..//..//Students.xml</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080"> 46</span> <span style="color: #008080"> 47</span> XmlElement root =<span style="color: #000000"> doc.DocumentElement; </span><span style="color: #008080"> 48</span> <span style="color: #008080"> 49</span> <span style="color: #008000">//</span><span style="color: #008000">篩選出符合條件的標記</span> <span style="color: #008080"> 50</span> <span style="color: #008080"> 51</span> XmlElement selectEle = (XmlElement)root.SelectSingleNode(<span style="color: #800000">"</span><span style="color: #800000">/學生名單/學生[@Name='</span><span style="color: #800000">"</span> + Name + <span style="color: #800000">"</span><span style="color: #800000">']</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080"> 52</span> <span style="color: #008080"> 53</span> <span style="color: #008000">//</span><span style="color: #008000">篩選出符合條件并給控件賦值</span> <span style="color: #008080"> 54</span> <span style="color: #008080"> 55</span> txtName.Text = selectEle.GetAttribute(<span style="color: #800000">"</span><span style="color: #800000">Name</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080"> 56</span> <span style="color: #008080"> 57</span> txtClass.Text = selectEle.GetAttribute(<span style="color: #800000">"</span><span style="color: #800000">Class</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080"> 58</span> <span style="color: #008080"> 59</span> txtNumber.Text = selectEle.GetElementsByTagName(<span style="color: #800000">"</span><span style="color: #800000">學號</span><span style="color: #800000">"</span>)[<span style="color: #800080">0</span><span style="color: #000000">].InnerText; </span><span style="color: #008080"> 60</span> <span style="color: #008080"> 61</span> dtpBirthday.Text = selectEle.GetElementsByTagName(<span style="color: #800000">"</span><span style="color: #800000">生日</span><span style="color: #800000">"</span>)[<span style="color: #800080">0</span><span style="color: #000000">].InnerText; </span><span style="color: #008080"> 62</span> <span style="color: #008080"> 63</span> cmbSex.Text = selectEle.GetElementsByTagName(<span style="color: #800000">"</span><span style="color: #800000">性別</span><span style="color: #800000">"</span>)[<span style="color: #800080">0</span><span style="color: #000000">].InnerText; </span><span style="color: #008080"> 64</span> <span style="color: #008080"> 65</span> <span style="color: #000000"> } </span><span style="color: #008080"> 66</span> <span style="color: #008080"> 67</span> <span style="color: #000000">} </span><span style="color: #008080"> 68</span> <span style="color: #008080"> 69</span> <span style="color: #000000">同樣在編輯好內容后,單擊確定按鈕后判斷當前是添加狀態還是編輯狀態: </span><span style="color: #008080"> 70</span> <span style="color: #008080"> 71</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> btnOK_Click(<span style="color: #0000ff">object</span><span style="color: #000000"> sender, EventArgs e) </span><span style="color: #008080"> 72</span> <span style="color: #008080"> 73</span> <span style="color: #000000">{ </span><span style="color: #008080"> 74</span> <span style="color: #008080"> 75</span> <span style="color: #0000ff">if</span><span style="color: #000000"> (IsAdd) </span><span style="color: #008080"> 76</span> <span style="color: #008080"> 77</span> <span style="color: #000000"> { </span><span style="color: #008080"> 78</span> <span style="color: #008080"> 79</span> <span style="color: #008000">//</span><span style="color: #008000">添加記錄代碼….</span> <span style="color: #008080"> 80</span> <span style="color: #008080"> 81</span> <span style="color: #000000"> } </span><span style="color: #008080"> 82</span> <span style="color: #008080"> 83</span> <span style="color: #0000ff">else</span> <span style="color: #008080"> 84</span> <span style="color: #008080"> 85</span> <span style="color: #000000"> { </span><span style="color: #008080"> 86</span> <span style="color: #008080"> 87</span> <span style="color: #0000ff">this</span>.Text = <span style="color: #800000">"</span><span style="color: #800000">編輯學生名單</span><span style="color: #800000">"</span><span style="color: #000000">; </span><span style="color: #008080"> 88</span> <span style="color: #008080"> 89</span> XmlDocument doc = <span style="color: #0000ff">new</span><span style="color: #000000"> XmlDocument(); </span><span style="color: #008080"> 90</span> <span style="color: #008080"> 91</span> doc.Load(<span style="color: #800000">@"</span><span style="color: #800000">..//..//Students.xml</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080"> 92</span> <span style="color: #008080"> 93</span> XmlElement root =<span style="color: #000000"> doc.DocumentElement; </span><span style="color: #008080"> 94</span> <span style="color: #008080"> 95</span> <span style="color: #008000">//</span><span style="color: #008000">篩選出符合條件的標記</span> <span style="color: #008080"> 96</span> <span style="color: #008080"> 97</span> XmlElement selectEle = (XmlElement)root.SelectSingleNode(<span style="color: #800000">"</span><span style="color: #800000">/學生名單/學生[@Name='</span><span style="color: #800000">"</span> + Name + <span style="color: #800000">"</span><span style="color: #800000">']</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080"> 98</span> <span style="color: #008080"> 99</span> selectEle.SetAttribute(<span style="color: #800000">"</span><span style="color: #800000">Name</span><span style="color: #800000">"</span><span style="color: #000000">,txtName.Text.Trim()); </span><span style="color: #008080">100</span> <span style="color: #008080">101</span> selectEle.SetAttribute(<span style="color: #800000">"</span><span style="color: #800000">Class</span><span style="color: #800000">"</span><span style="color: #000000">,txtClass.Text.Trim()); </span><span style="color: #008080">102</span> <span style="color: #008080">103</span> <span style="color: #008000">//</span><span style="color: #008000">篩選出的對象賦給XMLElement對象</span> <span style="color: #008080">104</span> <span style="color: #008080">105</span> XmlElement eSex = (XmlElement)selectEle.GetElementsByTagName(<span style="color: #800000">"</span><span style="color: #800000">性別</span><span style="color: #800000">"</span>)[<span style="color: #800080">0</span><span style="color: #000000">]; </span><span style="color: #008080">106</span> <span style="color: #008080">107</span> eSex.InnerText =<span style="color: #000000"> cmbSex.Text; </span><span style="color: #008080">108</span> <span style="color: #008080">109</span> XmlElement eNumber = (XmlElement)selectEle.GetElementsByTagName(<span style="color: #800000">"</span><span style="color: #800000">學號</span><span style="color: #800000">"</span>)[<span style="color: #800080">0</span><span style="color: #000000">]; </span><span style="color: #008080">110</span> <span style="color: #008080">111</span> eNumber.InnerText =<span style="color: #000000"> txtNumber.Text; </span><span style="color: #008080">112</span> <span style="color: #008080">113</span> XmlElement eBirth = (XmlElement)selectEle.GetElementsByTagName(<span style="color: #800000">"</span><span style="color: #800000">生日</span><span style="color: #800000">"</span>)[<span style="color: #800080">0</span><span style="color: #000000">]; </span><span style="color: #008080">114</span> <span style="color: #008080">115</span> eBirth.InnerText =<span style="color: #000000"> dtpBirthday.Text; </span><span style="color: #008080">116</span> <span style="color: #008080">117</span> <span style="color: #008080">118</span> <span style="color: #008080">119</span> doc.Save(<span style="color: #800000">@"</span><span style="color: #800000">..//..//Students.xml</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080">120</span> <span style="color: #008080">121</span> MessageBox.Show(<span style="color: #800000">"</span><span style="color: #800000">修改成功!</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080">122</span> <span style="color: #008080">123</span> <span style="color: #008000">//</span><span style="color: #008000">這一步很重要一定要關閉窗口不然再次修改會出現不可預料的BUG</span> <span style="color: #008080">124</span> <span style="color: #008080">125</span> <span style="color: #0000ff">this</span><span style="color: #000000">.Close(); </span><span style="color: #008080">126</span> <span style="color: #008080">127</span> <span style="color: #000000"> } </span><span style="color: #008080">128</span> <span style="color: #008080">129</span> }</pre> </div> <span class="cnblogs_code_collapse">View Code</span> </div> <p>?</p> <p><span style="font-size: 18pt"><strong>刪除記錄</strong></span></p> <p><span style="font-size: 14px; line-height: 1.5">?</span>?<strong>也是一樣的邏輯獲取當前選擇表格的姓名字段,然后按照相關姓名進行刪除,如果姓名不是唯一字段,那么可以使用</strong><strong>ID</strong><strong>,每次添加一條記錄的時候最大</strong><strong>ID+1</strong><strong>。</strong></p> <div class="cnblogs_code"> <pre><span style="color: #008080"> 1</span> <span style="color: #008000">//</span><span style="color: #008000">獲取當前選擇表格的行索引及姓名</span> <span style="color: #008080"> 2</span> <span style="color: #008080"> 3</span> <span style="color: #0000ff">int</span> dgvIndex =<span style="color: #000000"> dgvStudents.CurrentRow.Index; </span><span style="color: #008080"> 4</span> <span style="color: #008080"> 5</span> <span style="color: #0000ff">string</span> name = dgvStudents.Rows[dgvIndex].Cells[<span style="color: #800000">"</span><span style="color: #800000">姓名</span><span style="color: #800000">"</span><span style="color: #000000">].Value.ToString(); </span><span style="color: #008080"> 6</span> <span style="color: #008080"> 7</span> <span style="color: #008000">//</span><span style="color: #008000">篩選出符合條件的標記</span> <span style="color: #008080"> 8</span> <span style="color: #008080"> 9</span> XmlElement selectEle = (XmlElement)root.SelectSingleNode(<span style="color: #800000">"</span><span style="color: #800000">/學生名單/學生[@Name='</span><span style="color: #800000">"</span> + Name + <span style="color: #800000">"</span><span style="color: #800000">']</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008080">10</span> <span style="color: #008080">11</span> <span style="color: #008000">//</span><span style="color: #008000">刪除指定子元素</span> <span style="color: #008080">12</span> <span style="color: #008080">13</span> <span style="color: #000000">root.RemoveChild(selectEle); </span><span style="color: #008080">14</span> <span style="color: #008080">15</span> <span style="color: #000000">doc.Save(“…”); </span><span style="color: #008080">16</span> <span style="color: #008080">17</span> <span style="color: #008000">//</span><span style="color: #008000">具體代碼省略。。。。。不會的同學可以回到上篇文章。</span></pre> </div> <p>?</p> <p><strong>上面介紹的操作方法是最常使用幾種的,可以使用</strong><strong>XML</strong><strong>來完成一個數據量比較小的軟件,或者可以用來做軟件的配置文件。</strong></p> Sun, 08 Nov 2020 21:46:59 +0800關于asp.net假分頁的刪除操作的隨筆http://kf.icp123.net/sjk/8735.html <p>作為一個新人,上周負責優化一個后臺管理系統,遇到一個問題:點擊刪除按鈕之后,頁面又回到了第一頁。</p> <p>而我需要達到的效果是:點擊了刪除按鈕之后,原來是那一頁,刪除后還是在那一頁。</p> <p><span style="text-decoration: underline">由于項目是已經驗收了的,所以我不能改動太大。</span>我在網上頁找了很久,也有許多解決方案,但都不太適合。</p> <p>這個系統數據不多,所以用的假分頁,使用了Repeater控件,后臺將數據庫中的數據全部查詢出來放到Repeater中,在通過jquery進行分頁,下面是我寫的一個簡單的測試項目</p> <p>前臺代碼如下:</p> <div class="cnblogs_code"> <pre>&lt;%@ Page Language=<span style="color: #800000">"</span><span style="color: #800000">C#</span><span style="color: #800000">"</span> AutoEventWireup=<span style="color: #800000">"</span><span style="color: #800000">true</span><span style="color: #800000">"</span> CodeBehind=<span style="color: #800000">"</span><span style="color: #800000">Index.aspx.cs</span><span style="color: #800000">"</span> Inherits=<span style="color: #800000">"</span><span style="color: #800000">PagingTestDome1.Index</span><span style="color: #800000">"</span> %&gt; &lt;!DOCTYPE html&gt; &lt;html xmlns=<span style="color: #800000">"</span><span style="color: #800000">http://www.w3.org/1999/xhtml</span><span style="color: #800000">"</span>&gt; &lt;head runat=<span style="color: #800000">"</span><span style="color: #800000">server</span><span style="color: #800000">"</span>&gt; &lt;meta http-equiv=<span style="color: #800000">"</span><span style="color: #800000">Content-Type</span><span style="color: #800000">"</span> content=<span style="color: #800000">"</span><span style="color: #800000">text/html; charset=utf-8</span><span style="color: #800000">"</span>/&gt; &lt;script src=<span style="color: #800000">"</span><span style="color: #800000">js/jquery-1.6.2.min.js</span><span style="color: #800000">"</span>&gt;&lt;/script&gt; &lt;title&gt;&lt;/title&gt; &lt;style type=<span style="color: #800000">"</span><span style="color: #800000">text/css</span><span style="color: #800000">"</span>&gt;<span style="color: #000000"> a{ text</span>-decoration:none;color:#<span style="color: #800080">686868</span><span style="color: #000000">; } a:hover{ color:#6287ef; } .page_set { </span><span style="color: #0000ff">float</span><span style="color: #000000">: left; margin</span>-<span style="color: #000000">top: 50px; } .table_content { text</span>-<span style="color: #000000">align: center; } .page_set a { border: </span><span style="color: #800080">0</span>.5px solid #<span style="color: #800080">999</span><span style="color: #000000">; padding: 1px 4px; } .page_set p { display: inline; } </span>&lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=<span style="color: #800000">"</span><span style="color: #800000">form1</span><span style="color: #800000">"</span> runat=<span style="color: #800000">"</span><span style="color: #800000">server</span><span style="color: #800000">"</span>&gt; &lt;div&gt; &lt;h3&gt;假分頁――刪除&lt;/h3&gt; &lt;asp:Repeater ID=<span style="color: #800000">"</span><span style="color: #800000">rp_news</span><span style="color: #800000">"</span> runat=<span style="color: #800000">"</span><span style="color: #800000">server</span><span style="color: #800000">"</span> OnItemDataBound=<span style="color: #800000">"</span><span style="color: #800000">rp_news_ItemDataBound</span><span style="color: #800000">"</span> OnItemCommand=<span style="color: #800000">"</span><span style="color: #800000">rp_news_ItemCommand</span><span style="color: #800000">"</span>&gt; &lt;HeaderTemplate&gt; &lt;table id=<span style="color: #800000">"</span><span style="color: #800000">tableinfo</span><span style="color: #800000">"</span> border=<span style="color: #800000">"</span><span style="color: #800000">1px</span><span style="color: #800000">"</span> style=<span style="color: #800000">"</span><span style="color: #800000">border-collapse: collapse;</span><span style="color: #800000">"</span> bordercolor=<span style="color: #800000">"</span><span style="color: #800000">#eeeeee</span><span style="color: #800000">"</span> cellpadding=<span style="color: #800000">"</span><span style="color: #800000">2px</span><span style="color: #800000">"</span> cellspacing=<span style="color: #800000">"</span><span style="color: #800000">0</span><span style="color: #800000">"</span>&gt; &lt;tr style=<span style="color: #800000">"</span><span style="color: #800000">height:22px; background-color:#eeeeee; text-align:center;</span><span style="color: #800000">"</span>&gt; &lt;th style=<span style="color: #800000">"</span><span style="color: #800000">width:300px; font-weight:normal;</span><span style="color: #800000">"</span>&gt;編號&lt;/th&gt; &lt;th style=<span style="color: #800000">"</span><span style="color: #800000">width:300px; font-weight:normal;</span><span style="color: #800000">"</span>&gt;商品名&lt;/th&gt; &lt;th style=<span style="color: #800000">"</span><span style="color: #800000">width:300px; font-weight:normal;</span><span style="color: #800000">"</span>&gt;價格&lt;/th&gt; &lt;th style=<span style="color: #800000">"</span><span style="color: #800000">width:80px; font-weight:normal;</span><span style="color: #800000">"</span>&gt;刪除&lt;/th&gt; &lt;/tr&gt; &lt;tbody id=<span style="color: #800000">"</span><span style="color: #800000">trId</span><span style="color: #800000">"</span>&gt; &lt;/HeaderTemplate&gt; &lt;ItemTemplate&gt; &lt;asp:Panel ID=<span style="color: #800000">"</span><span style="color: #800000">plItem</span><span style="color: #800000">"</span> runat=<span style="color: #800000">"</span><span style="color: #800000">server</span><span style="color: #800000">"</span>&gt; &lt;tr style=<span style="color: #800000">"</span><span style="color: #800000">height:17px; background-color:White; text-align:center;</span><span style="color: #800000">"</span>&gt; &lt;asp:Label ID=<span style="color: #800000">"</span><span style="color: #800000">lb_ID1</span><span style="color: #800000">"</span> runat=<span style="color: #800000">"</span><span style="color: #800000">server</span><span style="color: #800000">"</span> Text=<span style="color: #800000">'</span><span style="color: #800000">&lt;%# Eval("id")%&gt;</span><span style="color: #800000">'</span>&gt;&lt;/asp:Label&gt; &lt;td&gt;&lt;asp:Label ID=<span style="color: #800000">"</span><span style="color: #800000">lb_ID</span><span style="color: #800000">"</span> runat=<span style="color: #800000">"</span><span style="color: #800000">server</span><span style="color: #800000">"</span> Text=<span style="color: #800000">'</span><span style="color: #800000">&lt;%# Eval("id")%&gt;</span><span style="color: #800000">'</span>&gt;&lt;/asp:Label&gt;&lt;/td&gt; &lt;td&gt;&lt;asp:Label ID=<span style="color: #800000">"</span><span style="color: #800000">lb_name</span><span style="color: #800000">"</span> runat=<span style="color: #800000">"</span><span style="color: #800000">server</span><span style="color: #800000">"</span> Text=<span style="color: #800000">'</span><span style="color: #800000">&lt;%# Eval("goodsName")%&gt;</span><span style="color: #800000">'</span>&gt;&lt;/asp:Label&gt;&lt;/td&gt; &lt;td&gt;&lt;asp:Label ID=<span style="color: #800000">"</span><span style="color: #800000">lb_price</span><span style="color: #800000">"</span> runat=<span style="color: #800000">"</span><span style="color: #800000">server</span><span style="color: #800000">"</span> Text=<span style="color: #800000">'</span><span style="color: #800000">&lt;%# Eval("price")%&gt;</span><span style="color: #800000">'</span>&gt;&lt;/asp:Label&gt;&lt;/td&gt; &lt;td&gt;&lt;asp:ImageButton ID=<span style="color: #800000">"</span><span style="color: #800000">lbtDelete</span><span style="color: #800000">"</span> ImageUrl=<span style="color: #800000">"</span><span style="color: #800000">img/delete.png</span><span style="color: #800000">"</span> runat=<span style="color: #800000">"</span><span style="color: #800000">server</span><span style="color: #800000">"</span> CommandName=<span style="color: #800000">"</span><span style="color: #800000">Delete</span><span style="color: #800000">"</span> /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/asp:Panel&gt; &lt;/ItemTemplate&gt; &lt;FooterTemplate&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/FooterTemplate&gt; &lt;/asp:Repeater&gt; &lt;div <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">page_set</span><span style="color: #800000">"</span>&gt; &lt;p id=<span style="color: #800000">"</span><span style="color: #800000">pages</span><span style="color: #800000">"</span>&gt;&lt;/p&gt; &lt;p id=<span style="color: #800000">"</span><span style="color: #800000">sjzl</span><span style="color: #800000">"</span>&gt;&lt;/p&gt; &lt;a href=<span style="color: #800000">"</span><span style="color: #800000">#</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">btn_first</span><span style="color: #800000">"</span>&gt;首頁&lt;/a&gt; &lt;a href=<span style="color: #800000">"</span><span style="color: #800000">#</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">btn_previous</span><span style="color: #800000">"</span>&gt;上一頁&lt;/a&gt; &lt;a href=<span style="color: #800000">"</span><span style="color: #800000">#</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">btn_next</span><span style="color: #800000">"</span>&gt;下一頁&lt;/a&gt; &lt;a href=<span style="color: #800000">"</span><span style="color: #800000">#</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">btn_last</span><span style="color: #800000">"</span>&gt;尾頁&lt;/a&gt; &lt;p&gt;轉到 &lt;/p&gt; &lt;input type=<span style="color: #700000">“text”</span> style=<span style="color: #800000">"</span><span style="color: #800000">width:40px;height:16px;</span><span style="color: #800000">"</span> maxlength=<span style="color: #800000">"</span><span style="color: #800000">5</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">changePage</span><span style="color: #800000">"</span> /&gt; &lt;p&gt;頁 &lt;/p&gt; &lt;a href=<span style="color: #800000">"</span><span style="color: #800000">#</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">btn_change</span><span style="color: #800000">"</span>&gt;跳轉&lt;/a&gt; &lt;/div&gt; &lt;div <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">clear</span><span style="color: #800000">"</span>&gt;&lt;/div&gt; &lt;script type=<span style="color: #800000">"</span><span style="color: #800000">text/javascript</span><span style="color: #800000">"</span>&gt; <span style="color: #0000ff">var</span> pageSize = <span style="color: #800080">5</span>; <span style="color: #008000">//</span><span style="color: #008000">每頁顯示的記錄條數</span> <span style="color: #0000ff">var</span> curPage = <span style="color: #800080">0</span>; <span style="color: #008000">//</span><span style="color: #008000">當前頁</span> <span style="color: #0000ff">var</span> lastPage; <span style="color: #008000">//</span><span style="color: #008000">最后頁</span> <span style="color: #0000ff">var</span> direct = <span style="color: #800080">0</span>; <span style="color: #008000">//</span><span style="color: #008000">方向</span> <span style="color: #0000ff">var</span> len; <span style="color: #008000">//</span><span style="color: #008000">總行數</span> <span style="color: #0000ff">var</span> page; <span style="color: #008000">//</span><span style="color: #008000">總頁數</span> <span style="color: #0000ff">var</span><span style="color: #000000"> begin; </span><span style="color: #0000ff">var</span><span style="color: #000000"> end; </span><span style="color: #0000ff">var</span> cPage=<span style="color: #800080">0</span><span style="color: #000000">;     $(document).ready(function display() { len </span>= $(<span style="color: #800000">"</span><span style="color: #800000">#trId tr</span><span style="color: #800000">"</span>).length; <span style="color: #008000">//</span><span style="color: #008000"> 求這個表的總行數,剔除第一行介紹</span> page = len % pageSize == <span style="color: #800080">0</span> ? len / pageSize : Math.floor(len / pageSize) + <span style="color: #800080">1</span>; <span style="color: #008000">//</span><span style="color: #008000">根據記錄條數,計算頁數</span><span style="color: #000000"> curPage </span>= <span style="color: #800080">1</span>; <span style="color: #008000">//</span><span style="color: #008000"> 設置當前為第一頁</span> displayPage(<span style="color: #800080">1</span>); <span style="color: #008000">//</span><span style="color: #008000">顯示第一頁</span> <span style="color: #000000">document.getElementById(</span><span style="color: #800000">"</span><span style="color: #800000">pages</span><span style="color: #800000">"</span>).innerHTML = <span style="color: #800000">"</span><span style="color: #800000">當前 </span><span style="color: #800000">"</span> + curPage + <span style="color: #800000">"</span><span style="color: #800000">/</span><span style="color: #800000">"</span> + page + <span style="color: #800000">"</span><span style="color: #800000"> 頁 </span><span style="color: #800000">"</span>; <span style="color: #008000">//</span><span style="color: #008000"> 顯示當前多少頁</span> document.getElementById(<span style="color: #800000">"</span><span style="color: #800000">sjzl</span><span style="color: #800000">"</span>).innerHTML = <span style="color: #800000">"</span><span style="color: #800000">數據總量 </span><span style="color: #800000">"</span> + len + <span style="color: #800000">""</span>; <span style="color: #008000">//</span><span style="color: #008000"> 顯示數據量</span>        <span style="color: #000000">$(</span><span style="color: #800000">"</span><span style="color: #800000">#btn_first</span><span style="color: #800000">"</span>).click(function firstPage() { <span style="color: #008000">//</span><span style="color: #008000"> 首頁</span> curPage = <span style="color: #800080">1</span><span style="color: #000000">; direct </span>= <span style="color: #800080">0</span><span style="color: #000000">; displayPage();</span><span style="color: #000000"> }); $(</span><span style="color: #800000">"</span><span style="color: #800000">#btn_previous</span><span style="color: #800000">"</span>).click(function frontPage() { <span style="color: #008000">//</span><span style="color: #008000"> 上一頁</span> direct = -<span style="color: #800080">1</span><span style="color: #000000">; displayPage();</span><span style="color: #000000"> }); $(</span><span style="color: #800000">"</span><span style="color: #800000">#btn_next</span><span style="color: #800000">"</span>).click(function nextPage() { <span style="color: #008000">//</span><span style="color: #008000"> 下一頁</span> direct = <span style="color: #800080">1</span><span style="color: #000000">; displayPage();</span><span style="color: #000000"> }); $(</span><span style="color: #800000">"</span><span style="color: #800000">#btn_last</span><span style="color: #800000">"</span>).click(function lastPage() { <span style="color: #008000">//</span><span style="color: #008000"> 尾頁</span> curPage =<span style="color: #000000"> page; direct </span>= <span style="color: #800080">0</span><span style="color: #000000">; displayPage();</span><span style="color: #000000"> }); $(</span><span style="color: #800000">"</span><span style="color: #800000">#btn_change</span><span style="color: #800000">"</span>).click(function changePage() { <span style="color: #008000">//</span><span style="color: #008000"> 轉頁</span> curPage = document.getElementById(<span style="color: #800000">"</span><span style="color: #800000">changePage</span><span style="color: #800000">"</span>).value * <span style="color: #800080">1</span><span style="color: #000000">; </span><span style="color: #0000ff">if</span> (!/^[<span style="color: #800080">1</span>-<span style="color: #800080">9</span>]\d*$/<span style="color: #000000">.test(curPage)) { alert(</span><span style="color: #800000">"</span><span style="color: #800000">請輸入正整數</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">return</span><span style="color: #000000">; } </span><span style="color: #0000ff">if</span> (curPage &gt;<span style="color: #000000"> page) { alert(</span><span style="color: #800000">"</span><span style="color: #800000">超出數據頁面</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">return</span><span style="color: #000000">; } direct </span>= <span style="color: #800080">0</span><span style="color: #000000">; displayPage(); }); }); function displayPage() { </span><span style="color: #0000ff">if</span> (curPage &lt;= <span style="color: #800080">1</span> &amp;&amp; direct == -<span style="color: #800080">1</span><span style="color: #000000">) { direct </span>= <span style="color: #800080">0</span><span style="color: #000000">; alert(</span><span style="color: #800000">"</span><span style="color: #800000">已經是第一頁了</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">return</span><span style="color: #000000">; } </span><span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (curPage &gt;= page &amp;&amp; direct == <span style="color: #800080">1</span><span style="color: #000000">) { direct </span>= <span style="color: #800080">0</span><span style="color: #000000">; alert(</span><span style="color: #800000">"</span><span style="color: #800000">已經是最后一頁了</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">return</span><span style="color: #000000">; } lastPage </span>=<span style="color: #000000"> curPage; </span><span style="color: #008000">//</span><span style="color: #008000"> 修復當len=1時,curPage計算+得0的bug</span> <span style="color: #0000ff">if</span> (len &gt;<span style="color: #000000"> pageSize) { curPage </span>= ((curPage + direct + len) %<span style="color: #000000"> len); } </span><span style="color: #0000ff">else</span><span style="color: #000000"> { curPage </span>= <span style="color: #800080">1</span><span style="color: #000000">; } document.getElementById(</span><span style="color: #800000">"</span><span style="color: #800000">pages</span><span style="color: #800000">"</span>).innerHTML = <span style="color: #800000">"</span><span style="color: #800000">當前 </span><span style="color: #800000">"</span> + curPage + <span style="color: #800000">"</span><span style="color: #800000">/</span><span style="color: #800000">"</span> + page + <span style="color: #800000">"</span><span style="color: #800000"> 頁 </span><span style="color: #800000">"</span>; <span style="color: #008000">//</span><span style="color: #008000"> 顯示當前多少頁</span> <span style="color: #000000"> begin </span>= (curPage - <span style="color: #800080">1</span>) * pageSize; <span style="color: #008000">//</span><span style="color: #008000"> 起始記錄號</span> end = begin + <span style="color: #800080">1</span> * pageSize - <span style="color: #800080">1</span>; <span style="color: #008000">//</span><span style="color: #008000"> 末尾記錄號</span>          <span style="color: #0000ff">if</span> (end &gt; len) end =<span style="color: #000000"> len; $(</span><span style="color: #800000">"</span><span style="color: #800000">#trId tr</span><span style="color: #800000">"</span>).hide(); <span style="color: #008000">//</span><span style="color: #008000"> 首先,設置這行為隱藏</span> $(<span style="color: #800000">"</span><span style="color: #800000">#trId tr</span><span style="color: #800000">"</span>).each(function (i) { <span style="color: #008000">//</span><span style="color: #008000"> 然后,通過條件判斷決定本行是否恢復顯示</span> <span style="color: #0000ff">if</span> ((i &gt;= begin &amp;&amp; i &lt;= end))<span style="color: #008000">//</span><span style="color: #008000">顯示begin&lt;=x&lt;=end的記錄</span> $(<span style="color: #0000ff">this</span><span style="color: #000000">).show(); }); } </span>&lt;/script&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> </div> <p>后臺代碼如下:</p> <div class="cnblogs_code"> <pre><span style="color: #0000ff">using</span><span style="color: #000000"> System; </span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Collections.Generic; </span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Linq; </span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Web; </span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Web.UI; </span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Web.UI.WebControls; </span><span style="color: #0000ff">using</span><span style="color: #000000"> DAL; </span><span style="color: #0000ff">using</span><span style="color: #000000"> BLL; </span><span style="color: #0000ff">using</span><span style="color: #000000"> Model; </span><span style="color: #0000ff">namespace</span><span style="color: #000000"> PagingTestDome1 { </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">partial</span> <span style="color: #0000ff">class</span><span style="color: #000000"> Index : System.Web.UI.Page { </span><span style="color: #0000ff">protected</span> <span style="color: #0000ff">void</span> Page_Load(<span style="color: #0000ff">object</span><span style="color: #000000"> sender, EventArgs e) { </span><span style="color: #0000ff">if</span> (!<span style="color: #000000">IsPostBack) { Bind(); } } </span><span style="color: #0000ff">protected</span> <span style="color: #0000ff">void</span><span style="color: #000000"> Bind() { BLL.tbl_goods goods </span>= <span style="color: #0000ff">new</span><span style="color: #000000"> BLL.tbl_goods(); IList</span>&lt;Model.tbl_goods&gt; List=<span style="color: #0000ff">new</span> List&lt;Model.tbl_goods&gt;<span style="color: #000000">(); List </span>=<span style="color: #000000"> goods.GetList(); rp_news.DataSource </span>=<span style="color: #000000"> List; rp_news.DataBind(); } </span><span style="color: #0000ff">protected</span> <span style="color: #0000ff">void</span> rp_news_ItemDataBound(<span style="color: #0000ff">object</span><span style="color: #000000"> sender, RepeaterItemEventArgs e) { </span><span style="color: #0000ff">if</span> (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==<span style="color: #000000"> ListItemType.AlternatingItem) { Panel pn </span>= (Panel)e.Item.FindControl(<span style="color: #800000">"</span><span style="color: #800000">plItem</span><span style="color: #800000">"</span><span style="color: #000000">); Label id </span>= (Label)pn.FindControl(<span style="color: #800000">"</span><span style="color: #800000">lb_ID1</span><span style="color: #800000">"</span><span style="color: #000000">); Label name </span>= (Label)pn.FindControl(<span style="color: #800000">"</span><span style="color: #800000">lb_name</span><span style="color: #800000">"</span><span style="color: #000000">); id.Visible </span>= <span style="color: #0000ff">false</span><span style="color: #000000">; ((ImageButton)e.Item.FindControl(</span><span style="color: #800000">"</span><span style="color: #800000">lbtDelete</span><span style="color: #800000">"</span>)).Attributes.Add(<span style="color: #800000">"</span><span style="color: #800000">onclick</span><span style="color: #800000">"</span>, <span style="color: #0000ff">string</span>.Format(<span style="color: #800000">"</span><span style="color: #800000">JavaScript:return confirm('你確認要刪除 {0} 嗎');</span><span style="color: #800000">"</span><span style="color: #000000">, name.Text)); } } </span><span style="color: #0000ff">protected</span> <span style="color: #0000ff">void</span> rp_news_ItemCommand(<span style="color: #0000ff">object</span><span style="color: #000000"> source, RepeaterCommandEventArgs e) { </span><span style="color: #0000ff">if</span> (e.CommandName == <span style="color: #800000">"</span><span style="color: #800000">Delete</span><span style="color: #800000">"</span><span style="color: #000000">) { Panel pn </span>= (Panel)e.Item.FindControl(<span style="color: #800000">"</span><span style="color: #800000">plItem</span><span style="color: #800000">"</span><span style="color: #000000">); Label id </span>= (Label)pn.FindControl(<span style="color: #800000">"</span><span style="color: #800000">lb_ID1</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">int</span> ID =<span style="color: #000000"> Convert.ToInt32(id.Text); BLL.tbl_goods goods </span>= <span style="color: #0000ff">new</span><span style="color: #000000"> BLL.tbl_goods(); goods.Delete(ID); Response.Write(</span><span style="color: #800000">"</span><span style="color: #800000">&lt;script&gt;alert('刪除成功?。?!')&lt;/script&gt;</span><span style="color: #800000">"</span><span style="color: #000000">); Bind(); } } } }</span></pre> </div> <p>這里的刪除按鈕是用的ImageButton,這是一個提交按鈕,所以點擊了之后頁面會刷新。</p> <p>在不能改動太大的前提下,要達到我所需要的效果。</p> <p>有一個思路就是將當前的頁碼保存下來,點擊刪除按鈕后,通過jquery控制它跳到保存下來的頁碼的那一頁。</p> <p>但是在前臺頁碼中,分頁處的代碼都是html標簽,不具有保存頁碼的功能,所以就需要改下前臺的代碼了。</p> <p>將分頁處的input標簽:</p> <div class="cnblogs_code"> <pre>&lt;input type=<span style="color: #a81930">"text"</span> style=<span style="color: #800000">"</span><span style="color: #800000">width:40px;height:16px;</span><span style="color: #800000">"</span> maxlength=<span style="color: #800000">"</span><span style="color: #800000">5</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">changePage</span><span style="color: #800000">"</span> /&gt;</pre> </div> <p>改為TextBox:</p> <div class="cnblogs_code"> <pre>&lt;asp:TextBox ID=<span style="color: #800000">"</span><span style="color: #800000">changePage</span><span style="color: #800000">"</span> runat=<span style="color: #800000">"</span><span style="color: #800000">server</span><span style="color: #800000">"</span> Width=<span style="color: #800000">"</span><span style="color: #800000">40</span><span style="color: #800000">"</span> Height=<span style="color: #800000">"</span><span style="color: #800000">16</span><span style="color: #800000">"</span> MaxLength=<span style="color: #800000">"</span><span style="color: #800000">5</span><span style="color: #800000">"</span>&gt;&lt;/asp:TextBox&gt; </pre> </div> <p>用asp.net所帶的TextBox來保存頁碼就可以了。</p> <p>那么,分頁的jquery也需要改一下了:</p> <div class="cnblogs_code"> <pre>&lt;script type=<span style="color: #800000">"</span><span style="color: #800000">text/javascript</span><span style="color: #800000">"</span>&gt; <span style="color: #0000ff">var</span> pageSize = <span style="color: #800080">5</span>; <span style="color: #008000">//</span><span style="color: #008000">每頁顯示的記錄條數</span> <span style="color: #0000ff">var</span> curPage = <span style="color: #800080">0</span>; <span style="color: #008000">//</span><span style="color: #008000">當前頁</span> <span style="color: #0000ff">var</span> lastPage; <span style="color: #008000">//</span><span style="color: #008000">最后頁</span> <span style="color: #0000ff">var</span> direct = <span style="color: #800080">0</span>; <span style="color: #008000">//</span><span style="color: #008000">方向</span> <span style="color: #0000ff">var</span> len; <span style="color: #008000">//</span><span style="color: #008000">總行數</span> <span style="color: #0000ff">var</span> page; <span style="color: #008000">//</span><span style="color: #008000">總頁數</span> <span style="color: #0000ff">var</span><span style="color: #000000"> begin; </span><span style="color: #0000ff">var</span><span style="color: #000000"> end; </span><span style="color: #0000ff">var</span> cPage=<span style="color: #800080">0</span><span style="color: #000000">; $(document).ready(function display() { len </span>= $(<span style="color: #800000">"</span><span style="color: #800000">#trId tr</span><span style="color: #800000">"</span>).length; <span style="color: #008000">//</span><span style="color: #008000"> 求這個表的總行數,剔除第一行介紹</span> page = len % pageSize == <span style="color: #800080">0</span> ? len / pageSize : Math.floor(len / pageSize) + <span style="color: #800080">1</span>; <span style="color: #008000">//</span><span style="color: #008000">根據記錄條數,計算頁數</span> <span style="color: #0000ff">if</span> (document.getElementById(<span style="color: #800000">"</span><span style="color: #800000">changePage</span><span style="color: #800000">"</span>).value * <span style="color: #800080">1</span> == <span style="color: #800000">""</span><span style="color: #000000">) { curPage </span>= <span style="color: #800080">1</span>; <span style="color: #008000">//</span><span style="color: #008000"> 設置當前為第一頁</span> displayPage(<span style="color: #800080">1</span>); <span style="color: #008000">//</span><span style="color: #008000">顯示第一頁</span> } <span style="color: #0000ff">else</span><span style="color: #000000"> { curPage </span>= document.getElementById(<span style="color: #800000">"</span><span style="color: #800000">changePage</span><span style="color: #800000">"</span>).value * <span style="color: #800080">1</span><span style="color: #000000">; displayPage(curPage);</span><span style="color: #008000">//</span><span style="color: #008000">跳轉到保存的頁碼處</span> <span style="color: #000000"> } document.getElementById(</span><span style="color: #800000">"</span><span style="color: #800000">pages</span><span style="color: #800000">"</span>).innerHTML = <span style="color: #800000">"</span><span style="color: #800000">當前 </span><span style="color: #800000">"</span> + curPage + <span style="color: #800000">"</span><span style="color: #800000">/</span><span style="color: #800000">"</span> + page + <span style="color: #800000">"</span><span style="color: #800000"> 頁 </span><span style="color: #800000">"</span>; <span style="color: #008000">//</span><span style="color: #008000"> 顯示當前多少頁</span> document.getElementById(<span style="color: #800000">"</span><span style="color: #800000">sjzl</span><span style="color: #800000">"</span>).innerHTML = <span style="color: #800000">"</span><span style="color: #800000">數據總量 </span><span style="color: #800000">"</span> + len + <span style="color: #800000">""</span>; <span style="color: #008000">//</span><span style="color: #008000"> 顯示數據量</span> <span style="color: #000000"> $(</span><span style="color: #800000">"</span><span style="color: #800000">#btn_first</span><span style="color: #800000">"</span>).click(function firstPage() { <span style="color: #008000">//</span><span style="color: #008000"> 首頁</span> curPage = <span style="color: #800080">1</span><span style="color: #000000">; direct </span>= <span style="color: #800080">0</span><span style="color: #000000">; displayPage(); $(</span><span style="color: #800000">"</span><span style="color: #800000">#changePage</span><span style="color: #800000">"</span>).val(curPage);<span style="color: #008000">//</span><span style="color: #008000">保存頁碼</span> <span style="color: #000000"> }); $(</span><span style="color: #800000">"</span><span style="color: #800000">#btn_previous</span><span style="color: #800000">"</span>).click(function frontPage() { <span style="color: #008000">//</span><span style="color: #008000"> 上一頁</span> direct = -<span style="color: #800080">1</span><span style="color: #000000">; displayPage(); $(</span><span style="color: #800000">"</span><span style="color: #800000">#changePage</span><span style="color: #800000">"</span>).val(curPage);<span style="color: #008000">//</span><span style="color: #008000">保存頁碼</span> <span style="color: #000000"> }); $(</span><span style="color: #800000">"</span><span style="color: #800000">#btn_next</span><span style="color: #800000">"</span>).click(function nextPage() { <span style="color: #008000">//</span><span style="color: #008000"> 下一頁</span> direct = <span style="color: #800080">1</span><span style="color: #000000">; displayPage(); $(</span><span style="color: #800000">"</span><span style="color: #800000">#changePage</span><span style="color: #800000">"</span>).val(curPage);<span style="color: #008000">//</span><span style="color: #008000">保存頁碼</span> <span style="color: #000000"> }); $(</span><span style="color: #800000">"</span><span style="color: #800000">#btn_last</span><span style="color: #800000">"</span>).click(function lastPage() { <span style="color: #008000">//</span><span style="color: #008000"> 尾頁</span> curPage =<span style="color: #000000"> page; direct </span>= <span style="color: #800080">0</span><span style="color: #000000">; displayPage(); $(</span><span style="color: #800000">"</span><span style="color: #800000">#changePage</span><span style="color: #800000">"</span>).val(curPage);<span style="color: #008000">//</span><span style="color: #008000">保存頁碼</span> <span style="color: #000000"> }); $(</span><span style="color: #800000">"</span><span style="color: #800000">#btn_change</span><span style="color: #800000">"</span>).click(function changePage() { <span style="color: #008000">//</span><span style="color: #008000"> 轉頁</span> curPage = document.getElementById(<span style="color: #800000">"</span><span style="color: #800000">changePage</span><span style="color: #800000">"</span>).value * <span style="color: #800080">1</span><span style="color: #000000">; </span><span style="color: #0000ff">if</span> (!/^[<span style="color: #800080">1</span>-<span style="color: #800080">9</span>]\d*$/<span style="color: #000000">.test(curPage)) { alert(</span><span style="color: #800000">"</span><span style="color: #800000">請輸入正整數</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">return</span><span style="color: #000000">; } </span><span style="color: #0000ff">if</span> (curPage &gt;<span style="color: #000000"> page) { alert(</span><span style="color: #800000">"</span><span style="color: #800000">超出數據頁面</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">return</span><span style="color: #000000">; } direct </span>= <span style="color: #800080">0</span><span style="color: #000000">; displayPage(); }); }); function displayPage() { </span><span style="color: #0000ff">if</span> (curPage &lt;= <span style="color: #800080">1</span> &amp;&amp; direct == -<span style="color: #800080">1</span><span style="color: #000000">) { direct </span>= <span style="color: #800080">0</span><span style="color: #000000">; alert(</span><span style="color: #800000">"</span><span style="color: #800000">已經是第一頁了</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">return</span><span style="color: #000000">; } </span><span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (curPage &gt;= page &amp;&amp; direct == <span style="color: #800080">1</span><span style="color: #000000">) { direct </span>= <span style="color: #800080">0</span><span style="color: #000000">; alert(</span><span style="color: #800000">"</span><span style="color: #800000">已經是最后一頁了</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">return</span><span style="color: #000000">; } lastPage </span>=<span style="color: #000000"> curPage; </span><span style="color: #008000">//</span><span style="color: #008000"> 修復當len=1時,curPage計算+得0的bug</span> <span style="color: #0000ff">if</span> (len &gt;<span style="color: #000000"> pageSize) { curPage </span>= ((curPage + direct + len) %<span style="color: #000000"> len); } </span><span style="color: #0000ff">else</span><span style="color: #000000"> { curPage </span>= <span style="color: #800080">1</span><span style="color: #000000">; } document.getElementById(</span><span style="color: #800000">"</span><span style="color: #800000">pages</span><span style="color: #800000">"</span>).innerHTML = <span style="color: #800000">"</span><span style="color: #800000">當前 </span><span style="color: #800000">"</span> + curPage + <span style="color: #800000">"</span><span style="color: #800000">/</span><span style="color: #800000">"</span> + page + <span style="color: #800000">"</span><span style="color: #800000"> 頁 </span><span style="color: #800000">"</span>; <span style="color: #008000">//</span><span style="color: #008000"> 顯示當前多少頁</span> <span style="color: #000000"> begin </span>= (curPage - <span style="color: #800080">1</span>) * pageSize; <span style="color: #008000">//</span><span style="color: #008000"> 起始記錄號</span> end = begin + <span style="color: #800080">1</span> * pageSize - <span style="color: #800080">1</span>; <span style="color: #008000">//</span><span style="color: #008000"> 末尾記錄號</span> <span style="color: #0000ff">if</span> (end &gt; len) end =<span style="color: #000000"> len; $(</span><span style="color: #800000">"</span><span style="color: #800000">#trId tr</span><span style="color: #800000">"</span>).hide(); <span style="color: #008000">//</span><span style="color: #008000"> 首先,設置這行為隱藏</span> $(<span style="color: #800000">"</span><span style="color: #800000">#trId tr</span><span style="color: #800000">"</span>).each(function (i) { <span style="color: #008000">//</span><span style="color: #008000"> 然后,通過條件判斷決定本行是否恢復顯示</span> <span style="color: #0000ff">if</span> ((i &gt;= begin &amp;&amp; i &lt;= end))<span style="color: #008000">//</span><span style="color: #008000">顯示begin&lt;=x&lt;=end的記錄</span> $(<span style="color: #0000ff">this</span><span style="color: #000000">).show(); }); } </span>&lt;/script&gt;</pre> </div> <p>好了,這樣差不多就達到了我所需要的效果了。</p> <p>結語:我是一個小菜鳥,第一次寫隨筆,歡迎大神指點,不喜勿噴。</p> <p>?</p> Sun, 08 Nov 2020 21:46:53 +0800多線程(5)async&amp;amp;amp;awaithttp://kf.icp123.net/sjk/8734.html <p>  .net 4.0的Task已經讓我們可以非常簡單地使用多線程,并且可以有返回值,也可以支持線程的取消等操作,可謂已經很強大了。但.net 4.5為我們帶來了async&amp;await,使得實現多線程的寫法更簡單,更優美,更符合線性思維。</p> <p>下面通過一個例子來演示通過Task和async&amp;await分別如何實現,并且最后還附上代碼執行順序圖。</p> <h2>使用Task實現</h2> <p>如下代碼:</p> <div class="cnblogs_code"> <pre><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">#region</span> 使用Task實現 <span style="color: #008080;"> 2</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> TestByTask() </span><span style="color: #008080;"> 3</span> <span style="color: #000000;">{ </span><span style="color: #008080;"> 4</span> Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">main thread start,current thread id:</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Thread.CurrentThread.ManagedThreadId); </span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">var</span> task = Task.Factory.StartNew&lt;<span style="color: #0000ff;">string</span>&gt;(() =&gt; <span style="color: #008080;"> 6</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 7</span> <span style="color: #0000ff;">return</span><span style="color: #000000;"> GetNameByTask(); </span><span style="color: #008080;"> 8</span> <span style="color: #000000;"> }); </span><span style="color: #008080;"> 9</span> Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">get another thread result,result:</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> task.Result); </span><span style="color: #008080;">10</span> Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">main thread completed!</span><span style="color: #800000;">"</span><span style="color: #000000;">); </span><span style="color: #008080;">11</span> <span style="color: #000000;">} </span><span style="color: #008080;">12</span> <span style="color: #008080;">13</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> GetNameByTask() </span><span style="color: #008080;">14</span> <span style="color: #000000;">{ </span><span style="color: #008080;">15</span> Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">another thread start,current thread id:</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Thread.CurrentThread.ManagedThreadId); </span><span style="color: #008080;">16</span> <span style="color: #0000ff;">return</span> <span style="color: #800000;">"</span><span style="color: #800000;">mcgrady</span><span style="color: #800000;">"</span><span style="color: #000000;">; </span><span style="color: #008080;">17</span> <span style="color: #000000;">} </span><span style="color: #008080;">18</span> <span style="color: #0000ff;">#endregion</span></pre> </div> <p>?</p> <p>輸出結果:</p> <p><img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214649160484320960715.png"></p> <p>?</p> <h2>使用async&amp;await實現</h2> <p>?假如使用async&amp;await如何實現呢,如下代碼:</p> <div class="cnblogs_code"> <pre><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">#region</span> 使用async&amp;await實現 <span style="color: #008080;"> 2</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">async</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> TestByAsyncAwait() </span><span style="color: #008080;"> 3</span> <span style="color: #000000;">{ </span><span style="color: #008080;"> 4</span> Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">main thread start,current thread id:</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Thread.CurrentThread.ManagedThreadId); </span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">var</span> name =<span style="color: #000000;"> GetNameByAsyncAwait(); </span><span style="color: #008080;"> 6</span> <span style="color: #008080;"> 7</span> Console.WriteLine(<span style="color: #0000ff;">string</span>.Format(<span style="color: #800000;">"</span><span style="color: #800000;">get another thread result,result:{0}</span><span style="color: #800000;">"</span>, <span style="color: #0000ff;">await</span><span style="color: #000000;"> name)); </span><span style="color: #008080;"> 8</span> Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">main thread completed!</span><span style="color: #800000;">"</span><span style="color: #000000;">); </span><span style="color: #008080;"> 9</span> <span style="color: #000000;">} </span><span style="color: #008080;">10</span> <span style="color: #008080;">11</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">async</span> Task&lt;<span style="color: #0000ff;">string</span>&gt;<span style="color: #000000;"> GetNameByAsyncAwait() </span><span style="color: #008080;">12</span> <span style="color: #000000;">{ </span><span style="color: #008080;">13</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">await</span> Task.Factory.StartNew&lt;<span style="color: #0000ff;">string</span>&gt;(() =&gt; <span style="color: #008080;">14</span> <span style="color: #000000;"> { </span><span style="color: #008080;">15</span> Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">another thread start,current thread id:</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Thread.CurrentThread.ManagedThreadId); </span><span style="color: #008080;">16</span> <span style="color: #0000ff;">return</span> <span style="color: #800000;">"</span><span style="color: #800000;">mcgrady</span><span style="color: #800000;">"</span><span style="color: #000000;">; </span><span style="color: #008080;">17</span> <span style="color: #000000;"> }); </span><span style="color: #008080;">18</span> <span style="color: #000000;">} </span><span style="color: #008080;">19</span> <span style="color: #0000ff;">#endregion</span></pre> </div> <p>?</p> <p>輸出結果:</p> <p><img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214649160484320928783.png"></p> <p>輸出結果跟使用Task相同。</p> <p>?</p> <p>代碼執行流程如下圖:</p> <p><img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214650160484321025873.png"></p> <p>?</p> <p>完整代碼:</p> <div class="cnblogs_code"> <img id="code_img_closed_db1b4c0c-9cf5-4cda-9f30-a81deb85f0e1" class="code_img_closed" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214650160484321067776.gif"><img id="code_img_opened_db1b4c0c-9cf5-4cda-9f30-a81deb85f0e1" class="code_img_opened" style="display: none;" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214651160484321138509.gif"><div id="cnblogs_code_open_db1b4c0c-9cf5-4cda-9f30-a81deb85f0e1" class="cnblogs_code_hide"> <pre><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">namespace</span><span style="color: #000000;"> ConsoleApplication25 </span><span style="color: #008080;"> 2</span> <span style="color: #000000;">{ </span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Program </span><span style="color: #008080;"> 4</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args) </span><span style="color: #008080;"> 6</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 7</span> <span style="color: #008000;">//</span><span style="color: #008000;">1,使用task實現 </span><span style="color: #008080;"> 8</span> <span style="color: #008000;">//</span><span style="color: #008000;">TestByTask(); </span><span style="color: #008080;"> 9</span> <span style="color: #008080;">10</span> <span style="color: #008000;">//</span><span style="color: #008000;">2,使用async&amp;await實現</span> <span style="color: #008080;">11</span> <span style="color: #000000;"> TestByAsyncAwait(); </span><span style="color: #008080;">12</span> <span style="color: #008080;">13</span> <span style="color: #000000;"> Console.ReadKey(); </span><span style="color: #008080;">14</span> <span style="color: #000000;"> } </span><span style="color: #008080;">15</span> <span style="color: #008080;">16</span> <span style="color: #0000ff;">#region</span> 使用Task實現 <span style="color: #008080;">17</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> TestByTask() </span><span style="color: #008080;">18</span> <span style="color: #000000;"> { </span><span style="color: #008080;">19</span> Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">main thread start,current thread id:</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Thread.CurrentThread.ManagedThreadId); </span><span style="color: #008080;">20</span> <span style="color: #0000ff;">var</span> task = Task.Factory.StartNew&lt;<span style="color: #0000ff;">string</span>&gt;(() =&gt; <span style="color: #008080;">21</span> <span style="color: #000000;"> { </span><span style="color: #008080;">22</span> <span style="color: #0000ff;">return</span><span style="color: #000000;"> GetNameByTask(); </span><span style="color: #008080;">23</span> <span style="color: #000000;"> }); </span><span style="color: #008080;">24</span> Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">get another thread result,result:</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> task.Result); </span><span style="color: #008080;">25</span> Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">main thread completed!</span><span style="color: #800000;">"</span><span style="color: #000000;">); </span><span style="color: #008080;">26</span> <span style="color: #000000;"> } </span><span style="color: #008080;">27</span> <span style="color: #008080;">28</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> GetNameByTask() </span><span style="color: #008080;">29</span> <span style="color: #000000;"> { </span><span style="color: #008080;">30</span> Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">another thread start,current thread id:</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Thread.CurrentThread.ManagedThreadId); </span><span style="color: #008080;">31</span> <span style="color: #0000ff;">return</span> <span style="color: #800000;">"</span><span style="color: #800000;">mcgrady</span><span style="color: #800000;">"</span><span style="color: #000000;">; </span><span style="color: #008080;">32</span> <span style="color: #000000;"> } </span><span style="color: #008080;">33</span> <span style="color: #0000ff;">#endregion</span> <span style="color: #008080;">34</span> <span style="color: #008080;">35</span> <span style="color: #0000ff;">#region</span> 使用async&amp;await實現 <span style="color: #008080;">36</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">async</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> TestByAsyncAwait() </span><span style="color: #008080;">37</span> <span style="color: #000000;"> { </span><span style="color: #008080;">38</span> Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">main thread start,current thread id:</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Thread.CurrentThread.ManagedThreadId); </span><span style="color: #008080;">39</span> <span style="color: #0000ff;">var</span> name =<span style="color: #000000;"> GetNameByAsyncAwait(); </span><span style="color: #008080;">40</span> <span style="color: #008080;">41</span> Console.WriteLine(<span style="color: #0000ff;">string</span>.Format(<span style="color: #800000;">"</span><span style="color: #800000;">get another thread result,result:{0}</span><span style="color: #800000;">"</span>, <span style="color: #0000ff;">await</span><span style="color: #000000;"> name)); </span><span style="color: #008080;">42</span> Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">main thread completed!</span><span style="color: #800000;">"</span><span style="color: #000000;">); </span><span style="color: #008080;">43</span> <span style="color: #000000;"> } </span><span style="color: #008080;">44</span> <span style="color: #008080;">45</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">async</span> Task&lt;<span style="color: #0000ff;">string</span>&gt;<span style="color: #000000;"> GetNameByAsyncAwait() </span><span style="color: #008080;">46</span> <span style="color: #000000;"> { </span><span style="color: #008080;">47</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">await</span> Task.Factory.StartNew&lt;<span style="color: #0000ff;">string</span>&gt;(() =&gt; <span style="color: #008080;">48</span> <span style="color: #000000;"> { </span><span style="color: #008080;">49</span> Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">another thread start,current thread id:</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Thread.CurrentThread.ManagedThreadId); </span><span style="color: #008080;">50</span> <span style="color: #0000ff;">return</span> <span style="color: #800000;">"</span><span style="color: #800000;">mcgrady</span><span style="color: #800000;">"</span><span style="color: #000000;">; </span><span style="color: #008080;">51</span> <span style="color: #000000;"> }); </span><span style="color: #008080;">52</span> <span style="color: #000000;"> } </span><span style="color: #008080;">53</span> <span style="color: #0000ff;">#endregion</span> <span style="color: #008080;">54</span> <span style="color: #000000;"> } </span><span style="color: #008080;">55</span> }</pre> </div> <span class="cnblogs_code_collapse">View Code</span> </div> <p>?</p> Sun, 08 Nov 2020 21:46:51 +0800多線程(7)多線程中的異常處理http://kf.icp123.net/sjk/8733.html <p>  多線程使用過程中,除了線程同步的問題要考慮外,異常處理也是經常要面對的事情。</p> <h2>默認主線程捕獲不到異步線程的異常</h2> <p>如下代碼:</p> <p><img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214636160484319624893.png"></p> <div class="cnblogs_code"> <img id="code_img_closed_ee4fab9b-8d64-444e-9a91-990eaf97889a" class="code_img_closed" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214637160484319726637.gif"><img id="code_img_opened_ee4fab9b-8d64-444e-9a91-990eaf97889a" class="code_img_opened" style="display: none;" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214637160484319774144.gif"><div id="cnblogs_code_open_ee4fab9b-8d64-444e-9a91-990eaf97889a" class="cnblogs_code_hide"> <pre><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">namespace</span><span style="color: #000000;"> ConsoleApplication29 </span><span style="color: #008080;"> 2</span> <span style="color: #000000;">{ </span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Program </span><span style="color: #008080;"> 4</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args) </span><span style="color: #008080;"> 6</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 7</span> <span style="color: #008000;">//</span><span style="color: #008000;">異步線程的異常處理</span> <span style="color: #008080;"> 8</span> <span style="color: #0000ff;">try</span> <span style="color: #008080;"> 9</span> <span style="color: #000000;"> { </span><span style="color: #008080;">10</span> Task.Factory.StartNew(() =&gt; <span style="color: #008080;">11</span> <span style="color: #000000;"> { </span><span style="color: #008080;">12</span> <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> Exception(<span style="color: #800000;">"</span><span style="color: #800000;">異步線和發生異常了!</span><span style="color: #800000;">"</span><span style="color: #000000;">); </span><span style="color: #008080;">13</span> <span style="color: #000000;"> }); </span><span style="color: #008080;">14</span> <span style="color: #000000;"> } </span><span style="color: #008080;">15</span> <span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception ex) </span><span style="color: #008080;">16</span> <span style="color: #000000;"> { </span><span style="color: #008080;">17</span> <span style="color: #008000;">//</span><span style="color: #008000;">這里是捕獲不到的</span> <span style="color: #008080;">18</span> <span style="color: #000000;"> Console.WriteLine(ex.ToString()); </span><span style="color: #008080;">19</span> <span style="color: #000000;"> } </span><span style="color: #008080;">20</span> <span style="color: #008080;">21</span> <span style="color: #000000;"> Console.ReadKey(); </span><span style="color: #008080;">22</span> <span style="color: #000000;"> } </span><span style="color: #008080;">23</span> <span style="color: #000000;"> } </span><span style="color: #008080;">24</span> }</pre> </div> <span class="cnblogs_code_collapse">View Code</span> </div> <h2>常用的異常處理方法</h2> <p><strong>1,在異步線程內部使用try/catch</strong></p> <p>如下代碼:</p> <p><img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214639160484319991496.png"></p> <div class="cnblogs_code"> <img id="code_img_closed_e700285a-f57c-4358-b722-3fab40224dfb" class="code_img_closed" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214639160484319940730.gif"><img id="code_img_opened_e700285a-f57c-4358-b722-3fab40224dfb" class="code_img_opened" style="display: none;" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214640160484320046123.gif"><div id="cnblogs_code_open_e700285a-f57c-4358-b722-3fab40224dfb" class="cnblogs_code_hide"> <pre><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">namespace</span><span style="color: #000000;"> ConsoleApplication29 </span><span style="color: #008080;"> 2</span> <span style="color: #000000;">{ </span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Program </span><span style="color: #008080;"> 4</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args) </span><span style="color: #008080;"> 6</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 7</span> <span style="color: #008000;">//</span><span style="color: #008000;">異步線程的異常處理 </span><span style="color: #008080;"> 8</span> <span style="color: #008000;">//</span><span style="color: #008000;">1,在異步線程內部使用try/catch</span> <span style="color: #008080;"> 9</span> Task.Factory.StartNew(() =&gt; <span style="color: #008080;">10</span> <span style="color: #000000;"> { </span><span style="color: #008080;">11</span> <span style="color: #0000ff;">try</span> <span style="color: #008080;">12</span> <span style="color: #000000;"> { </span><span style="color: #008080;">13</span> <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> Exception(<span style="color: #800000;">"</span><span style="color: #800000;">異步線和發生異常了!</span><span style="color: #800000;">"</span><span style="color: #000000;">); </span><span style="color: #008080;">14</span> <span style="color: #000000;"> } </span><span style="color: #008080;">15</span> <span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception ex) </span><span style="color: #008080;">16</span> <span style="color: #000000;"> { </span><span style="color: #008080;">17</span> <span style="color: #000000;"> Console.WriteLine(ex.ToString()); </span><span style="color: #008080;">18</span> <span style="color: #000000;"> } </span><span style="color: #008080;">19</span> <span style="color: #000000;"> }); </span><span style="color: #008080;">20</span> <span style="color: #008080;">21</span> <span style="color: #000000;"> Console.ReadKey(); </span><span style="color: #008080;">22</span> <span style="color: #000000;"> } </span><span style="color: #008080;">23</span> <span style="color: #000000;"> } </span><span style="color: #008080;">24</span> }</pre> </div> <span class="cnblogs_code_collapse">View Code</span> </div> <p>?運行結果:</p> <p><img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214640160484320068809.png"></p> <p><strong>2,調用Task的Wait方法</strong></p> <p>?如下代碼:</p> <p><img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214641160484320144609.png"></p> <p><span style="color: #ff0000;"><strong>注意:</strong></span></p> <p><span style="color: #ff0000;"><strong>除了調用Task的Wait方法后,在主線程可以捕獲異常外,對于有返回值的Task,只要接收了它的返回值就不再需要調用Wait方法了。</strong></span></p> <div class="cnblogs_code"> <img id="code_img_closed_0e897db4-a6dc-43c4-87c6-a373f73866f5" class="code_img_closed" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214641160484320163258.gif"><img id="code_img_opened_0e897db4-a6dc-43c4-87c6-a373f73866f5" class="code_img_opened" style="display: none;" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214642160484320244640.gif"><div id="cnblogs_code_open_0e897db4-a6dc-43c4-87c6-a373f73866f5" class="cnblogs_code_hide"> <pre><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">namespace</span><span style="color: #000000;"> ConsoleApplication29 </span><span style="color: #008080;"> 2</span> <span style="color: #000000;">{ </span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Program </span><span style="color: #008080;"> 4</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args) </span><span style="color: #008080;"> 6</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 7</span> <span style="color: #008000;">//</span><span style="color: #008000;">異步線程的異常處理 </span><span style="color: #008080;"> 8</span> <span style="color: #008000;">//</span><span style="color: #008000;">2,調用Task的Wait方法</span> <span style="color: #008080;"> 9</span> <span style="color: #0000ff;">try</span> <span style="color: #008080;">10</span> <span style="color: #000000;"> { </span><span style="color: #008080;">11</span> <span style="color: #0000ff;">var</span> task = Task.Factory.StartNew(() =&gt; <span style="color: #008080;">12</span> <span style="color: #000000;"> { </span><span style="color: #008080;">13</span> <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> Exception(<span style="color: #800000;">"</span><span style="color: #800000;">異步線和發生異常了!</span><span style="color: #800000;">"</span><span style="color: #000000;">); </span><span style="color: #008080;">14</span> <span style="color: #000000;"> }); </span><span style="color: #008080;">15</span> <span style="color: #000000;"> task.Wait(); </span><span style="color: #008080;">16</span> <span style="color: #000000;"> } </span><span style="color: #008080;">17</span> <span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception ex) </span><span style="color: #008080;">18</span> <span style="color: #000000;"> { </span><span style="color: #008080;">19</span> <span style="color: #000000;"> Console.WriteLine(ex.ToString()); </span><span style="color: #008080;">20</span> <span style="color: #000000;"> } </span><span style="color: #008080;">21</span> <span style="color: #008080;">22</span> <span style="color: #000000;"> Console.ReadKey(); </span><span style="color: #008080;">23</span> <span style="color: #000000;"> } </span><span style="color: #008080;">24</span> <span style="color: #000000;"> } </span><span style="color: #008080;">25</span> }</pre> </div> <span class="cnblogs_code_collapse">View Code</span> </div> <p>?</p> <p>運行結果:</p> <p><img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214642160484320265173.png"></p> <p><strong>3,在Task的ContinueWith方法中讀取Task的Exception屬性</strong></p> <p>如下代碼:</p> <p><img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214643160484320342292.png"></p> <div class="cnblogs_code"> <img id="code_img_closed_a508774e-a9a1-4148-97b9-0267493bbbd7" class="code_img_closed" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214643160484320387432.gif"><img id="code_img_opened_a508774e-a9a1-4148-97b9-0267493bbbd7" class="code_img_opened" style="display: none;" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214644160484320466512.gif"><div id="cnblogs_code_open_a508774e-a9a1-4148-97b9-0267493bbbd7" class="cnblogs_code_hide"> <pre><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">namespace</span><span style="color: #000000;"> ConsoleApplication29 </span><span style="color: #008080;"> 2</span> <span style="color: #000000;">{ </span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Program </span><span style="color: #008080;"> 4</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args) </span><span style="color: #008080;"> 6</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 7</span> <span style="color: #008000;">//</span><span style="color: #008000;">異步線程的異常處理 </span><span style="color: #008080;"> 8</span> <span style="color: #008000;">//</span><span style="color: #008000;">3,在Task的ContinueWith方法中讀取Task的Exception屬性</span> <span style="color: #008080;"> 9</span> Task.Factory.StartNew(() =&gt; <span style="color: #008080;">10</span> <span style="color: #000000;"> { </span><span style="color: #008080;">11</span> <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> Exception(<span style="color: #800000;">"</span><span style="color: #800000;">異步線和發生異常了!</span><span style="color: #800000;">"</span><span style="color: #000000;">); </span><span style="color: #008080;">12</span> }).ContinueWith(t =&gt; <span style="color: #008080;">13</span> <span style="color: #000000;"> { </span><span style="color: #008080;">14</span> <span style="color: #0000ff;">if</span><span style="color: #000000;"> (t.IsFaulted) </span><span style="color: #008080;">15</span> <span style="color: #000000;"> { </span><span style="color: #008080;">16</span> <span style="color: #000000;"> Console.WriteLine(t.Exception.InnerException); </span><span style="color: #008080;">17</span> <span style="color: #000000;"> } </span><span style="color: #008080;">18</span> <span style="color: #000000;"> }, TaskContinuationOptions.OnlyOnFaulted); </span><span style="color: #008080;">19</span> <span style="color: #008080;">20</span> <span style="color: #000000;"> Console.ReadKey(); </span><span style="color: #008080;">21</span> <span style="color: #000000;"> } </span><span style="color: #008080;">22</span> <span style="color: #000000;"> } </span><span style="color: #008080;">23</span> }</pre> </div> <span class="cnblogs_code_collapse">View Code</span> </div> <p>?</p> <p>運行結果:</p> <p><img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214645160484320523461.png"></p> <p>?<strong>4,全局設置TaskScheduler.UnobservedTaskException</strong></p> <p>?如果異步線程里的異常沒有被處理,也沒有調用Task.Wait方法將異常傳給主線程處理,最嚴重的后果可能會導致整個應用程序奔潰。詳細原因參考:System.Threading.Tasks.Task引起的IIS應用程序池崩潰</p> <p>所以,為了保證應用程序不會因為異步線程的異常未被處理導致掛掉,推薦的做法是,全局設置TaskScheduler.UnobservedTaskException。</p> <p>如果是web程序,可以在Global的Application_Start事件中進行設置,如下代碼:</p> <p><img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214645160484320555433.png"></p> <div class="cnblogs_code"> <img id="code_img_closed_6039d4e7-fe7c-4556-9b20-a206e1b010f4" class="code_img_closed" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214645160484320576907.gif"><img id="code_img_opened_6039d4e7-fe7c-4556-9b20-a206e1b010f4" class="code_img_opened" style="display: none;" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214646160484320665480.gif"><div id="cnblogs_code_open_6039d4e7-fe7c-4556-9b20-a206e1b010f4" class="cnblogs_code_hide"> <pre><span style="color: #008080;">1</span> <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> Application_Start(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, EventArgs e) </span><span style="color: #008080;">2</span> <span style="color: #000000;">{ </span><span style="color: #008080;">3</span> System.Threading.Tasks.TaskScheduler.UnobservedTaskException += (s, v) =&gt; <span style="color: #008080;">4</span> <span style="color: #000000;"> { </span><span style="color: #008080;">5</span> <span style="color: #000000;"> v.SetObserved(); </span><span style="color: #008080;">6</span> <span style="color: #000000;"> }; </span><span style="color: #008080;">7</span> }</pre> </div> <span class="cnblogs_code_collapse">View Code</span> </div> <p>?</p> Sun, 08 Nov 2020 21:46:46 +0800注冊時的密碼強度http://kf.icp123.net/sjk/8732.html <p>登陸密碼抗壓強度有4個狀態,各自如下圖。</p> <p>無登陸密碼狀態</p> <p> <img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214629160484318967572.png"> </p> <p>登陸密碼低等狀態</p> <p> <img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214629160484318994773.png"> </p> <p>登陸密碼初級狀態</p> <p> <img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214630160484319093968.png"> </p> <p>登陸密碼高級狀態</p> <p> <img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214630160484319051308.png"> </p> <p>完成的編碼關鍵以下:</p> <p>HTML編碼</p> <div class="cnblogs_code"> <div class="line number1 index0 alt2"> <p>&lt;input name="password" type="PassWord" onKeyUp="CheckIntensity(this.value)"&gt;<br>&lt;table border="0" cellpadding="0" cellspacing="0"&gt;<br> &lt;tr align="center"&gt;<br> &lt;td id="pwd_Weak" class="pwd pwd_c"&gt; &lt;/td&gt;<br> &lt;td id="pwd_Medium" class="pwd pwd_c pwd_f"&gt;無&lt;/td&gt;<br> &lt;td id="pwd_Strong" class="pwd pwd_c pwd_c_r"&gt; &lt;/td&gt;<br> &lt;/tr&gt;<br>&lt;/table&gt;</p> </div> </div> <p>CSS編碼</p> <div class="cnblogs_code"> <pre><span style="color: #800000">.pwd</span>{<span style="color: #ff0000">width</span>:<span style="color: #0000ff">50px</span>;<span style="color: #ff0000">height</span>:<span style="color: #0000ff">20px</span>;<span style="color: #ff0000">line-height</span>:<span style="color: #0000ff">14px</span>;<span style="color: #ff0000">padding-top</span>:<span style="color: #0000ff">2px</span>;}<span style="color: #800000"> .pwd_f</span>{<span style="color: #ff0000">color</span>:<span style="color: #0000ff">#BBBBBB</span>;}<span style="color: #800000"> .pwd_c</span>{<span style="color: #ff0000">background-color</span>:<span style="color: #0000ff">#F3F3F3</span>;<span style="color: #ff0000">border-top</span>:<span style="color: #0000ff">1px solid #D0D0D0</span>;<span style="color: #ff0000">border-bottom</span>:<span style="color: #0000ff">1px solid #D0D0D0</span>;<span style="color: #ff0000">border-left</span>:<span style="color: #0000ff">1px solid #D0D0D0</span>;}<span style="color: #800000"> .pwd_Weak_c</span>{<span style="color: #ff0000">background-color</span>:<span style="color: #0000ff">#FF4545</span>;<span style="color: #ff0000">border-top</span>:<span style="color: #0000ff">1px solid #BB2B2B</span>;<span style="color: #ff0000">border-bottom</span>:<span style="color: #0000ff">1px solid #BB2B2B</span>;<span style="color: #ff0000">border-left</span>:<span style="color: #0000ff">1px solid #BB2B2B</span>;}<span style="color: #800000"> .pwd_Medium_c</span>{<span style="color: #ff0000">background-color</span>:<span style="color: #0000ff">#FFD35E</span>;<span style="color: #ff0000">border-top</span>:<span style="color: #0000ff">1px solid #E9AE10</span>;<span style="color: #ff0000">border-bottom</span>:<span style="color: #0000ff">1px solid #E9AE10</span>;<span style="color: #ff0000">border-left</span>:<span style="color: #0000ff">1px solid #E9AE10</span>;}<span style="color: #800000"> .pwd_Strong_c</span>{<span style="color: #ff0000">background-color</span>:<span style="color: #0000ff">#2ABB1C</span>;<span style="color: #ff0000">border-top</span>:<span style="color: #0000ff">1px solid #267A12</span>;<span style="color: #ff0000">border-bottom</span>:<span style="color: #0000ff">1px solid #267A12</span>;<span style="color: #ff0000">border-left</span>:<span style="color: #0000ff">1px solid #267A12</span>;}<span style="color: #800000"> .pwd_c_r</span>{<span style="color: #ff0000">border-right</span>:<span style="color: #0000ff">1px solid #D0D0D0</span>;}<span style="color: #800000"> .pwd_Weak_c_r</span>{<span style="color: #ff0000">border-right</span>:<span style="color: #0000ff">1px solid #BB2B2B</span>;}<span style="color: #800000"> .pwd_Medium_c_r</span>{<span style="color: #ff0000">border-right</span>:<span style="color: #0000ff">1px solid #E9AE10</span>;}<span style="color: #800000"> .pwd_Strong_c_r</span>{<span style="color: #ff0000">border-right</span>:<span style="color: #0000ff">1px solid #267A12</span>;}</pre> </div> <p>JS編碼</p> <div class="cnblogs_code"> <pre><span style="color: #0000ff">function</span><span style="color: #000000"> CheckIntensity(pwd) { </span><span style="color: #0000ff">var</span><span style="color: #000000"> Mcolor, Wcolor, Scolor, Color_Html; </span> <span style="color: #0000ff">var</span> m = 0<span style="color: #000000">; </span><span style="color: #008000">//</span><span style="color: #008000">搭配數據</span> <span style="color: #0000ff">if</span> (/\d /<span style="color: #000000">.test(pwd)) { </span><span style="color: #0000ff">debugger</span><span style="color: #000000">; m</span> <span style="color: #000000">; }; </span><span style="color: #008000">//</span><span style="color: #008000">搭配英文字母</span> <span style="color: #0000ff">if</span> (/[A-Za-z] /<span style="color: #000000">.test(pwd)) { m</span> <span style="color: #000000">; }; </span><span style="color: #008000">//</span><span style="color: #008000">搭配除數據英文字母外的特殊字符</span> <span style="color: #0000ff">if</span> (/[^0-9a-zA-Z] /<span style="color: #000000">.test(pwd)) { m</span> <span style="color: #000000">; }; </span><span style="color: #0000ff">if</span> (pwd.length &lt;= 6) { m = 1<span style="color: #000000">; } </span><span style="color: #0000ff">if</span> (pwd.length &lt;= 0) { m = 0<span style="color: #000000">; } </span><span style="color: #0000ff">switch</span><span style="color: #000000"> (m) { </span><span style="color: #0000ff">case</span> 1<span style="color: #000000">: Wcolor </span>= "pwd pwd_Weak_c"<span style="color: #000000">; Mcolor </span>= "pwd pwd_c"<span style="color: #000000">; Scolor </span>= "pwd pwd_c pwd_c_r"<span style="color: #000000">; Color_Html </span>= "弱"<span style="color: #000000">; </span><span style="color: #0000ff">break</span><span style="color: #000000">; </span><span style="color: #0000ff">case</span> 2<span style="color: #000000">: Wcolor </span>= "pwd pwd_Medium_c"<span style="color: #000000">; Mcolor </span>= "pwd pwd_Medium_c"<span style="color: #000000">; Scolor </span>= "pwd pwd_c pwd_c_r"<span style="color: #000000">; Color_Html </span>= "中"<span style="color: #000000">; </span><span style="color: #0000ff">break</span><span style="color: #000000">; </span><span style="color: #0000ff">case</span> 3<span style="color: #000000">: Wcolor </span>= "pwd pwd_Strong_c"<span style="color: #000000">; Mcolor </span>= "pwd pwd_Strong_c"<span style="color: #000000">; Scolor </span>= "pwd pwd_Strong_c pwd_Strong_c_r"<span style="color: #000000">; Color_Html </span>= "強"<span style="color: #000000">; </span><span style="color: #0000ff">break</span><span style="color: #000000">; </span><span style="color: #0000ff">default</span><span style="color: #000000">: Wcolor </span>= "pwd pwd_c"<span style="color: #000000">; Mcolor </span>= "pwd pwd_c pwd_f"<span style="color: #000000">; Scolor </span>= "pwd pwd_c pwd_c_r"<span style="color: #000000">; Color_Html </span>= "無"<span style="color: #000000">; </span><span style="color: #0000ff">break</span><span style="color: #000000">; } document.getElementById(</span>'pwd_Weak').className =<span style="color: #000000"> Wcolor; document.getElementById(</span>'pwd_Medium').className =<span style="color: #000000"> Mcolor; document.getElementById(</span>'pwd_Strong').className =<span style="color: #000000"> Scolor; document.getElementById(</span>'pwd_Medium').innerHTML =<span style="color: #000000"> Color_Html; } </span></pre> </div> <p>?</p> Sun, 08 Nov 2020 21:46:34 +0800多線程(6)線程同步http://kf.icp123.net/sjk/8731.html <div>?  使用多線程很容易,但是如果多個線程同時訪問一個共享資源時而不加以控制,就會導致數據損壞。所以多線程并發時,必須要考慮線程同步(或稱線程安全)的問題。?</div> <h2>什么是線程同步</h2> <div><span style="color: #ff0000;"><strong>多個線程同時訪問共享資源時,使多個線程順序(串行)訪問共享資源的機制。</strong></span></div> <div>注意:</div> <div>1,共享資源,比如全局變量和靜態變量。</div> <div>2,訪問,一般指寫操作,讀操作無需考慮線程同步。</div> <div>3,串行,指當一個線程正在訪問共享資源時,其它線程等待,直到該線程釋放鎖。</div> <h2>線程同步帶來哪些問題</h2> <div>如果能保證多個線程不會同時訪問共享資源,那么就不需要考慮線程同步。</div> <div>雖然線程同步能保證多線程同時訪問共享數據時線程安全,但是同時也會帶來以下問題:</div> <div>1,使用起來繁瑣,因為必須找出代碼中所有可能由多個線程同時訪問的共享數據,并且要用額外的代碼將這些代碼包圍起來,獲取和釋放一個線程同步鎖,而一旦有一處忘記用鎖包圍,共享數據就會被損壞。</div> <div>2,損害性能,因為獲取和釋放一個鎖是需要時間的。</div> <div>3,可能會造成更多的線程被創建,由于線程同步鎖一次只允許一個線程訪問共享資源,當線程池線程試圖獲取一個暫時無法獲取的鎖時,線程池就會創建一個新的線程。</div> <div>所以,要從設計上盡可能地避免線程同步,實在不能避免的再考慮線程同步。</div> <h2>線程同步的常用解決方案</h2> <p><span style="font-size: 18pt;"><strong>1,鎖</strong></span></p> <div>包括lock關鍵字和Monitor類型。</div> <div>?</div> <div><strong>使用lock關鍵字實現:</strong></div> <div>?<img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214618160484317870571.png"><p>?</p> <div class="cnblogs_code"> <img id="code_img_closed_438a809c-0df6-4e93-93b2-a78b64fd5520" class="code_img_closed" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214619160484317992107.gif"><img id="code_img_opened_438a809c-0df6-4e93-93b2-a78b64fd5520" class="code_img_opened" style="display: none;" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214619160484317983491.gif"><div id="cnblogs_code_open_438a809c-0df6-4e93-93b2-a78b64fd5520" class="cnblogs_code_hide"> <pre><span style="color: #008080;"> 1</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span> <span style="color: #008080;"> 2</span> <span style="color: #808080;">///</span><span style="color: #008000;"> 線程同步計算器 </span><span style="color: #008080;"> 3</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span> <span style="color: #008080;"> 4</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> SyncCounter : CounterBase </span><span style="color: #008080;"> 5</span> <span style="color: #000000;">{ </span><span style="color: #008080;"> 6</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span> <span style="color: #008080;"> 7</span> <span style="color: #808080;">///</span><span style="color: #008000;"> 全局變量 </span><span style="color: #008080;"> 8</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span> <span style="color: #008080;"> 9</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> Result = <span style="color: #800080;">0</span><span style="color: #000000;">; </span><span style="color: #008080;">10</span> <span style="color: #008080;">11</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">readonly</span> <span style="color: #0000ff;">object</span> lockObj = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">object</span><span style="color: #000000;">(); </span><span style="color: #008080;">12</span> <span style="color: #008080;">13</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> Increase() </span><span style="color: #008080;">14</span> <span style="color: #000000;"> { </span><span style="color: #008080;">15</span> <span style="color: #0000ff;">lock</span><span style="color: #000000;"> (lockObj) </span><span style="color: #008080;">16</span> <span style="color: #000000;"> { </span><span style="color: #008080;">17</span> Result++<span style="color: #000000;">; </span><span style="color: #008080;">18</span> <span style="color: #000000;"> } </span><span style="color: #008080;">19</span> <span style="color: #000000;"> } </span><span style="color: #008080;">20</span> <span style="color: #008080;">21</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> Decrease() </span><span style="color: #008080;">22</span> <span style="color: #000000;"> { </span><span style="color: #008080;">23</span> <span style="color: #0000ff;">lock</span><span style="color: #000000;"> (lockObj) </span><span style="color: #008080;">24</span> <span style="color: #000000;"> { </span><span style="color: #008080;">25</span> Result--<span style="color: #000000;">; </span><span style="color: #008080;">26</span> <span style="color: #000000;"> } </span><span style="color: #008080;">27</span> <span style="color: #000000;"> } </span><span style="color: #008080;">28</span> }</pre> </div> <span class="cnblogs_code_collapse">View Code</span> </div> </div> <div><span style="color: #ff0000;"><strong>需要注意的是:</strong></span></div> <div><span style="color: #ff0000;"><strong>1,lock鎖定的對象必須是引用類型,不能是值類型。因為值類型傳入會發生裝箱,這樣每次lock的將是一個不同的對象,就沒有辦法實現多線程同步了。</strong></span></div> <div><span style="color: #ff0000;"><strong>2,避免使用public類型的對象,這樣很容易導致死鎖。因為其它代碼也有可能鎖定該對象。</strong></span></div> <div><span style="color: #ff0000;"><strong>3,避免鎖定字符串,因為字符串會被CLR暫留(也就是說兩個變量的字符串內容相同,.net會把暫留的字符串對象分配給變量),導致應用程序中鎖定的是同一個對象,造成死鎖。</strong></span></div> <div>?</div> <div><strong>使用Monitor實現:</strong></div> <div>?<img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214620160484318089133.png"><div class="cnblogs_code"> <img id="code_img_closed_69f3665e-d9fe-4dc8-9434-c9795f113169" class="code_img_closed" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214620160484318027102.gif"><img id="code_img_opened_69f3665e-d9fe-4dc8-9434-c9795f113169" class="code_img_opened" style="display: none;" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214621160484318197809.gif"><div id="cnblogs_code_open_69f3665e-d9fe-4dc8-9434-c9795f113169" class="cnblogs_code_hide"> <pre><span style="color: #008080;"> 1</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span> <span style="color: #008080;"> 2</span> <span style="color: #808080;">///</span><span style="color: #008000;"> 線程同步計算器 </span><span style="color: #008080;"> 3</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span> <span style="color: #008080;"> 4</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> SyncCounter : CounterBase </span><span style="color: #008080;"> 5</span> <span style="color: #000000;">{ </span><span style="color: #008080;"> 6</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span> <span style="color: #008080;"> 7</span> <span style="color: #808080;">///</span><span style="color: #008000;"> 全局變量 </span><span style="color: #008080;"> 8</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span> <span style="color: #008080;"> 9</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> Result = <span style="color: #800080;">0</span><span style="color: #000000;">; </span><span style="color: #008080;">10</span> <span style="color: #008080;">11</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">readonly</span> <span style="color: #0000ff;">object</span> lockObj = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">object</span><span style="color: #000000;">(); </span><span style="color: #008080;">12</span> <span style="color: #008080;">13</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> Increase() </span><span style="color: #008080;">14</span> <span style="color: #000000;"> { </span><span style="color: #008080;">15</span> <span style="color: #000000;"> Monitor.Enter(lockObj); </span><span style="color: #008080;">16</span> <span style="color: #0000ff;">try</span> <span style="color: #008080;">17</span> <span style="color: #000000;"> { </span><span style="color: #008080;">18</span> Result++<span style="color: #000000;">; </span><span style="color: #008080;">19</span> <span style="color: #000000;"> } </span><span style="color: #008080;">20</span> <span style="color: #0000ff;">finally</span> <span style="color: #008080;">21</span> <span style="color: #000000;"> { </span><span style="color: #008080;">22</span> <span style="color: #000000;"> Monitor.Exit(lockObj); </span><span style="color: #008080;">23</span> <span style="color: #000000;"> } </span><span style="color: #008080;">24</span> <span style="color: #000000;"> } </span><span style="color: #008080;">25</span> <span style="color: #008080;">26</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> Decrease() </span><span style="color: #008080;">27</span> <span style="color: #000000;"> { </span><span style="color: #008080;">28</span> <span style="color: #000000;"> Monitor.Enter(lockObj); </span><span style="color: #008080;">29</span> <span style="color: #0000ff;">try</span> <span style="color: #008080;">30</span> <span style="color: #000000;"> { </span><span style="color: #008080;">31</span> Result--<span style="color: #000000;">; </span><span style="color: #008080;">32</span> <span style="color: #000000;"> } </span><span style="color: #008080;">33</span> <span style="color: #0000ff;">finally</span> <span style="color: #008080;">34</span> <span style="color: #000000;"> { </span><span style="color: #008080;">35</span> <span style="color: #000000;"> Monitor.Exit(lockObj); </span><span style="color: #008080;">36</span> <span style="color: #000000;"> } </span><span style="color: #008080;">37</span> <span style="color: #000000;"> } </span><span style="color: #008080;">38</span> }</pre> </div> <span class="cnblogs_code_collapse">View Code</span> </div> <p>完整代碼:</p> <div class="cnblogs_code"> <img id="code_img_closed_6b1ced0b-62a9-4b14-ac17-65dd9a53cca1" class="code_img_closed" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214621160484318120157.gif"><img id="code_img_opened_6b1ced0b-62a9-4b14-ac17-65dd9a53cca1" class="code_img_opened" style="display: none;" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214622160484318299715.gif"><div id="cnblogs_code_open_6b1ced0b-62a9-4b14-ac17-65dd9a53cca1" class="cnblogs_code_hide"> <pre><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">namespace</span><span style="color: #000000;"> ConsoleApplication28 </span><span style="color: #008080;"> 2</span> <span style="color: #000000;">{ </span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Program </span><span style="color: #008080;"> 4</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args) </span><span style="color: #008080;"> 6</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 7</span> <span style="color: #008000;">//</span><span style="color: #008000;">同時發起3個異步線程</span> <span style="color: #008080;"> 8</span> Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">普通(非線程同步)計算器測試...</span><span style="color: #800000;">"</span><span style="color: #000000;">); </span><span style="color: #008080;"> 9</span> <span style="color: #0000ff;">var</span> normalCounter = <span style="color: #0000ff;">new</span><span style="color: #000000;"> NormalCounter(); </span><span style="color: #008080;"> 10</span> <span style="color: #0000ff;">var</span> tasks = <span style="color: #0000ff;">new</span> List&lt;Task&gt;<span style="color: #000000;">(); </span><span style="color: #008080;"> 11</span> <span style="color: #0000ff;">var</span> task1 = Task.Factory.StartNew(() =&gt; <span style="color: #008080;"> 12</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 13</span> <span style="color: #000000;"> TestCounter(normalCounter); </span><span style="color: #008080;"> 14</span> <span style="color: #000000;"> }); </span><span style="color: #008080;"> 15</span> <span style="color: #000000;"> tasks.Add(task1); </span><span style="color: #008080;"> 16</span> <span style="color: #008080;"> 17</span> <span style="color: #0000ff;">var</span> task2 = Task.Factory.StartNew(() =&gt; <span style="color: #008080;"> 18</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 19</span> <span style="color: #000000;"> TestCounter(normalCounter); </span><span style="color: #008080;"> 20</span> <span style="color: #000000;"> }); </span><span style="color: #008080;"> 21</span> <span style="color: #000000;"> tasks.Add(task2); </span><span style="color: #008080;"> 22</span> <span style="color: #008080;"> 23</span> <span style="color: #0000ff;">var</span> task3 = Task.Factory.StartNew(() =&gt; <span style="color: #008080;"> 24</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 25</span> <span style="color: #000000;"> TestCounter(normalCounter); </span><span style="color: #008080;"> 26</span> <span style="color: #000000;"> }); </span><span style="color: #008080;"> 27</span> <span style="color: #000000;"> tasks.Add(task3); </span><span style="color: #008080;"> 28</span> <span style="color: #008080;"> 29</span> <span style="color: #008080;"> 30</span> <span style="color: #000000;"> Task.WaitAll(tasks.ToArray()); </span><span style="color: #008080;"> 31</span> Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">NormalCounter.Result:</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> normalCounter.Result); </span><span style="color: #008080;"> 32</span> Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">*******************************************</span><span style="color: #800000;">"</span><span style="color: #000000;">); </span><span style="color: #008080;"> 33</span> <span style="color: #008080;"> 34</span> Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">線程同步計算器測試...</span><span style="color: #800000;">"</span><span style="color: #000000;">); </span><span style="color: #008080;"> 35</span> <span style="color: #0000ff;">var</span> syncCounter = <span style="color: #0000ff;">new</span><span style="color: #000000;"> SyncCounter(); </span><span style="color: #008080;"> 36</span> <span style="color: #0000ff;">var</span> tasks1 = <span style="color: #0000ff;">new</span> List&lt;Task&gt;<span style="color: #000000;">(); </span><span style="color: #008080;"> 37</span> task1 = Task.Factory.StartNew(() =&gt; <span style="color: #008080;"> 38</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 39</span> <span style="color: #000000;"> TestCounter(syncCounter); </span><span style="color: #008080;"> 40</span> <span style="color: #000000;"> }); </span><span style="color: #008080;"> 41</span> <span style="color: #000000;"> tasks1.Add(task1); </span><span style="color: #008080;"> 42</span> <span style="color: #008080;"> 43</span> task2 = Task.Factory.StartNew(() =&gt; <span style="color: #008080;"> 44</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 45</span> <span style="color: #000000;"> TestCounter(syncCounter); </span><span style="color: #008080;"> 46</span> <span style="color: #000000;"> }); </span><span style="color: #008080;"> 47</span> <span style="color: #000000;"> tasks1.Add(task2); </span><span style="color: #008080;"> 48</span> <span style="color: #008080;"> 49</span> task3 = Task.Factory.StartNew(() =&gt; <span style="color: #008080;"> 50</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 51</span> <span style="color: #000000;"> TestCounter(syncCounter); </span><span style="color: #008080;"> 52</span> <span style="color: #000000;"> }); </span><span style="color: #008080;"> 53</span> <span style="color: #000000;"> tasks1.Add(task3); </span><span style="color: #008080;"> 54</span> <span style="color: #008080;"> 55</span> <span style="color: #000000;"> Task.WaitAll(tasks1.ToArray()); </span><span style="color: #008080;"> 56</span> Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">SyncCounter.Result:</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> syncCounter.Result); </span><span style="color: #008080;"> 57</span> <span style="color: #008080;"> 58</span> <span style="color: #000000;"> Console.ReadKey(); </span><span style="color: #008080;"> 59</span> <span style="color: #000000;"> } </span><span style="color: #008080;"> 60</span> <span style="color: #008080;"> 61</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span> <span style="color: #008080;"> 62</span> <span style="color: #808080;">///</span> <span style="color: #008080;"> 63</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span> <span style="color: #008080;"> 64</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;param name="counter"&gt;&lt;/param&gt;</span> <span style="color: #008080;"> 65</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> TestCounter(CounterBase counter) </span><span style="color: #008080;"> 66</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 67</span> <span style="color: #008000;">//</span><span style="color: #008000;">100000次加減</span> <span style="color: #008080;"> 68</span> <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = <span style="color: #800080;">0</span>; i &lt; <span style="color: #800080;">100000</span>; i++<span style="color: #000000;">) </span><span style="color: #008080;"> 69</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 70</span> <span style="color: #000000;"> counter.Increase(); </span><span style="color: #008080;"> 71</span> <span style="color: #000000;"> counter.Decrease(); </span><span style="color: #008080;"> 72</span> <span style="color: #000000;"> } </span><span style="color: #008080;"> 73</span> <span style="color: #000000;"> } </span><span style="color: #008080;"> 74</span> <span style="color: #000000;"> } </span><span style="color: #008080;"> 75</span> <span style="color: #008080;"> 76</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span> <span style="color: #008080;"> 77</span> <span style="color: #808080;">///</span><span style="color: #008000;"> 計算器基類 </span><span style="color: #008080;"> 78</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span> <span style="color: #008080;"> 79</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> CounterBase </span><span style="color: #008080;"> 80</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 81</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span> <span style="color: #008080;"> 82</span> <span style="color: #808080;">///</span><span style="color: #008000;"> 加 </span><span style="color: #008080;"> 83</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span> <span style="color: #008080;"> 84</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> Increase(); </span><span style="color: #008080;"> 85</span> <span style="color: #008080;"> 86</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span> <span style="color: #008080;"> 87</span> <span style="color: #808080;">///</span><span style="color: #008000;"> 減 </span><span style="color: #008080;"> 88</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span> <span style="color: #008080;"> 89</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> Decrease(); </span><span style="color: #008080;"> 90</span> <span style="color: #000000;"> } </span><span style="color: #008080;"> 91</span> <span style="color: #008080;"> 92</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span> <span style="color: #008080;"> 93</span> <span style="color: #808080;">///</span><span style="color: #008000;"> 普通計算器 </span><span style="color: #008080;"> 94</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span> <span style="color: #008080;"> 95</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> NormalCounter : CounterBase </span><span style="color: #008080;"> 96</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 97</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span> <span style="color: #008080;"> 98</span> <span style="color: #808080;">///</span><span style="color: #008000;"> 全局變量 </span><span style="color: #008080;"> 99</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span> <span style="color: #008080;">100</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> Result = <span style="color: #800080;">0</span><span style="color: #000000;">; </span><span style="color: #008080;">101</span> <span style="color: #008080;">102</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> Increase() </span><span style="color: #008080;">103</span> <span style="color: #000000;"> { </span><span style="color: #008080;">104</span> Result++<span style="color: #000000;">; </span><span style="color: #008080;">105</span> <span style="color: #000000;"> } </span><span style="color: #008080;">106</span> <span style="color: #008080;">107</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> Decrease() </span><span style="color: #008080;">108</span> <span style="color: #000000;"> { </span><span style="color: #008080;">109</span> Result--<span style="color: #000000;">; </span><span style="color: #008080;">110</span> <span style="color: #000000;"> } </span><span style="color: #008080;">111</span> <span style="color: #008080;">112</span> <span style="color: #000000;"> } </span><span style="color: #008080;">113</span> <span style="color: #008080;">114</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span> <span style="color: #008080;">115</span> <span style="color: #808080;">///</span><span style="color: #008000;"> 線程同步計算器 </span><span style="color: #008080;">116</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span> <span style="color: #008080;">117</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> SyncCounter : CounterBase </span><span style="color: #008080;">118</span> <span style="color: #000000;"> { </span><span style="color: #008080;">119</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span> <span style="color: #008080;">120</span> <span style="color: #808080;">///</span><span style="color: #008000;"> 全局變量 </span><span style="color: #008080;">121</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span> <span style="color: #008080;">122</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> Result = <span style="color: #800080;">0</span><span style="color: #000000;">; </span><span style="color: #008080;">123</span> <span style="color: #008080;">124</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">readonly</span> <span style="color: #0000ff;">object</span> lockObj = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">object</span><span style="color: #000000;">(); </span><span style="color: #008080;">125</span> <span style="color: #008080;">126</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> Increase() </span><span style="color: #008080;">127</span> <span style="color: #000000;"> { </span><span style="color: #008080;">128</span> <span style="color: #0000ff;">lock</span><span style="color: #000000;"> (lockObj) </span><span style="color: #008080;">129</span> <span style="color: #000000;"> { </span><span style="color: #008080;">130</span> Result++<span style="color: #000000;">; </span><span style="color: #008080;">131</span> <span style="color: #000000;"> } </span><span style="color: #008080;">132</span> <span style="color: #000000;"> } </span><span style="color: #008080;">133</span> <span style="color: #008080;">134</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> Decrease() </span><span style="color: #008080;">135</span> <span style="color: #000000;"> { </span><span style="color: #008080;">136</span> <span style="color: #0000ff;">lock</span><span style="color: #000000;"> (lockObj) </span><span style="color: #008080;">137</span> <span style="color: #000000;"> { </span><span style="color: #008080;">138</span> Result--<span style="color: #000000;">; </span><span style="color: #008080;">139</span> <span style="color: #000000;"> } </span><span style="color: #008080;">140</span> <span style="color: #000000;"> } </span><span style="color: #008080;">141</span> <span style="color: #000000;"> } </span><span style="color: #008080;">142</span> }</pre> </div> <span class="cnblogs_code_collapse">View Code</span> </div> <p>??</p> <p><strong>lock關鍵字揭密:</strong></p> </div> <div>?通過查看lock關鍵字生成的IL代碼,如下圖:</div> <div> <img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214623160484318322718.png"><p><span style="color: #ff0000;"><strong>從上圖可以得出以下結論:</strong></span></p> <p><span style="color: #ff0000;"><strong>lock關鍵字內部就是使用Monitor類(或者說lock關鍵字是Monitor的語法糖),使用lock關鍵字比直接使用Monitor更好,原因有二。</strong></span></p> <p><span style="color: #ff0000;"><strong>1,lock語法更簡潔。</strong></span></p> <p><span style="color: #ff0000;"><strong>2,lock確保了即使代碼拋出異常,也可以釋放鎖,因為在finally中調用了Monitor.Exit方法。?</strong></span></p> </div> <p><span style="font-size: 18pt;"><strong>2,信號同步</strong></span></p> <div>信號同步機制中涉及的類型都繼承自抽象類WaitHandle,這些類型有EventWaitHandle(類型化為AutoResetEvent、ManualResetEvent)和Semaphore以及Mutex。關系如下圖。</div> <div> <img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214623160484318383435.png"><p>?</p> <p>下面是使用信號同步機制的一個簡單的例子,如下代碼:</p> <p><img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214624160484318483548.png"></p> <div class="cnblogs_code"> <img id="code_img_closed_919dd532-4234-42df-8816-107a4a99a19c" class="code_img_closed" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214625160484318532777.gif"><img id="code_img_opened_919dd532-4234-42df-8816-107a4a99a19c" class="code_img_opened" style="display: none;" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214625160484318511243.gif"><div id="cnblogs_code_open_919dd532-4234-42df-8816-107a4a99a19c" class="cnblogs_code_hide"> <pre><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">namespace</span><span style="color: #000000;"> WindowsFormsApplication1 </span><span style="color: #008080;"> 2</span> <span style="color: #000000;">{ </span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">partial</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Form1 : Form </span><span style="color: #008080;"> 4</span> <span style="color: #000000;"> { </span><span style="color: #008080;"> 5</span> <span style="color: #008000;">//</span><span style="color: #008000;">信號</span> <span style="color: #008080;"> 6</span> AutoResetEvent autoResetEvent = <span style="color: #0000ff;">new</span> AutoResetEvent(<span style="color: #0000ff;">false</span><span style="color: #000000;">); </span><span style="color: #008080;"> 7</span> <span style="color: #008080;"> 8</span> <span style="color: #0000ff;">public</span><span style="color: #000000;"> Form1() </span><span style="color: #008080;"> 9</span> <span style="color: #000000;"> { </span><span style="color: #008080;">10</span> <span style="color: #000000;"> InitializeComponent(); </span><span style="color: #008080;">11</span> <span style="color: #008080;">12</span> CheckForIllegalCrossThreadCalls = <span style="color: #0000ff;">false</span><span style="color: #000000;">; </span><span style="color: #008080;">13</span> <span style="color: #000000;"> } </span><span style="color: #008080;">14</span> <span style="color: #008080;">15</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span> <span style="color: #008080;">16</span> <span style="color: #808080;">///</span><span style="color: #008000;"> 開始 </span><span style="color: #008080;">17</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span> <span style="color: #008080;">18</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;param name="sender"&gt;&lt;/param&gt;</span> <span style="color: #008080;">19</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;param name="e"&gt;&lt;/param&gt;</span> <span style="color: #008080;">20</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> button1_Click(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, EventArgs e) </span><span style="color: #008080;">21</span> <span style="color: #000000;"> { </span><span style="color: #008080;">22</span> Task.Factory.StartNew(() =&gt; <span style="color: #008080;">23</span> <span style="color: #000000;"> { </span><span style="color: #008080;">24</span> <span style="color: #0000ff;">this</span>.richTextBox1.Text+=<span style="color: #800000;">"</span><span style="color: #800000;">線程啟動...</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Environment.NewLine; </span><span style="color: #008080;">25</span> <span style="color: #0000ff;">this</span>.richTextBox1.Text += <span style="color: #800000;">"</span><span style="color: #800000;">開始處理一些實際的工作</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Environment.NewLine; </span><span style="color: #008080;">26</span> Thread.Sleep(<span style="color: #800080;">3000</span><span style="color: #000000;">); </span><span style="color: #008080;">27</span> <span style="color: #008080;">28</span> <span style="color: #0000ff;">this</span>.richTextBox1.Text += <span style="color: #800000;">"</span><span style="color: #800000;">我開始等待別的線程給我信號,才愿意繼續下去</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Environment.NewLine; </span><span style="color: #008080;">29</span> <span style="color: #000000;"> autoResetEvent.WaitOne(); </span><span style="color: #008080;">30</span> <span style="color: #0000ff;">this</span>.richTextBox1.Text += <span style="color: #800000;">"</span><span style="color: #800000;">我繼續做一些工作,然后結束了!</span><span style="color: #800000;">"</span><span style="color: #000000;">; </span><span style="color: #008080;">31</span> <span style="color: #000000;"> }); </span><span style="color: #008080;">32</span> <span style="color: #000000;"> } </span><span style="color: #008080;">33</span> <span style="color: #008080;">34</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span> <span style="color: #008080;">35</span> <span style="color: #808080;">///</span><span style="color: #008000;"> 信號同步 </span><span style="color: #008080;">36</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span> <span style="color: #008080;">37</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;param name="sender"&gt;&lt;/param&gt;</span> <span style="color: #008080;">38</span> <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;param name="e"&gt;&lt;/param&gt;</span> <span style="color: #008080;">39</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> button2_Click(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, EventArgs e) </span><span style="color: #008080;">40</span> <span style="color: #000000;"> { </span><span style="color: #008080;">41</span> <span style="color: #008000;">//</span><span style="color: #008000;">給在autoResetEvent上等待的線程一個信號</span> <span style="color: #008080;">42</span> <span style="color: #000000;"> autoResetEvent.Set(); </span><span style="color: #008080;">43</span> <span style="color: #000000;"> } </span><span style="color: #008080;">44</span> <span style="color: #000000;"> } </span><span style="color: #008080;">45</span> }</pre> </div> <span class="cnblogs_code_collapse">View Code</span> </div> <p>運行效果:</p> <p>1,線程阻塞,等待信號。</p> <p><img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214626160484318645200.png"></p> <p>2,主線程發送信號,讓線程繼續執行。</p> <p><img src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214626160484318639342.png"></p> </div> <div> <p><span style="font-size: 18pt;"><strong>3,線程安全的集合類</strong></span></p> </div> <div>我們也可以通過使用.net提供的線程安全的集合類來保證線程安全。在命名空間:System.Collections.Concurrent下。</div> <div>主要包括:</div> <div> <ul> <li>ConcurrentQueue 線程安全版本的Queue<strong><span style="color: #ff0000;">【常用】</span></strong> </li> <li>ConcurrentStack線程安全版本的Stack</li> <li>ConcurrentBag線程安全的對象集合</li> <li>ConcurrentDictionary線程安全的Dictionary<span style="color: #ff0000;"><strong>【常用】</strong></span> </li> </ul> </div> <div>?</div> Sun, 08 Nov 2020 21:46:27 +0800WPF(C#)與MATLAB混合編程http://kf.icp123.net/sjk/8730.html <p style="text-align: center"><span style="font-size: 18px">WPF(C#)與MATLAB混和程序編寫</span></p> <p style="text-align: left"><span style="font-size: 18px">? ? ? ?WPF能夠為開發人員出示方便快捷地搭建客戶互動頁面的解決方案,而matlab則在計算機的應用層面擁有 無以倫比的優點,因而在一些必須將科學研究優化算法變換為系統軟件的新項目中,必須運用到WPF也就是說C#和matlab的混和程序編寫。對比于MFC和MATLAB的混和變為,C#兩者之間的混編所必須做的準備工作更加簡易。</span></p> <p style="text-align: left"><span style="font-size: 18px; color: #0000ff">1、編寫必須被WPF程序流程啟用的matlab函數文檔,比如:test.m;</span></p> <p style="text-align: left"><span style="font-size: 18px; color: #0000ff">2、編譯程序test.m文檔;</span></p> <p style="text-align: left"><span style="font-size: 18px; color: #0000ff">? ? <span style="color: #000000">(1)開啟matlab的布署專用工具(我常用到的MATLAB版本號為R2014a,系統軟件為Windows7 32位系統)</span></span></p> <p style="text-align: left"><span style="font-size: 18px; color: #000000">? ? ? 將test.m文檔所屬的文件目錄設定為各項工作文件目錄,在matlabcmd中鍵入:“deploytool”,在出現的三個選擇項中挑選Library Compiler選擇項;</span></p> <p style="text-align: left"><span style="font-size: 18px; color: #000000"> <img style="display: block; margin-left: auto; margin-right: auto" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214613160484317376999.png"> </span></p> <p>?</p> <p style="text-align: left">? ? <span style="font-size: 18px">(2)開展布署的設定</span></p> <p style="text-align: left"><span style="font-size: 18px">? ? ?如下圖所顯示,在編號1處挑選所輸出的庫種類,因為是對于C#開展布署,因而挑選.NET Assembly選擇項。在編號2處,點一下右邊的減號按鍵加上必須被編譯程序的MATLAB函數,比如本例中的test.m函數,能夠在這其中加上好幾個MATLAB函數文檔。編號3處是鍵入所形成的庫的名字。編號4處,能夠設置所形成的類的名字,能夠將以前所加上的MATLAB函數加上進來,做為類中的一個函數,這一步能夠覺得是新創建一個類,在這其中創建大家要想啟用的MATLAB函數的C#完成。在前四步設置好以后就可以點一下編號5處的打包按鍵。打包需要的時間較為長,耐心等候程序流程打包進行。</span></p> <p style="text-align: left"> <img style="display: block; margin-left: auto; margin-right: auto" src="http://www.tacshn.com/zb_users/upload/2020/11/20201108214614160484317445205.png"> </p> <p style="text-align: left"><span style="font-size: 18px; color: #0000ff">3、將形成的DLL庫運用到wpf新項目中,而且在新項目中開展有關設定</span></p> <p style="text-align: left"><span style="font-size: 18px">? ? ? 這一步這篇blog上邊寫的很清晰,能夠參照在其中的內容進行最后一步。</span></p> <p style="text-align: left"><span style="font-size: 18px; color: #0000ff">PS:小編近期檢測C#與Matlab混和程序編寫的可擴展性,各自檢測了64位WIN7、win10X64、32位系統win7和64位win10,發覺應用32位系統matlab內的MWArray.dll,沒法運用到64位的系統軟件上,因而必須對于不一樣的對系統新項目開展編譯程序。<br></span></p> <p style="text-align: left">?</p> <p>?</p> Sun, 08 Nov 2020 21:46:16 +0800C# 泛型http://kf.icp123.net/sjk/8729.html <p>一、泛型</p> <p>假定我要寫個公共的輸出傳到主要參數的方式(無需泛型),由于萬物皆目標的原因,我先界定一個方式show(object obj),如下邊所顯示:</p> <div class="cnblogs_code"> <pre> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Show(<span style="color: #0000ff">object</span><span style="color: #000000"> obj) { Console.WriteLine(obj.ToString()); }</span></pre> </div> <p>實行這一方式</p> <div class="cnblogs_code"> <pre> <span style="color: #0000ff">int</span> i = <span style="color: #800080">1</span>; <span style="color: #008000">//</span><span style="color: #008000">裝箱</span> Show(i);</pre> </div> <p>假如傳到的是值種類,值類型轉換為引用類型,我們知道會產生裝箱,它是對特性的危害,想一想如果是個結合,就得數次實行裝箱、開箱實際操作。如ArrayList類,ArrayList存儲目標,Add()方式界定為必須把一個目標做為主要參數,假如傳到的值種類,就得裝箱,在載入ArrayList中的值時,又得開展開箱,如下邊編碼所顯示:</p> <div class="cnblogs_code"> <pre> <span style="color: #0000ff">var</span> list = <span style="color: #0000ff">new</span><span style="color: #000000"> ArrayList(); list.Add(</span><span style="color: #800080">1</span>); <span style="color: #008000">//</span><span style="color: #008000">裝箱</span> <span style="color: #0000ff">foreach</span> (<span style="color: #0000ff">int</span> i <span style="color: #0000ff">in</span><span style="color: #000000"> list) { Console.WriteLine(i); </span><span style="color: #008000">//</span><span style="color: #008000">開箱</span> }</pre> </div> <p>假如應用泛型,就不容易出現那樣的難題了,大家應用List&lt;T&gt;類來更新改造上邊編碼:</p> <div class="cnblogs_code"> <pre> <span style="color: #0000ff">var</span> list = <span style="color: #0000ff">new</span> List&lt;<span style="color: #0000ff">int</span>&gt;<span style="color: #000000">(); list.Add(</span><span style="color: #800080">1</span><span style="color: #000000">); </span><span style="color: #0000ff">foreach</span> (<span style="color: #0000ff">int</span> i <span style="color: #0000ff">in</span><span style="color: #000000"> list) { Console.WriteLine(i); }</span></pre> </div> <p>這兒就不會有裝箱和開箱了,因此 我們在應用結合的情況下,盡可能應用泛型結合,不必應用非泛型結合。</p> <p>二、種類安全性</p> <p>在上面ArrayList類中,加上主要參數時,能夠加上一切目標,例如上邊的事例,假如在加上整數金額種類后再加上引用類型,那么做在編譯程序時是沒有一切難題,可是在foreach句子應用整數金額種類迭代更新的情況下便會出錯。</p> <div class="cnblogs_code"> <pre> <span style="color: #0000ff">var</span> list = <span style="color: #0000ff">new</span><span style="color: #000000"> ArrayList(); list.Add(</span><span style="color: #800080">1</span>); <span style="color: #008000">//</span><span style="color: #008000">裝箱</span> list.Add(<span style="color: #800000">"</span><span style="color: #800000">string</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">foreach</span> (<span style="color: #0000ff">int</span> i <span style="color: #0000ff">in</span><span style="color: #000000"> list) { Console.WriteLine(i); }</span></pre> </div> <p>此刻便會報InvalidCastException的出現異常。</p> <p>假如應用泛型結合List&lt;T&gt;的情況下去調用上邊的編碼,在編譯程序的情況下便會出錯。因此 這個地方大家就能了解,泛型是在編譯程序時就早已實行了,因此 系統軟件運作時大家時沒有裝箱開箱的系統軟件花銷,并非泛型是在運作時實行的,因此 很有可能造成 出現異常產生;</p> <p>三、建立泛型類和泛型方法</p> <p>泛型方法,從我最開始第一個事例Show(object) ?,選用泛型來調用,界定為Show&lt;T&gt;(T);</p> <div class="cnblogs_code"> <pre> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Show&lt;T&gt;<span style="color: #000000">(T obj) { Console.WriteLine(obj.ToString()); }</span></pre> </div> <p>?泛型類,如public class List&lt;T&gt;{}</p> <p>3.1 取名承諾</p> <ul> <li>? ? ? 泛型類型的名字用英文字母T做為作為前綴。</li> <li>? ? ? 要是沒有獨特的規定,泛型類型運作用隨意類取代,且只使有一個泛型類型,就可以用標識符T做為泛型類型的名字。</li> <li>? ? ? 假如泛型類型有獨特的規定(如它務必完成一個插口或繼承自基類),或是應用了2個或之上的泛型類型,就應給泛型類型應用說明性的名字:</li> </ul> <p>? ? ? public delegate void EventHandler&lt;TEventArgs&gt;(object sender,TEventArgs e);</p> <p>? ? ? public delegate TOutput Convert&lt;TInput,TOutput&gt;(TInput input);</p> <p>? ? ? public class SortedList&lt;TKey,TValue&gt;{};</p> <p>3.2 初始值</p> <p>? ? ?在泛型類和泛型方法中造成的一個難題是,在事先不明下列狀況時,如何把初始值分派給參數化設計種類 T,給出參數化設計種類 T 的一個自變量 t,僅有當 T 為引用類型時,句子 t = null 才合理;僅有當 T 為標值種類而不是構造時,句子 t = 0 才可以一切正常應用。?解決方法是應用 <strong>default</strong>?關鍵詞,此關鍵詞針對引用類型會回到 null,針對標值種類會回到零。 針對構造,此關鍵詞將回到復位為零或 null 的每一個構造組員。</p> <p>應用方法如:<strong>T obj=default(T);</strong></p> <p>3.3 管束</p> <p>在界定泛型類時,能夠對手機客戶端編碼可以在實例化類時用以種類主要參數的種類類型釋放限定。 假如手機客戶端編碼試著應用某一管束所不允許的種類來實例化類,則會造成編譯程序時不正確。 這種限定稱之為管束。 管束是應用<strong>where</strong>前后文關鍵詞特定的。??下表列出了六種種類的管束:</p> <table style="height: 217px; width: 841px; border-color: #000000; border-width: 1080x; border-style: solid"><tbody> <tr> <td>管束  </td> <td>表明</td> </tr> <tr> <td>where T:struct  </td> <td>針對構造的管束,種類T務必是值種類。</td> </tr> <tr> <td>where T:class  </td> <td>類的管束,種類T務必是運用種類。</td> </tr> <tr> <td>where T:&lt;插口名字&gt;</td> <td>種類主要參數務必是特定的插口或完成特定的插口。 能夠特定好幾個插口管束。 管束插口還可以是泛型的。</td> </tr> <tr> <td>where T:&lt;基類名&gt;</td> <td>種類主要參數務必是特定的基類或繼承自特定的基類。</td> </tr> <tr> <td>where T:new()    </td> <td>種類主要參數務必具備無主要參數的公共性構造方法。 當與別的管束一起應用時,new()?管束務必最終特定。</td> </tr> <tr> <td>where T1:T2          </td> <td>種類T1務必是種類T2或繼承自泛型類型T2,該管束也稱之為裸型管束。</td> </tr> </tbody></table> <p>?</p> <p>?</p> <p>?</p> <p>?</p> <p>?</p> <p>?</p> <p>?</p> <p>?</p> <div class="cnblogs_code"> <pre> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> MyClass&lt;T&gt; <span style="color: #0000ff">where</span> T : IComparer&lt;T&gt;, <span style="color: #0000ff">new</span><span style="color: #000000">() { }</span></pre> </div> <p>上邊編碼,應用泛型類型加上了2個管束,申明特定種類T務必完成了IComparer插口,且務必有一個默認設置構造方法</p> <div class="cnblogs_code"> <pre> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> MyClass&lt;TOutput, TInput&gt; <span style="color: #0000ff">where</span> TOutput : IComparer&lt;TOutput&gt;, <span style="color: #0000ff">new</span><span style="color: #000000">() </span><span style="color: #0000ff">where</span> TInput:<span style="color: #0000ff">class</span><span style="color: #000000">,TOutput { }</span></pre> </div> <p>上邊編碼用了2個泛型類型,TOutput務必完成了IComparer插口,且務必有一個默認設置構造方法,TInput務必是引用類型,且種類務必是TOutput或繼承自TOutput。</p> <p>3.4 承繼</p> <p>泛型類型能夠完成泛型插口,還可以繼承自一個類。泛型類型能夠繼承自泛型基類,其規定務必反復插口的泛型類型,或是務必特定基類的種類。如下述所顯示:</p> <div class="cnblogs_code"> <pre> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> BaseClass&lt;T&gt;<span style="color: #000000"> { } </span><span style="color: #808080">///</span><span style="color: #008000">務必反復插口\基類的泛型類型</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> MyClass&lt;T&gt; : BaseClass&lt;T&gt; { }</pre> </div> <div class="cnblogs_code"> <pre> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> BaseClass&lt;T&gt;<span style="color: #000000"> { } </span><span style="color: #808080">///</span><span style="color: #008000">務必特定基類的種類</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> MyClass&lt;T&gt; : BaseClass&lt;String&gt; { }</pre> </div> <p>派生類能夠是泛型類或者非泛型類,比如界定一個抽象性的泛型基類,它在派生類中用一個實際的種類完成,如下述所顯示:</p> <div class="cnblogs_code"> <pre> <span style="color: #0000ff">public</span> <span style="color: #0000ff">abstract</span> <span style="color: #0000ff">class</span> Calcu&lt;T&gt;<span style="color: #000000"> { </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">abstract</span><span style="color: #000000"> T Add(T x, T y); </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">abstract</span><span style="color: #000000"> T Sub(T x, T y); } </span><span style="color: #808080">///</span> <span style="color: #808080">&lt;summary&gt;</span> <span style="color: #808080">///</span><span style="color: #008000"> 派生類中實際的種類完成 </span><span style="color: #808080">///</span> <span style="color: #808080">&lt;/summary&gt;</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> IntCalcu : Calcu&lt;<span style="color: #0000ff">int</span>&gt;<span style="color: #000000"> { </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">int</span> Add(<span style="color: #0000ff">int</span> x, <span style="color: #0000ff">int</span><span style="color: #000000"> y) { </span><span style="color: #0000ff">return</span> x <span style="color: #000000"> y; } </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">int</span> Sub(<span style="color: #0000ff">int</span> x, <span style="color: #0000ff">int</span><span style="color: #000000"> y) { </span><span style="color: #0000ff">return</span> x -<span style="color: #000000"> y; } }</span></pre> </div> <p>四、總結</p> <p>這種泛型類和泛型方法<span id="mt2" class="sentence SentenceHover">將一個或好幾個種類的特定延遲到手機客戶端編碼申明并實例化此類或方式的情況下。?<span id="mt3" class="sentence">比如,根據應用泛型類型主要參數 T,您能夠撰寫別的手機客戶端編碼可以應用的單獨類,而不至于導入運作時強制轉換或裝箱實際操作的成本費或風險性。在構架中有句話是讓一切能延遲時間的延遲時間。</span></span></p> <p>?</p> Sun, 08 Nov 2020 21:46:11 +0800C# wx獲取token基本方法http://kf.icp123.net/sjk/8728.html <div class="cnblogs_code"> <pre><span style="color: #0000ff">#region</span> 請求Url,不發送數據 <span style="color: #808080">///</span> <span style="color: #808080">&lt;summary&gt;</span> <span style="color: #808080">///</span><span style="color: #008000"> 請求Url,不發送數據 </span><span style="color: #808080">///</span> <span style="color: #808080">&lt;/summary&gt;</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">string</span> RequestUrl(<span style="color: #0000ff">string</span><span style="color: #000000"> url) { </span><span style="color: #0000ff">return</span> RequestUrl(url, <span style="color: #800000">"</span><span style="color: #800000">POST</span><span style="color: #800000">"</span><span style="color: #000000">); } </span><span style="color: #0000ff">#endregion</span> <span style="color: #0000ff">#region</span> 請求Url,不發送數據 <span style="color: #808080">///</span> <span style="color: #808080">&lt;summary&gt;</span> <span style="color: #808080">///</span><span style="color: #008000"> 請求Url,不發送數據 </span><span style="color: #808080">///</span> <span style="color: #808080">&lt;/summary&gt;</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">string</span> RequestUrl(<span style="color: #0000ff">string</span> url, <span style="color: #0000ff">string</span><span style="color: #000000"> method) { </span><span style="color: #008000">//</span><span style="color: #008000"> 設置參數</span> <span style="color: #0000ff">var</span> request = WebRequest.Create(url) <span style="color: #0000ff">as</span><span style="color: #000000"> HttpWebRequest; </span><span style="color: #0000ff">var</span> cookieContainer = <span style="color: #0000ff">new</span><span style="color: #000000"> CookieContainer(); request.CookieContainer </span>=<span style="color: #000000"> cookieContainer; request.AllowAutoRedirect </span>= <span style="color: #0000ff">true</span><span style="color: #000000">; request.Method </span>=<span style="color: #000000"> method; request.ContentType </span>= <span style="color: #800000">"</span><span style="color: #800000">text/html</span><span style="color: #800000">"</span><span style="color: #000000">; request.Headers.Add(</span><span style="color: #800000">"</span><span style="color: #800000">charset</span><span style="color: #800000">"</span>, <span style="color: #800000">"</span><span style="color: #800000">utf-8</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008000">//</span><span style="color: #008000">發送請求并獲取相應回應數據</span> <span style="color: #0000ff">var</span> response = request.GetResponse() <span style="color: #0000ff">as</span><span style="color: #000000"> HttpWebResponse; </span><span style="color: #008000">//</span><span style="color: #008000">直到request.GetResponse()程序才開始向目標網頁發送Post請求</span> Stream responseStream =<span style="color: #000000"> response.GetResponseStream(); </span><span style="color: #0000ff">var</span> sr = <span style="color: #0000ff">new</span><span style="color: #000000"> StreamReader(responseStream, Encoding.UTF8); </span><span style="color: #008000">//</span><span style="color: #008000">返回結果網頁(html)代碼</span> <span style="color: #0000ff">string</span> content =<span style="color: #000000"> sr.ReadToEnd(); </span><span style="color: #0000ff">return</span><span style="color: #000000"> content; } </span><span style="color: #0000ff">#endregion</span> <span style="color: #0000ff">#region</span> 獲取Json字符串某節點的值 <span style="color: #808080">///</span> <span style="color: #808080">&lt;summary&gt;</span> <span style="color: #808080">///</span><span style="color: #008000"> 獲取Json字符串某節點的值 </span><span style="color: #808080">///</span> <span style="color: #808080">&lt;/summary&gt;</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">string</span> GetJsonValue(<span style="color: #0000ff">string</span> jsonStr, <span style="color: #0000ff">string</span><span style="color: #000000"> key) { </span><span style="color: #0000ff">string</span> result = <span style="color: #0000ff">string</span><span style="color: #000000">.Empty; </span><span style="color: #0000ff">if</span> (!<span style="color: #0000ff">string</span><span style="color: #000000">.IsNullOrEmpty(jsonStr)) { key </span>= <span style="color: #800000">"</span><span style="color: #800000">\"</span><span style="color: #800000">"</span> + key.Trim(<span style="color: #800000">'</span><span style="color: #800000">"</span><span style="color: #800000">'</span>) + <span style="color: #800000">"</span><span style="color: #800000">\"</span><span style="color: #800000">"</span><span style="color: #000000">; </span><span style="color: #0000ff">int</span> index = jsonStr.IndexOf(key) + key.Length + <span style="color: #800080">1</span><span style="color: #000000">; </span><span style="color: #0000ff">if</span> (index &gt; key.Length + <span style="color: #800080">1</span><span style="color: #000000">) { </span><span style="color: #008000">//</span><span style="color: #008000">先截逗號,若是最后一個,截"}"號,取最小值</span> <span style="color: #0000ff">int</span> end = jsonStr.IndexOf(<span style="color: #800000">'</span><span style="color: #800000">,</span><span style="color: #800000">'</span><span style="color: #000000">, index); </span><span style="color: #0000ff">if</span> (end == -<span style="color: #800080">1</span><span style="color: #000000">) { end </span>= jsonStr.IndexOf(<span style="color: #800000">'</span><span style="color: #800000">}</span><span style="color: #800000">'</span><span style="color: #000000">, index); } result </span>= jsonStr.Substring(index, end -<span style="color: #000000"> index); result </span>= result.Trim(<span style="color: #0000ff">new</span> [] {<span style="color: #800000">'</span><span style="color: #800000">"</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span> <span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">\"</span><span style="color: #800000">'</span>}); <span style="color: #008000">//</span><span style="color: #008000">過濾引號或空格</span> <span style="color: #000000">} } </span><span style="color: #0000ff">return</span><span style="color: #000000"> result; } </span><span style="color: #0000ff">#endregion</span> <span style="color: #0000ff">#region</span> 驗證Token是否過期 <span style="color: #808080">///</span> <span style="color: #808080">&lt;summary&gt;</span> <span style="color: #808080">///</span><span style="color: #008000"> 驗證Token是否過期 </span><span style="color: #808080">///</span> <span style="color: #808080">&lt;/summary&gt;</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">bool</span> TokenExpired(<span style="color: #0000ff">string</span><span style="color: #000000"> access_token) { </span><span style="color: #0000ff">string</span> jsonStr =<span style="color: #000000"> RequestUrl(</span><span style="color: #0000ff">string</span>.Format(<span style="color: #800000">"</span><span style="color: #800000">https://api.weixin.qq.com/cgi-bin/menu/get?access_token={0}</span><span style="color: #800000">"</span><span style="color: #000000">, access_token)); </span><span style="color: #0000ff">if</span> (GetJsonValue(jsonStr, <span style="color: #800000">"</span><span style="color: #800000">errcode</span><span style="color: #800000">"</span>) == <span style="color: #800000">"</span><span style="color: #800000">42001</span><span style="color: #800000">"</span><span style="color: #000000">) { </span><span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span><span style="color: #000000">; } </span><span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">; } </span><span style="color: #0000ff">#endregion</span> <span style="color: #0000ff">#region</span> 獲取Token <span style="color: #808080">///</span> <span style="color: #808080">&lt;summary&gt;</span> <span style="color: #808080">///</span><span style="color: #008000"> 獲取Token </span><span style="color: #808080">///</span> <span style="color: #808080">&lt;/summary&gt;</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">string</span> GetToken(<span style="color: #0000ff">string</span> appid, <span style="color: #0000ff">string</span><span style="color: #000000"> secret) { </span><span style="color: #0000ff">string</span> strJson =<span style="color: #000000"> RequestUrl( </span><span style="color: #0000ff">string</span><span style="color: #000000">.Format( </span><span style="color: #800000">"</span><span style="color: #800000">https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid={0}&amp;secret={1}</span><span style="color: #800000">"</span><span style="color: #000000">, appid, secret)); </span><span style="color: #0000ff">return</span> GetJsonValue(strJson, <span style="color: #800000">"</span><span style="color: #800000">access_token</span><span style="color: #800000">"</span><span style="color: #000000">); } </span><span style="color: #0000ff">#endregion</span> <span style="color: #008000">//</span><span style="color: #008000">獲取Openid</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">string</span> GetOpenId(<span style="color: #0000ff">string</span> appid, <span style="color: #0000ff">string</span> secret, <span style="color: #0000ff">string</span><span style="color: #000000"> code) { </span><span style="color: #0000ff">string</span> strJson =<span style="color: #000000"> RequestUrl( </span><span style="color: #0000ff">string</span><span style="color: #000000">.Format( </span><span style="color: #800000">"</span><span style="color: #800000">https://api.weixin.qq.com/sns/jscode2session?appid={0}&amp;secret={1}&amp;js_code={2}&amp;grant_type=authorization_code</span><span style="color: #800000">"</span><span style="color: #000000">, appid, secret, code)); </span><span style="color: #008000">//</span><span style="color: #008000">LogUtil.WriteLog(strJson);</span> <span style="color: #0000ff">return</span> GetJsonValue(strJson, <span style="color: #800000">"</span><span style="color: #800000">openid</span><span style="color: #800000">"</span><span style="color: #000000">); }</span></pre> </div> <p>?</p> Sun, 08 Nov 2020 21:46:06 +0800ADO.NET中的DataSet和DataReaderhttp://kf.icp123.net/sjk/8727.html <p>ADO.NET出示2個目標用以查找關聯型數據并把它儲存在運行內存中,分別是DataSet和DataReader。DataSet出示運行內存中關聯數據的主要表現--包含表和順序、管束等表間的關聯的詳細數據結合。DataReader出示迅速、只往前、寫保護的來源于數據庫的數據流。</p> <p>應用DataSet時,一般應用DataAdapter(也可能是CommandBuilder)與數據源互動,用DataView對DataSet中的數據開展排序和過慮。DataSet能夠被承繼來創建加強類型的DataSet,用以曝露表、行、列做為加強類型目標特性。 <br>下邊的內容包括何時應用DataSet或DataReader,及其如何提升瀏覽他們所包括的數據,也包含如何提升DataAdapter和DataView的應用(也包含CommandBuilder)。 </p> <p><br>DataSet與DataReader的比照:<br>在設計方案應用軟件時,決策應用DataSet還是DataReader必須考慮到應用軟件必須的作用。 </p> <p>應用DataSet是為了更好地完成應用軟件的以下作用: <br>l 實際操作結果中的好幾個分離出來的表。 <br>2 實際操作來源于好幾個源(比如來源于好幾個數據庫、XML文檔和excel表的混和數據)的數據。 <br>3 在層中間互換數據或應用XML Web服務。與DataReader 不一樣,DataSet能被傳送到遠程控制手機客戶端。 <br>4 根據緩存多次重復使用同樣的行結合以提升 性能(比如排序、檢索或過慮數據)。 <br>5 每排實行很多的解決。在應用DataReader回到的行上開展拓展解決將使聯接存有的時間比必需的更久,進而減少高效率。 <br>6 應用XML實際操作(比如XSLT變換和Xpath查看)維護保養數據。 </p> <p>在應用軟件必須下列作用時應用DataReader: <br>l 不用緩存數據。 <br>2 已經解決的結果集很大而不可以所有放進運行內存中。 <br>3 必須快速一次性瀏覽數據,選用只往前的寫保護的方法。 <br>留意:當添充DataSet的情況下,DataAdapter應用DataReader。因而應用DataAdapter替代DataSet得到 的性能是節省了DataSet耗費的運行內存和拼裝DataSet所必須的周期時間。這類性能的提升 絕大多數是名存實亡的,因而你應該依據必須的作用為基本來做設計方案決策。 </p> <p>應用強類型DataSet的益處 <br>應用DataSet的另一個益處是它能被承繼用以創建強類型的DataSet。強類型DataSet的益處包含設計方案時的定期檢查強類型DataSet 的Visual Studio .NET句子添充。如果你為DataSet固定不動了考試大綱或關聯構造時,就能創建強類型DataSet,把列和行做為目標的特性而不是項的結合。比如,做為曝露消費者表的某一行的字段名的替代,你能曝露Customer目標的 Name特性。強類型的DataSet衍化自DataSet類,因而不容易放棄DataSet的一切作用,換句話說,強類型的DataSet還可以是遠程控制的,并做為數據關聯控制(比如DataGrid)的數據源出示。假如不清楚考試大綱,也可以根據應用一般的DataSet得到 益處,可是缺失了強類型 </p> <p>DataSet的附加特性 <br>在強類型DataSet中解決空值 <br>應用強類型DataSet時,你可以給DataSet 的XML考試大綱定義語言(XSD)作注釋以保證 強類型DataSet恰當的解決空(Null)的引入??罩担╪ullValue)注解使你可以用String.Empty這一特殊值替代DBNull、維持了空引入、或是造成一個出現異常。挑選在其中的哪一個取決于應用軟件的內容,默認設置狀況下碰到空引入將造成一個出現異常。 </p> <p>刷新DataSet中的數據 <br>假如你期待應用升級后的值從服務器更新數據集中化的值,應用DataAdapter.Fill。假如主鍵界定在數據表上,DataAdapter.Fill根據主鍵搭配新行,并把網絡服務器的數據改為已存有的行。被更新行的RowState設定為Unchanged,即便 在更新前它被改動過。留意假如給數據表界定了主鍵,DataAdapter.Fill加上新行很有可能反復主鍵值。 <br>假如期待用網絡服務器的當今值更新一個表,而且維持表中國銀行的更改,你務必優選應用DataAdapter.Fill組成它,添充一個新的數據表,然后將該數據表合拼(Merge)進一個數據集,并把preserveChanges值設成true。 </p> <p>在DataSet中檢索數據 <br>在一個數據集中化查看合乎特殊條件的行后,應用根據索引(index-based)的查詢表將提升 性能。給數據表特定主鍵(PrimaryKey)值時,就創建了一個索引。當以數據表創建數據主視圖(DataView)時也創建了索引。下邊是一些應用根據索引查詢的方法: <br>假如查看是在數據表的主鍵列上開展的,應用DataTable.Rows.Find替代DataTable.Select。 <br>查看非主鍵列,能夠應用數據主視圖來提升 好幾個數據查看的速率。當給數據主視圖加上排序時,將創建檢索時應用的索引。數據主視圖曝露了查看下一層數據表的Find和FindRows方式。 <br>假如你并不是查看表的排序主視圖,還可以根據為數據表創建數據主視圖得到 根據索引的查詢表的益處。留意假如你實行數據上的好幾個查看它是唯一的益處。假如你只實行單獨查看,必須創建索引的全過程將由于應用索引而減少了性能。</p> Sun, 08 Nov 2020 21:46:04 +0800国产黃色A片三級三級三級
<ruby id="xh35p"><ins id="xh35p"></ins></ruby>

      <noframes id="xh35p"><listing id="xh35p"><listing id="xh35p"></listing></listing>

        <address id="xh35p"></address>