<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7578039559471749169</id><updated>2024-10-05T10:04:57.327+08:00</updated><category term="Android"/><category term="C#"/><category term="ASP.NET MVC"/><category term="SQL"/><category term="MS-SQL"/><category term="ASP.NET Webform"/><category term="JQuery"/><category term="Entity Framework"/><category term="Visual Studio"/><category term="Echo"/><category term="ADO.NET"/><category term="CSS"/><category term="Critic"/><category term="Diary"/><category term="Java"/><category term="Thread"/><category term="DB"/><category term="Algorithm"/><category term="EndNote"/><category term="HTML"/><category term="Stored Procedure"/><category term="CopyPaste"/><category term="IIS"/><category term="SignalR"/><category term="Autofac"/><category term="CKIP"/><category term="GAE"/><category term="JSON"/><category term="Quartz.net"/><category term="WCF"/><category term="Weka"/><category term="WinForm"/><category term="Blogger"/><category term="Dapper"/><category term="Design Pattern"/><category term="Eclipse"/><category term="ExtJS"/><category term="SQLite"/><category term="SSRS"/><category term="StackOverflow"/><category term="UML"/><category term="WPF"/><category term="Web API"/><category term="Windows"/><category term="Windows server"/><category term="XML"/><category term="Zxing"/><category term="azure"/><category term="google-code-prettify"/><title type='text'>Life Online</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>215</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-4564866111181367421</id><published>2020-11-26T15:40:00.006+08:00</published><updated>2020-11-26T15:42:00.999+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="XML"/><title type='text'>【C#】XDocument Descendants() 取不到東西</title><content type='html'>&lt;pre class=&quot;prettyprint linenums:1&quot;&gt; string xml = @&quot;&amp;lt;?xml version=&#39;1.0&#39; encoding=&#39;utf-8&#39;?&amp;gt;
                &amp;lt;ComputerBuildReportRequest xmlns:i=&#39;http://www.w3.org/2001/XMLSchema-instance&#39; xmlns=&#39;http://schemas.ms.it.oem/digitaldistribution/2010/10&#39;&amp;gt;
                    &amp;lt;Bindings&amp;gt;
                        &amp;lt;Binding&amp;gt;
                            &amp;lt;ProductKeyID&amp;gt;1000000002&amp;lt;/ProductKeyID&amp;gt;
                            &amp;lt;HardwareHash&amp;gt;xxx&amp;lt;/HardwareHash&amp;gt;
                            &amp;lt;HXSpecifics&amp;gt;
                                &amp;lt;UnitPartNumber&amp;gt;123456-789&amp;lt;/UnitPartNumber&amp;gt;
                            &amp;lt;/HXSpecifics&amp;gt;
                        &amp;lt;/Binding&amp;gt;
                        &amp;lt;Binding&amp;gt;
                            &amp;lt;ProductKeyID&amp;gt;1000000003&amp;lt;/ProductKeyID&amp;gt;
                            &amp;lt;HardwareHash&amp;gt;yyy&amp;lt;/HardwareHash&amp;gt;
                            &amp;lt;HXSpecifics&amp;gt;
                                &amp;lt;UnitPartNumber&amp;gt;123456-787&amp;lt;/UnitPartNumber&amp;gt;
                            &amp;lt;/HXSpecifics&amp;gt;
                        &amp;lt;/Binding&amp;gt;
                      &amp;lt;/Bindings&amp;gt;
                &amp;lt;/ComputerBuildReportRequest&amp;gt;&quot;;
XDocument doc = XDocument.Parse(xml);
var elements = doc.Descendants(&quot;Binding&quot;); // empty
&lt;/pre&gt;
以前就碰過，一直懶得研究 XDocument 到底為什麼有時候會秀逗，直接改用 XmlDocument。
這次碰到非用不可的情況，就藉機深究下。&amp;nbsp;&lt;div&gt;&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;
&lt;br /&gt;
簡單說，有 namespace 的情況下查都要補 namespace 給它，非常智障。
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;string xml = @&quot;&amp;lt;?xml version=&#39;1.0&#39; encoding=&#39;utf-8&#39;?&amp;gt;
                &amp;lt;ComputerBuildReportRequest xmlns:i=&#39;http://www.w3.org/2001/XMLSchema-instance&#39; xmlns=&#39;http://schemas.ms.it.oem/digitaldistribution/2010/10&#39;&amp;gt;
                    &amp;lt;Bindings&amp;gt;
                        &amp;lt;Binding&amp;gt;
                            &amp;lt;ProductKeyID&amp;gt;1000000002&amp;lt;/ProductKeyID&amp;gt;
                            &amp;lt;HardwareHash&amp;gt;xxx&amp;lt;/HardwareHash&amp;gt;
                            &amp;lt;HXSpecifics&amp;gt;
                                &amp;lt;UnitPartNumber&amp;gt;123456-789&amp;lt;/UnitPartNumber&amp;gt;
                            &amp;lt;/HXSpecifics&amp;gt;
                        &amp;lt;/Binding&amp;gt;
                        &amp;lt;Binding&amp;gt;
                            &amp;lt;ProductKeyID&amp;gt;1000000003&amp;lt;/ProductKeyID&amp;gt;
                            &amp;lt;HardwareHash&amp;gt;yyy&amp;lt;/HardwareHash&amp;gt;
                            &amp;lt;HXSpecifics&amp;gt;
                                &amp;lt;UnitPartNumber&amp;gt;123456-787&amp;lt;/UnitPartNumber&amp;gt;
                            &amp;lt;/HXSpecifics&amp;gt;
                        &amp;lt;/Binding&amp;gt;
                      &amp;lt;/Bindings&amp;gt;
                &amp;lt;/ComputerBuildReportRequest&amp;gt;&quot;;
XDocument doc = XDocument.Parse(xml);
XNamespace df = doc.Root.Name.Namespace;
var elements = doc.Descendants(df + &quot;Binding&quot;); 
&lt;/pre&gt;
而且這用法還不接受$串字的語法糖，是真的棒。&amp;nbsp;&lt;div&gt;&amp;nbsp;ref: &lt;a href=&quot;https://stackoverflow.com/questions/7503276/xml-linq-descendants-returns-nothing&quot;&gt;https://stackoverflow.com/questions/7503276/xml-linq-descendants-returns-nothing&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/4564866111181367421/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2020/11/cxdocument-descendants.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/4564866111181367421'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/4564866111181367421'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2020/11/cxdocument-descendants.html' title='【C#】XDocument Descendants() 取不到東西'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-1851663008025696883</id><published>2020-06-27T21:37:00.001+08:00</published><updated>2020-06-27T21:37:25.933+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><title type='text'>【C#】Verify permission of specific directory</title><content type='html'>Actual verification of read permission to specific directory without opens a stream.&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;private static bool HasPermissionToReadFiles(string path)
{
    var readAllow = false;
    var readDeny = false;
    var firstFoundFile = Directory.EnumerateFiles(path, &quot;*.*&quot;, SearchOption.AllDirectories).FirstOrDefault();
    if (firstFoundFile == null)
    {
        throw new Exception(&quot;There is no file in provided path.&quot;);
    }
    var accessControlList = Directory.GetAccessControl(firstFoundFile);
    if (accessControlList == null)
        return false;
    var accessRules = accessControlList.GetAccessRules(true, true, typeof(SecurityIdentifier));
    if (accessRules == null)
        return false;

    foreach (FileSystemAccessRule rule in accessRules)
    {
        if ((FileSystemRights.Read &amp;amp; rule.FileSystemRights) != FileSystemRights.Read) continue;

        if (rule.AccessControlType == AccessControlType.Allow)
            readAllow = true;
        else if (rule.AccessControlType == AccessControlType.Deny)
            readDeny = true;
    }

    return readAllow &amp;amp;&amp;amp; !readDeny;
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/1851663008025696883/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2020/06/cverify-permission-of-specific-directory.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/1851663008025696883'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/1851663008025696883'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2020/06/cverify-permission-of-specific-directory.html' title='【C#】Verify permission of specific directory'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-8435703612694103304</id><published>2020-06-26T20:28:00.003+08:00</published><updated>2020-06-27T21:31:27.801+08:00</updated><title type='text'>【Powershell】Create task scheduler entry</title><content type='html'>用 PowerShell 建&amp;nbsp;scheduler task&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;CreateSSLCertificateSwitchSchedule.ps1&lt;br /&gt;
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;$trigger = @(

&amp;nbsp; &amp;nbsp; $(New-ScheduledTaskTrigger -AtStartup -RandomDelay (New-TimeSpan -Minutes 2)),

&amp;nbsp; &amp;nbsp; $(New-ScheduledTaskTrigger -Daily -At 3am))

$action = New-ScheduledTaskAction -Execute &quot;D:\bin\SwitchSSLCertificateBack\SwitchSSLCertificateBack.exe&quot;

Register-ScheduledTask -TaskName &quot;SwitchSSLCertificateBack&quot; -Trigger $trigger -User &quot;SYSTEM&quot; -Action $action
&lt;/pre&gt;
&lt;br /&gt;
Trigger 有每天凌晨3點跟開機後兩分鐘兩種。&lt;br /&gt;
&lt;br /&gt;
CreateSSLCertificateSwitchSchedule.bat，主要是第三列的 ps call&lt;br /&gt;
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;
cd /d &quot;%~dp0&lt;br /&gt;
Robocopy &quot;.&quot; &quot;D:\bin\SwitchSSLCertificateBack&quot;&lt;br /&gt;
Powershell.exe -executionpolicy remotesigned -File&amp;nbsp; CreateSSLCertificateSwitchSchedule.ps1&lt;br /&gt;
@echo off&lt;br /&gt;
echo The batch has been finished. Press any key to exit.&lt;br /&gt;
pause &amp;gt;nul
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/8435703612694103304/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2020/06/powershell-create-task-scheduler-entry.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/8435703612694103304'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/8435703612694103304'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2020/06/powershell-create-task-scheduler-entry.html' title='【Powershell】Create task scheduler entry'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-662957775562352724</id><published>2019-05-28T17:09:00.001+08:00</published><updated>2019-05-28T17:09:46.936+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="MS-SQL"/><title type='text'>【MS-SQL】Can&#39;t open .trc file generated by SQLDiag</title><content type='html'>想直接雙擊打開&amp;nbsp;SQLDiag 錄的 trc 但會碰到&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: #f4cccc;&quot;&gt;SQL Server Profiler&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: #f4cccc;&quot;&gt;Failed to open a file. Access is denied.&lt;/span&gt;&lt;/div&gt;
的錯誤視窗&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;似乎是因為 SQL Server Service 用 NT 帳號跑起來它做出來的檔案不會繼承權限(尤其你是 domain account 進電腦沒有本機 admin)。&lt;br /&gt;
&lt;br /&gt;
trc 檔右鍵&lt;br /&gt;
Properties &amp;gt; Security &amp;gt; Advanced &amp;gt; Permissions &amp;gt; Continue &amp;gt; Enable inheritance &amp;gt; OK&lt;br /&gt;
再跑就可以了。&lt;br /&gt;
&lt;br /&gt;
ref: &lt;a href=&quot;https://social.msdn.microsoft.com/Forums/sqlserver/en-US/4d51dd01-d0d0-4adb-a51e-2f7fa84be1d1/cant-open-trace-files?forum=databasedesign&quot;&gt;MSDN&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/662957775562352724/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2019/05/ms-sqlcant-open-trc-generated-by-sqldiag.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/662957775562352724'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/662957775562352724'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2019/05/ms-sqlcant-open-trc-generated-by-sqldiag.html' title='【MS-SQL】Can&#39;t open .trc file generated by SQLDiag'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-1198016241859585869</id><published>2019-05-28T16:03:00.000+08:00</published><updated>2019-05-28T16:04:39.753+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="MS-SQL"/><title type='text'>【MS-SQL】SQLDiag debug</title><content type='html'>要跑 SQLDiag 的時候發生錯誤，錄完之後沒產生 trc 檔，cmd 有以下錯誤。&lt;br /&gt;
&lt;span style=&quot;background-color: #f4cccc;&quot;&gt;&lt;machinename&gt;\&lt;instancename&gt; Error querying installation path for SQLDIAG on &lt;instancename&gt; . Function result: 2. Message: The system cannot find the file specified.&lt;/instancename&gt;&lt;/instancename&gt;&lt;/machinename&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;但這看不出 file specified 指什麼，這時候要先找到執行目錄下的&lt;br /&gt;
\internal\##MachineName_InstanceName_Run_sp_trace.OUT&lt;br /&gt;
裡面才會有錯誤的細節。&lt;br /&gt;
&lt;span style=&quot;background-color: #f4cccc;&quot;&gt;Msg 19062, Level 16, State 1, Server MachineName\InstanceName, Procedure sp_trace_create, Line 1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f4cccc;&quot;&gt;Could not create a trace file.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f4cccc;&quot;&gt;No active traces.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f4cccc;&quot;&gt;Output folder=C:\Program Files\Microsoft SQL Server\MSRS11.SQL12\Reporting Services\LOG&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f4cccc;&quot;&gt;Windows error occurred while running SP_TRACE_CREATE. Error = 0x80070003(The system cannot find the path specified.).&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
會發現目錄下缺了 LOG 這個資料夾(倒是有個叫 LogFiles，感覺就是寫的人沒溝通好)，所以手動補上這個資料夾即可。&lt;br /&gt;
&lt;br /&gt;
ref: 恆逸的講師&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/1198016241859585869/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2019/05/ms-sqlsqldiag-debug.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/1198016241859585869'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/1198016241859585869'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2019/05/ms-sqlsqldiag-debug.html' title='【MS-SQL】SQLDiag debug'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-4564274339271991959</id><published>2019-05-21T14:47:00.002+08:00</published><updated>2019-05-21T14:47:38.474+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="WCF"/><title type='text'>【WCF】500 InternalServerError The server encountered an error processing the request. See server logs for more details</title><content type='html'>&lt;div class=&quot;heading1&quot;&gt;
&lt;span style=&quot;background-color: #f4cccc;&quot;&gt;&lt;b&gt;Error Status Code:&lt;/b&gt; &#39;InternalServerError&#39;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;background-color: #f4cccc;&quot;&gt;&lt;b&gt;Details: &lt;/b&gt;The server encountered an error processing the request. 
Please see the server logs for more details.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
但 Visual Studio 沒有拋出任何 exception。
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
在 Service class 加以下 attribute&lt;br /&gt;
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;[ServiceBehavior(IncludeExceptionDetailInFaults = true)]&lt;/pre&gt;
※ 注意是加在 class，不是 method。&lt;br /&gt;
&lt;br /&gt;
再用 fiddler 的 web view 就可以看到被隱藏的錯誤訊息了(通常是參數序列化轉換的問題)。&lt;br /&gt;
&lt;br /&gt;
ref: &lt;a href=&quot;https://stackoverflow.com/questions/21803522/the-server-encountered-an-error-processing-the-request-see-server-logs-for-more&quot;&gt;SO&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/4564274339271991959/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2019/05/wcf500-internalservererror-server.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/4564274339271991959'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/4564274339271991959'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2019/05/wcf500-internalservererror-server.html' title='【WCF】500 InternalServerError The server encountered an error processing the request. See server logs for more details'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-7514525706599327477</id><published>2019-01-08T17:29:00.001+08:00</published><updated>2019-01-08T17:29:43.104+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Visual Studio"/><title type='text'>【Visual Studio】Unexpected exception Could not load file or assembly &#39;&#39; or one of its dependencies. </title><content type='html'>&lt;span style=&quot;background-color: #f4cccc;&quot;&gt;Unexpected exception Could not load file or assembly &#39;&#39; or one of its dependencies.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;主程式 compile 的 platform 跟 dll 不一致，如果 dll&amp;nbsp;選 x64 主程式也要相同，不能選 Any CPU。</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/7514525706599327477/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2019/01/visual-studiounexpected-exception-could.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/7514525706599327477'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/7514525706599327477'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2019/01/visual-studiounexpected-exception-could.html' title='【Visual Studio】Unexpected exception Could not load file or assembly &#39;&#39; or one of its dependencies. '/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-5891158890778135942</id><published>2018-12-19T16:31:00.005+08:00</published><updated>2018-12-19T16:31:45.720+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><title type='text'>【C#】Max concurrent tasks</title><content type='html'>&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;static void Main()
{
    Foo();
    Console.ReadLine();
}

private static async Task Foo()
{
    var urls = new[] {
        &quot;A&quot;,
        &quot;B&quot;,
        &quot;C&quot;,
        &quot;D&quot;,
        &quot;E&quot;,
    };
    var maxThreads = 4;
    var q = new ConcurrentQueue&amp;lt;string&amp;gt;(urls);
    var tasks = new List&amp;lt;Task&amp;gt;();
    for (int n = 0; n &amp;lt; maxThreads; n++)
    {
        tasks.Add(Task.Run(async () =&amp;gt;
        {
            while (q.TryDequeue(out string url))
            {
                Console.WriteLine($&quot;start {url}&quot;);
                await Task.Delay(3000);
                Console.WriteLine($&quot;end {url}&quot;);
            }
        }));
    }
    await Task.WhenAll(tasks);
}
&lt;/pre&gt;
ref: &lt;a href=&quot;https://markheath.net/post/constraining-concurrent-threads-csharp&quot;&gt;sound code&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/5891158890778135942/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2018/12/cmax-concurrent-tasks.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/5891158890778135942'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/5891158890778135942'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2018/12/cmax-concurrent-tasks.html' title='【C#】Max concurrent tasks'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-4169361155342161614</id><published>2018-11-26T18:43:00.000+08:00</published><updated>2018-11-26T18:43:40.933+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="MS-SQL"/><title type='text'>【MS-SQL】Cannot open backup device &#39;xxx&#39;. Operating system error 5(Access is denied.)</title><content type='html'>&lt;span style=&quot;background-color: #f4cccc;&quot;&gt;Cannot open backup device &#39;E:\folder-name\file-name.bak&#39;. Operating system error 5(Access is denied.).&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;SQL Server Service 沒有寫目的地資料夾的權限，補上即可</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/4169361155342161614/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2018/11/cannot-open-backup-device-efolder.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/4169361155342161614'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/4169361155342161614'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2018/11/cannot-open-backup-device-efolder.html' title='【MS-SQL】Cannot open backup device &#39;xxx&#39;. Operating system error 5(Access is denied.)'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-7206921472112732681</id><published>2018-11-24T17:24:00.000+08:00</published><updated>2018-11-24T20:06:41.173+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="ADO.NET"/><category scheme="http://www.blogger.com/atom/ns#" term="MS-SQL"/><title type='text'>【MS-SQL】Connection pool</title><content type='html'>&lt;span style=&quot;background-color: #f4cccc;&quot;&gt;Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;Connection pool&lt;br /&gt;
當一個方法用完了特定的 connection 時，其實它不會立刻被回收，而是會被放進 connection pool 保存。&lt;br /&gt;
&lt;br /&gt;
每個應用可以創造複數的 connection pools，取用哪個 pool 取決於 connection string 是否相同，這也是最好把 connection string 存在 config 中共用的理由之一。&lt;br /&gt;
&lt;br /&gt;
Connection pool 預設最多可以有 100 條 connection 存在，若是多緒共用相同的 pool，做的很慢沒有及時關閉 connection，pool 占滿，同 connection string 的其他命令等到超時就會出現開版的錯誤。&lt;br /&gt;
&lt;br /&gt;
根除這個問題應該要檢討業務邏輯的設定為什麼會造成額滿，是不是有關閉寫錯。&lt;br /&gt;
&lt;br /&gt;
若真的需求特殊可以調整 connection string 的 Max pool size 和 Min pool size 應對。&lt;br /&gt;
&lt;br /&gt;
Max pool size&lt;br /&gt;
調整 pool 最多可容納的 connection 數量，預設為 100。&lt;br /&gt;
&lt;br /&gt;
Min pool size&lt;br /&gt;
調整 pool 初始化時先幫你建好的 connection 數量，預設為 0。&lt;br /&gt;
&lt;br /&gt;
ref: &lt;a href=&quot;https://www.huanlintalk.com/2012/05/net-connection-pool.html&quot;&gt;蔡老師的文章&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/7206921472112732681/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2018/11/ms-sqlconnection-pool.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/7206921472112732681'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/7206921472112732681'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2018/11/ms-sqlconnection-pool.html' title='【MS-SQL】Connection pool'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-5113082018772824627</id><published>2018-11-15T18:35:00.004+08:00</published><updated>2018-11-15T18:37:09.037+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="MS-SQL"/><title type='text'>【MS-SQL】BACKUP LOG cannot be performed because there is no current database backup</title><content type='html'>&lt;span style=&quot;background-color: #f4cccc;&quot;&gt;BACKUP LOG cannot be performed because there is no current database backup&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
不確定是 shrink 還是改 log size 或其他什麼的，突然沒辦法用之前做的 full back 還原。&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhA6M1yB2kKtNJWpC5RWtiFcx13CCUXj8TQYjh_TG1S7nom-n5IlSZPhAOMvkcIq7Fa02sO-AfCxAn6AZYG5pLe1ln2mf_FCo0d6dMESOoiSnjXwXEABw3z2ymOarUeQhCsZkByxCqKgWM/s1600/de.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;736&quot; data-original-width=&quot;1333&quot; height=&quot;352&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhA6M1yB2kKtNJWpC5RWtiFcx13CCUXj8TQYjh_TG1S7nom-n5IlSZPhAOMvkcIq7Fa02sO-AfCxAn6AZYG5pLe1ln2mf_FCo0d6dMESOoiSnjXwXEABw3z2ymOarUeQhCsZkByxCqKgWM/s640/de.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
ref: &lt;a href=&quot;https://stackoverflow.com/a/29497004/1973651&quot;&gt;SO&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/5113082018772824627/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2018/11/ms-sqlbackup-log-cannot-be-performed.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/5113082018772824627'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/5113082018772824627'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2018/11/ms-sqlbackup-log-cannot-be-performed.html' title='【MS-SQL】BACKUP LOG cannot be performed because there is no current database backup'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhA6M1yB2kKtNJWpC5RWtiFcx13CCUXj8TQYjh_TG1S7nom-n5IlSZPhAOMvkcIq7Fa02sO-AfCxAn6AZYG5pLe1ln2mf_FCo0d6dMESOoiSnjXwXEABw3z2ymOarUeQhCsZkByxCqKgWM/s72-c/de.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-2025192021235817085</id><published>2018-11-15T15:05:00.001+08:00</published><updated>2018-11-15T18:36:58.034+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="MS-SQL"/><category scheme="http://www.blogger.com/atom/ns#" term="SQL"/><title type='text'>【MS-SQL】The transaction log for database &#39;DatabaseName&#39; is full due to &#39;LOG_BACKUP&#39;</title><content type='html'>&lt;span style=&quot;background-color: #f4cccc;&quot;&gt;The transaction log for database &#39;DatabaseName&#39; is full due to &#39;LOG_BACKUP&#39;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;交易紀錄爆炸，shrink 或增加上限。
&lt;br /&gt;
&lt;br /&gt;
Shrink
&lt;br /&gt;
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;USE &amp;lt;DatabaseName&amp;gt;;  
GO  
-- Truncate the log by changing the database recovery model to SIMPLE.  
ALTER DATABASE &amp;lt;DatabaseName&amp;gt;  
SET RECOVERY SIMPLE;  
GO  
-- Shrink the truncated log file to 1 MB.  
DBCC SHRINKFILE (&amp;lt;DatabaseName&amp;gt;_Log, 1);  
GO  
-- Reset the database recovery model.  
ALTER DATABASE &amp;lt;DatabaseName&amp;gt;
SET RECOVERY FULL;  
GO 
&lt;/pre&gt;
&lt;br /&gt;
上限
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWymMsS3gZp4Hi4KdfJH7YDN_2PAKoNSP2gxwD3FfYjPM9izcjcJ9z_-Dqq0iC9T8528OvodvS3su0iOXMShoJSjOMvrKVk4KMMkumdfZ6_ribB48ptqGr9RaH-JpRmGX11rYGv6wrIGg/s1600/demopng.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;499&quot; data-original-width=&quot;826&quot; height=&quot;386&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWymMsS3gZp4Hi4KdfJH7YDN_2PAKoNSP2gxwD3FfYjPM9izcjcJ9z_-Dqq0iC9T8528OvodvS3su0iOXMShoJSjOMvrKVk4KMMkumdfZ6_ribB48ptqGr9RaH-JpRmGX11rYGv6wrIGg/s640/demopng.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/2025192021235817085/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2018/11/tsqlthe-transaction-log-for-database.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/2025192021235817085'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/2025192021235817085'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2018/11/tsqlthe-transaction-log-for-database.html' title='【MS-SQL】The transaction log for database &#39;DatabaseName&#39; is full due to &#39;LOG_BACKUP&#39;'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWymMsS3gZp4Hi4KdfJH7YDN_2PAKoNSP2gxwD3FfYjPM9izcjcJ9z_-Dqq0iC9T8528OvodvS3su0iOXMShoJSjOMvrKVk4KMMkumdfZ6_ribB48ptqGr9RaH-JpRmGX11rYGv6wrIGg/s72-c/demopng.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-8982597370540811168</id><published>2018-11-07T16:23:00.003+08:00</published><updated>2018-11-07T17:13:35.031+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><title type='text'>【C#】0x string to byte[] and convert back</title><content type='html'>MD5 測試用到的&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;public static byte[] HexStrToByteArray(string hex)
{
    int NumberChars = hex.Length;
    byte[] bytes = new byte[NumberChars / 2];
    for (int i = 0; i &amp;lt; NumberChars; i += 2)
        bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
    return bytes;
}

public static string ByteArrayToHexStr(byte[] data)
{
    if (data == null)
        return string.Empty;
    if (data.Length == 0)
        return string.Empty;
    StringBuilder dataStr = new StringBuilder();

    dataStr.Append(&quot;0x&quot;);
    foreach (byte b in data)
        dataStr.Append(b.ToString(&quot;X2&quot;));
    return dataStr.ToString();
}
&lt;/pre&gt;
ref: &lt;a href=&quot;https://stackoverflow.com/questions/321370/how-can-i-convert-a-hex-string-to-a-byte-array&quot;&gt;to byte array&lt;/a&gt;, &amp;nbsp;&amp;nbsp;&lt;a href=&quot;https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa&quot;&gt;to hex string&lt;/a&gt;, &amp;nbsp;&amp;nbsp;&lt;a href=&quot;https://stackoverflow.com/questions/68677/how-can-i-print-a-binary-value-as-hex-in-tsql&quot;&gt;binary to hex string in TSQL&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/8982597370540811168/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2018/11/c0x-string-to-byte-and-convert-back.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/8982597370540811168'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/8982597370540811168'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2018/11/c0x-string-to-byte-and-convert-back.html' title='【C#】0x string to byte[] and convert back'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-4685009170473412805</id><published>2018-09-09T17:39:00.003+08:00</published><updated>2018-09-09T17:39:43.648+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><title type='text'>【C#】IProgress</title><content type='html'>IProgress 是類似輕量化版的 BackgroundWorker&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;它沒有 IsBusy, WorkComplete 事件等，要自己做，但我覺得比較直觀，貌似對 async 的支援也好點。&lt;br /&gt;
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;public partial class Form1 : Form
{
    IProgress&amp;lt;int&amp;gt; _progress;
    public Form1()
    {
        InitializeComponent();

        _progress = new Progress&amp;lt;int&amp;gt;(UpdateProgressBar);
    }

    private void UpdateProgressBar(int val)
    {
        pbProgress.Value = val;

        if (val == 100)
        {
            MessageBox.Show(&quot;Finished&quot;);
            pbProgress.Value = 0;
        }
    }

    private async void btnStart_Click(object sender, EventArgs e)
    {
        if (pbProgress.Value == 0)
        {
            await Task.Run(async () =&amp;gt;
            {
                for (int i = 0; i &amp;lt; 100; i++)
                {
                    _progress.Report(i + 1);
                    await Task.Delay(100 - i);
                }
            });
        }
    }
}
&lt;/pre&gt;
&lt;br /&gt;
ref: &lt;a href=&quot;https://github.com/iamshiao/MyProgressLab.git&quot;&gt;GitHub 範例&lt;/a&gt;(Form2是 backgroundworker，可以在 program 切換)&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/4685009170473412805/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2018/09/ciprogress.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/4685009170473412805'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/4685009170473412805'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2018/09/ciprogress.html' title='【C#】IProgress'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-3248124152485878878</id><published>2018-08-20T17:34:00.000+08:00</published><updated>2018-08-20T17:38:52.641+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="Thread"/><title type='text'>【C#】Various kinds of lock</title><content type='html'>除了標準的 lock (){} 外，還有對應不同應用的其他鎖&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;根據同時是否允許複數的執行緒存取 critical section 可分為 exclusive 和 shared 兩類&lt;br /&gt;
&lt;br /&gt;
Exclusive&lt;br /&gt;
指一次只能有一個 thread 進入。&lt;br /&gt;
lock=Monitor、Mutex、Other EventWaitHandle&lt;br /&gt;
&lt;br /&gt;
Shared&lt;br /&gt;
一次可以有指定數量的 threads 進入。&lt;br /&gt;
Semaphore、ReadWriteLock&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;lock and Monitor&lt;/b&gt;&lt;br /&gt;
基本上 lock(){}就是
&lt;br /&gt;
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;var temp = obj;
Monitor.Enter(temp);
try
{
   body
}
finally
{
    Monitor.Exit(temp);
}
&lt;/pre&gt;
的語法糖，如果沒有要用 Monitor.TryEnter() 設 timeout 的話都用 lock{}就可以。&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Mutex&lt;/b&gt;&lt;br /&gt;
以下是用作鎖的例子並附上同屬 EventWaitHandle 的&amp;nbsp;AutoResetEvent 交互參考，基本上 Mutex 的主要用途是&lt;b&gt;跨 Processes 同步&lt;/b&gt;，沒有此需求就不要用它浪費資源了。&lt;br /&gt;
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;//static AutoResetEvent _ewh = new AutoResetEvent(true);
static Mutex _mut = new Mutex();
static bool _taken = false;

static void Main()
{
    for (int i = 0; i &amp;lt; 5; i++)
    {
        Task.Run(async () =&amp;gt;
        {
            await Task.Delay(new Random().Next(100, 500));
            Take();
        });
    }

    Console.ReadLine();
}

private static void Take()
{
    //_ewh.WaitOne();
    _mut.WaitOne();
    if (_taken)
    {
        Console.WriteLine($&quot;{Thread.CurrentThread.ManagedThreadId} attempt failed, resource was taken&quot;);
    }
    else
    {
        _taken = true;
        Console.WriteLine($&quot;{Thread.CurrentThread.ManagedThreadId} took the resource&quot;);
    }

    //_ewh.Set();
    _mut.ReleaseMutex();
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Semaphore&lt;/b&gt;&lt;br /&gt;
這個跟 ReadWriteLock 都有 Slim 版，Slim 版不能跨 Processes，但資源消耗較小，一般會用 Slim 版。 用法類似 WaitHandle，但建構式處可指定進入緒的數量。&lt;br /&gt;
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;static SemaphoreSlim sphs = new SemaphoreSlim(3);
static BlockingCollection&amp;lt;string&amp;gt; resources = new BlockingCollection&amp;lt;string&amp;gt;() { &quot;res1&quot;, &quot;res2&quot;, &quot;res3&quot;, &quot;res4&quot;, &quot;res5&quot;, &quot;res6&quot;, &quot;res7&quot;, &quot;res8&quot;, &quot;res9&quot; };

static void Main()
{
    var resCount = resources.Count;
    for (int i = 0; i &amp;lt; resCount; i++)
    {
        Task.Run(async () =&amp;gt;
        {
            await Take();
        });
    }

    Console.ReadLine();
}

private async static Task Take()
{
    Console.WriteLine($&quot;{Thread.CurrentThread.ManagedThreadId} is waiting&quot;);
    sphs.Wait();
    if (resources.Any())
    {
        var res = resources.Take();
        Console.WriteLine($&quot;{Thread.CurrentThread.ManagedThreadId} takes the {res}&quot;);

    }
    else
    {
        Console.WriteLine($&quot;{Thread.CurrentThread.ManagedThreadId} attempt failed, all resources was taken&quot;);
    }
    await Task.Delay(new Random().Next(500, 1000));

    Console.WriteLine($&quot;{Thread.CurrentThread.ManagedThreadId} released&quot;);
    sphs.Release();
}
&lt;/pre&gt;
我一直覺得 WaitHandle 這個家族的用法語意很差，但不知道為什麼很多類都是參考這個風格做的，搞得多緒每次寫每次查。&lt;br /&gt;
&lt;br /&gt;
ref: &lt;a href=&quot;http://arthurmvc.blogspot.com/2013/09/netlock.html&quot;&gt;亞斯狼(鎖差別一覽表)&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/3248124152485878878/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2018/08/cvarious-kinds-of-lock.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/3248124152485878878'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/3248124152485878878'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2018/08/cvarious-kinds-of-lock.html' title='【C#】Various kinds of lock'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-3678775451778101185</id><published>2018-08-20T14:51:00.001+08:00</published><updated>2018-08-20T14:51:46.463+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="Thread"/><title type='text'>【C#】Interlocked</title><content type='html'>為多重執行緒共用的變數提供不可部分完成的作業 (Atomic Operation)。&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;以上為 MSDN 的解釋，白話一點講，&lt;b&gt;透過 Interlocked 提供的方法操作變數是執行緒安全的。&lt;/b&gt;&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
這個類通常用在基礎型別，所以常見的操作就是&amp;nbsp;Increment(相當於++)、Decrement(相當於--)、Exchange(相當於賦值)。&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/3678775451778101185/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2018/08/cinterlocked.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/3678775451778101185'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/3678775451778101185'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2018/08/cinterlocked.html' title='【C#】Interlocked'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-7642477605408042695</id><published>2018-08-19T21:07:00.001+08:00</published><updated>2018-11-02T10:12:34.234+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="Thread"/><title type='text'>【C#】Task</title><content type='html'>Task 是抽象化後的緒操作類(from the thread pool)&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;它的優點有&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;可以回傳值&lt;/li&gt;
&lt;li&gt;搭配 async&amp;nbsp;await 優化對 I/O bound 任務的行為&lt;/li&gt;
&lt;li&gt;語意改善&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;b&gt;回傳值&lt;/b&gt;&lt;br /&gt;
Thread 是沒有回傳值這回事的，要靠存取一個外部變數來達成類似的功能，但 Task 可以直接如以下操作&lt;br /&gt;
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;static void Main(string[] args)
{
    FooAsync();
    Console.ReadLine();
}

private static async void FooAsync()
{
    string msg = await Task.Run(() =&amp;gt;
     {
         return &quot;msg from task&quot;;
     });
    Console.WriteLine(msg);
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;await&amp;nbsp; an I/O bound task&lt;/b&gt;&lt;br /&gt;
簡單來說 CPU bound 是消耗本機 CPU 與相關資源程度來決定運算效能的任務，相對的 I/O bound 是即使提升了上述資源供給也沒有直觀幫助的任務；比方 web service，其速度取決於 伺服器要多久才能吐回資料，無關你自身的運算資源。&lt;br /&gt;
&lt;br /&gt;
標記方法為 async 搭配對 I/O 任務 await 可以迴避掉等待 output 占用 thread 的情況&lt;br /&gt;
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;static void Main(string[] args)
{
    FooAsync();
    Console.ReadLine();
}

private static async void FooAsync()
{
    string msg = await GetJson(&quot;https://jsonplaceholder.typicode.com/todos&quot;);
    Console.WriteLine(msg);
}

public static async Task&lt;string&gt; GetJson(string url)
{
    var client = new HttpClient();
    return await client.GetStringAsync(url);
}
&lt;/string&gt;&lt;/pre&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/7642477605408042695/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2018/08/threadtask.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/7642477605408042695'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/7642477605408042695'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2018/08/threadtask.html' title='【C#】Task'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-8880399755535138499</id><published>2018-08-19T20:23:00.001+08:00</published><updated>2018-08-19T20:23:42.650+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="Thread"/><title type='text'>【C#】Exception handling while multi-threading</title><content type='html'>&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;static void Main(string[] args)
{
    
    try
    {
        Task.Run(() =&amp;gt;
        {
            throw new Exception(&quot;occurs in task&quot;);
        });
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    Console.ReadLine();
}
&lt;/pre&gt;
執行上述程式你會發現 cmd 沒寫 ex.Message，原因是 exception handling 是 per thread 的，也就是說&lt;b&gt;在外緒發生的 exception 主緒 catch 不到&lt;/b&gt;。&lt;br /&gt;
&lt;br /&gt;
這有幾個解&lt;br /&gt;
1. 把 try catch 改包在裡面(這個最簡單)&lt;br /&gt;
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;static void Main(string[] args)
{
    Task.Run(() =&amp;gt;
    {
        try
        {
            throw new Exception(&quot;occurs in task&quot;);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    });
    Console.ReadLine();
}
&lt;/pre&gt;
&lt;br /&gt;
2. 靠&amp;nbsp;TaskStatus 判斷執行結果並挖掘其中的錯誤&lt;br /&gt;
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;var t = Task.Run(() =&amp;gt;
{
    throw new Exception(&quot;occurs in task&quot;);
})
.ContinueWith((result) =&amp;gt;
{
    if (result.Status == TaskStatus.Faulted)
    {
        foreach (var ex in result.Exception.InnerExceptions)
        {
            Console.WriteLine(ex.Message);
        }
    }
});
&lt;/pre&gt;
&lt;br /&gt;
3. 使用 await
&lt;br /&gt;
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;static void Main(string[] args)
{
    FooAsync();
    Console.ReadLine();
}

private static async Task FooAsync()
{
    try
    {
        await Task.Run(() =&amp;gt;
        {
            throw new Exception(&quot;occurs in task&quot;);
        });
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}
&lt;/pre&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/8880399755535138499/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2018/08/cexception-handling-while-multi.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/8880399755535138499'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/8880399755535138499'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2018/08/cexception-handling-while-multi.html' title='【C#】Exception handling while multi-threading'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-8039634424534173954</id><published>2018-08-19T17:51:00.003+08:00</published><updated>2018-08-19T19:55:30.193+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="Thread"/><title type='text'>【C#】Thread 101</title><content type='html'>&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;外緒最主要的功能是&lt;b&gt;防止一個耗時過長的任務 block 主緒&lt;/b&gt;導致 UI 不能操作且程式看起來像沒有回應。&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;多緒並不總是比較快&lt;/b&gt;，比方多個執行緒存取相同的資源，進行短時間的簡單計算，資源的分配與執行緒的調動成本最終可能比計算更高。&lt;br /&gt;
&lt;br /&gt;
執行緒的使用時機應該是&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;以外緒防止 UI hang 住&lt;/li&gt;
&lt;li&gt;同時大量獨立的要求&lt;/li&gt;
&lt;li&gt;需要知道進度&lt;/li&gt;
&lt;li&gt;耗時長的運算&lt;/li&gt;
&lt;li&gt;有優先度或順序性的運算&lt;/li&gt;
&lt;/ol&gt;
比方讀取成績、計算加權排名；&lt;br /&gt;
在A緒每完成讀取成績時就可以吐給使用者先看、並同時以B緒運算加權分數並加入排名樹。&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;前/背景緒
&lt;/b&gt;&lt;br /&gt;
由 new Thread() 建出的緒預設為前景緒，除非強制關閉，否則 Process 會等到所有前景緒結束才關閉。&lt;br /&gt;
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;static void Main(string[] args)
{
    Thread t = new Thread(() =&amp;gt;
    {
        for (int i = 0; i &amp;lt; 5; i++)
        {
            Console.WriteLine(i + 1);
            Thread.Sleep(1000);
        }
    });
    // t.IsBackground = true;
    t.Start();
    Console.ReadLine();
}
&lt;/pre&gt;
若按下 Enter 觸發 Console.ReadLine() cmd 也不會馬上關閉，而是等到 t 執行完，若 IsBackground = true 則即使 t 未執行完 cmd 也會馬上關閉。&lt;br /&gt;
一般我們會用的是背景緒。&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Thread Pool&lt;/b&gt;&lt;br /&gt;
提供已預先建好並納管的背景緒，降低執行緒的建構/回收成本。&lt;br /&gt;
一般會從這裡抽而不是自己建；從這抽出的緒完成任務後會回歸，降低反覆建置的成本。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/8039634424534173954/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2018/08/cthread.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/8039634424534173954'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/8039634424534173954'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2018/08/cthread.html' title='【C#】Thread 101'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-4538330126857320926</id><published>2018-07-03T17:54:00.004+08:00</published><updated>2018-07-03T17:54:45.819+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Diary"/><title type='text'>【Dairy】Word multilevel list convex</title><content type='html'>1.1 xxx&lt;br /&gt;
1.2 xxx&lt;br /&gt;
...&lt;br /&gt;
1.10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;xxx&lt;br /&gt;
凸一大格的問題&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHvgof5mVBwQLbaOqzfg6zUUO0qkd4YuFjdqJy8hCIFEDh2NKDGhHeAPe4v04f-wyKQ-eIQamq7qsUj_wvhP-SiPdPqUNrs_iTLC2v9t17jK7MkHnlOjgc6Xbp3YttZinfjuevk2Y8VEc/s1600/Image1.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;505&quot; data-original-width=&quot;579&quot; height=&quot;347&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHvgof5mVBwQLbaOqzfg6zUUO0qkd4YuFjdqJy8hCIFEDh2NKDGhHeAPe4v04f-wyKQ-eIQamq7qsUj_wvhP-SiPdPqUNrs_iTLC2v9t17jK7MkHnlOjgc6Xbp3YttZinfjuevk2Y8VEc/s400/Image1.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjywTEgd2FuFZJMTFkd24doVHjarsQzeEwHv-DjUijp6c57Enxn_g95RKto1DAeZtlg1EBFr3qWXCVrM0go983Daqc4YJFfKt8n4njbs1SagaDtKp5_lYw8qoWC4lA9wG9ellMZUuncJ6s/s1600/Image2.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;493&quot; data-original-width=&quot;633&quot; height=&quot;311&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjywTEgd2FuFZJMTFkd24doVHjarsQzeEwHv-DjUijp6c57Enxn_g95RKto1DAeZtlg1EBFr3qWXCVrM0go983Daqc4YJFfKt8n4njbs1SagaDtKp5_lYw8qoWC4lA9wG9ellMZUuncJ6s/s400/Image2.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/4538330126857320926/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2018/07/dairyword-multilevel-list-convex.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/4538330126857320926'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/4538330126857320926'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2018/07/dairyword-multilevel-list-convex.html' title='【Dairy】Word multilevel list convex'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHvgof5mVBwQLbaOqzfg6zUUO0qkd4YuFjdqJy8hCIFEDh2NKDGhHeAPe4v04f-wyKQ-eIQamq7qsUj_wvhP-SiPdPqUNrs_iTLC2v9t17jK7MkHnlOjgc6Xbp3YttZinfjuevk2Y8VEc/s72-c/Image1.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-6195851244193217748</id><published>2018-06-28T17:34:00.000+08:00</published><updated>2018-06-28T17:43:22.209+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="CopyPaste"/><category scheme="http://www.blogger.com/atom/ns#" term="SQL"/><title type='text'>【SQL】The transaction log for database is full due to &#39;LOG_BACKUP&#39;</title><content type='html'>交易紀錄滿了的處理&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;清紀錄
&lt;br /&gt;
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt; -- Truncate the log by changing the database recovery model to SIMPLE.  
ALTER DATABASE MyDB
SET RECOVERY SIMPLE;  
GO  
-- Shrink the truncated log file to 1 MB.  
DBCC SHRINKFILE (MyDB_log, 1);  
GO  
-- Reset the database recovery model.  
ALTER DATABASE MyDB
SET RECOVERY FULL;  
GO 
-- select name from sys.database_files; 查 log file name(沒改名的話一般都是 dbName_log) 
&lt;/pre&gt;
&lt;br /&gt;
ref: &lt;a href=&quot;https://stackoverflow.com/questions/21228688/the-transaction-log-for-database-is-full-due-to-log-backup-in-a-shared-host&quot;&gt;SO&lt;/a&gt;&lt;br /&gt;
擴充等其他應對：&lt;a href=&quot;https://docs.microsoft.com/zh-tw/sql/relational-databases/logs/troubleshoot-a-full-transaction-log-sql-server-error-9002?view=sql-server-2017&quot;&gt;MSDN&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/6195851244193217748/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2018/06/sqlthe-transaction-log-for-database-is.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/6195851244193217748'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/6195851244193217748'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2018/06/sqlthe-transaction-log-for-database-is.html' title='【SQL】The transaction log for database is full due to &#39;LOG_BACKUP&#39;'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-8123088481952948367</id><published>2018-06-25T13:47:00.004+08:00</published><updated>2018-08-19T19:28:04.818+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="Thread"/><title type='text'>【C#】Lock</title><content type='html'>讓多執行緒在同時無法存取同段程式&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;你的帳戶有100塊，你拿了金融卡去提100，但很巧的是，你媽也拿了你的本子去提100，如果兩人同時存取同帳戶，且都提款成功，那這個帳戶就變成-100了。&lt;br /&gt;
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;static decimal _balance = 100;
private static readonly object _lockObj = new object();

public static void Withdraw(decimal amount)
{
    //lock (_lockObj)
    //{
        if (amount &amp;gt; _balance)
        {
            throw new Exception(&quot;Insufficient funds&quot;);
        }
        _balance -= amount;
    //}
}

static void Main(string[] args)
{
    for (int i = 0; i &amp;lt; 5; i++)
    {
        Console.WriteLine(&quot;========================================&quot;);
        _balance = 100;

        var ta = Task.Run(() =&amp;gt;
        {
            try
            {
                Withdraw(100);
                Console.WriteLine(&quot;Task A withdraw&quot;);
            }
            catch (Exception ex)
            {
                Console.WriteLine($&quot;Task A {ex.Message}&quot;);
            }
        });

        var tb = Task.Run(() =&amp;gt;
        {
            try
            {
                Withdraw(100);
                Console.WriteLine(&quot;Task B withdraw&quot;);
            }
            catch (Exception ex)
            {
                Console.WriteLine($&quot;Task B {ex.Message}&quot;);
            }
        });

        Task.WaitAll(new Task[] { ta, tb });
        Console.WriteLine($&quot;balance: {_balance}&quot;);

        Console.WriteLine(&quot;========================================&quot;);
    }

    Console.ReadLine();
}&lt;/pre&gt;
試5次左右應該就有機會試出來。&lt;br /&gt;
&lt;br /&gt;
為了防止多緒發生這種情況，我們會使用 Lock 進行保護，lock 的種類依應用細節不同有蠻多的，但最基本款就是&lt;br /&gt;
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;lock(_lockObj)
{
    // critical section
}&lt;/pre&gt;
&lt;br /&gt;
第一次用 lock 很容易發生的誤會是以為 lock 的保護對象是資源，但實際上 lock 保護的對象是&lt;a href=&quot;https://zh.wikipedia.org/wiki/%E8%87%A8%E7%95%8C%E5%8D%80%E6%AE%B5&quot;&gt;存取該資源的程式碼(critical section)&lt;/a&gt;，如前 Demo 解註解，可以看到 lock {} 中的判斷領錢與實際扣錢的程式碼才是其保護對象。&lt;br /&gt;
&lt;br /&gt;
整體來說可以想成這樣&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_zQYQfkSeTuWutoNN2VingZBIAD-de4d5lZCyKsiN0DOswb_E0lMc8baBZmaWjf5Lt5XEhMk_lJD1BCefyN8mOUl7uidan8Wj-ynodtNBZLeHtTjJP7T5f1kj5Gs3vN3JLUX7uAjOFY0/s1600/door.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;226&quot; data-original-width=&quot;520&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_zQYQfkSeTuWutoNN2VingZBIAD-de4d5lZCyKsiN0DOswb_E0lMc8baBZmaWjf5Lt5XEhMk_lJD1BCefyN8mOUl7uidan8Wj-ynodtNBZLeHtTjJP7T5f1kj5Gs3vN3JLUX7uAjOFY0/s1600/door.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
至於 _lockObj 就只是要有個變數可以讓 lock statement 標現在有沒有 thread 已經進來了，細節參考: &lt;a href=&quot;https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/lock-statement&quot;&gt;lock&lt;/a&gt;。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/8123088481952948367/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2018/06/clock.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/8123088481952948367'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/8123088481952948367'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2018/06/clock.html' title='【C#】Lock'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_zQYQfkSeTuWutoNN2VingZBIAD-de4d5lZCyKsiN0DOswb_E0lMc8baBZmaWjf5Lt5XEhMk_lJD1BCefyN8mOUl7uidan8Wj-ynodtNBZLeHtTjJP7T5f1kj5Gs3vN3JLUX7uAjOFY0/s72-c/door.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-5224481133615802938</id><published>2018-06-24T23:29:00.002+08:00</published><updated>2018-08-19T19:28:14.452+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="Thread"/><title type='text'>【C#】Synchronize thread</title><content type='html'>多執行緒同步&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
某玩具需要 零件A、B組裝，零件由個別的產線製作，假設製作速度 A&amp;gt;B；那 A完成後就要在組裝台等 B 送來才能拼組。 在這個情境中 產線 A、B 即是 thread，生產零件是 thread 進行的任務；A 完成後等待 B 的這個行為就是多緒同步。&lt;br /&gt;
&lt;br /&gt;
傳統的方法&lt;br /&gt;
用 EventWaitHandle(也就是 &lt;b&gt;AutoResetEvent &lt;/b&gt;或&amp;nbsp;&lt;b&gt;ManualResetEvent&lt;/b&gt;) 控制子緒&lt;br /&gt;
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;static AutoResetEvent handleA = new AutoResetEvent(false);
static AutoResetEvent handleB = new AutoResetEvent(false);

static void Main(string[] args)
{
    for (int i = 0; i &amp;gt; 2; i++)
    {
        Task.Run(() =&amp;gt;
        {
            Console.WriteLine(&quot;Start to create component A&quot;);
            Thread.Sleep(2500);
            Console.WriteLine(&quot;Finished component A&quot;);

            handleA.Set();
        });

        Task.Run(() =&amp;gt;
        {
            Console.WriteLine(&quot;Start to create component B&quot;);
            Thread.Sleep(5000);
            Console.WriteLine(&quot;Finished component B&quot;);

            handleB.Set();
        });

        Console.WriteLine(&quot;Wait for components&quot;);
        handleA.WaitOne();
        handleB.WaitOne();
        // 上面兩行可以取代成 WaitHandle.WaitAll(new WaitHandle[] { handleA, handleB });
        Console.WriteLine(&quot;Assemble the toy&quot;);
        Console.WriteLine(&quot;Toy created&quot;);
    }

    Console.ReadLine();
}
&lt;/pre&gt;
每個需要停等的任務要有一個 handle，它的建構引數 initState = false 表示 unsignaled，在 unsignaled 的狀態下 handle.WaitOne() 會 block 當前的 thread，直到 handle.Set() 送出釋放訊號為止。&lt;br /&gt;
在 Demo 中我們在兩個 thread 開始後就用&amp;nbsp;handleA, B 都 WaitOne()把主緒 block 住，等待個別任務的最後一行 Set() 對 handle 個別解鎖；兩個 handle 都解鎖之後主緒即恢復運行。&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;AutoResetEvent vs&amp;nbsp;&amp;nbsp;ManualResetEvent&lt;/b&gt;&lt;br /&gt;
兩者的差異就就像字面上一樣，ManualResetEvent 在 Set() 之後需要手動 Reset() 調整回 unsignal 的狀態，否則 WaitOne() 將不會發生作用；把前面 Demo 的 AutoResetEvent 都改成 ManualResetEvent 即可觀察到第二 loop 時 WaitOne 未 block 主緒。&lt;br /&gt;
ref: &lt;a href=&quot;https://docs.microsoft.com/zh-tw/dotnet/csharp/programming-guide/concepts/threading/thread-synchronization&quot;&gt;MSDN&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Quick sum up&lt;br /&gt;
1. 一個任務，一個 handle&lt;br /&gt;
2. EventWaitHandle 的 ctor 一般是給 false，因為我們希望它一開始就是可以 block 緒的狀態&lt;br /&gt;
3. handle.WaitOne() 會 block 當前的 thread；handle.Set() 則會釋放&lt;br /&gt;
4. AutoResetEvent 比較常用，因為不用特意去 Reset()&lt;br /&gt;
&lt;br /&gt;
傳統方法不管是寫還是閱讀都不甚直觀，之所以還是提一下是因為常 mantain 到，來看看新方法吧!&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;TPL Task.WaitAll()&lt;/b&gt;&lt;br /&gt;
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;for (int i = 0; i &amp;lt; 2; i++)
{
    Task taskA = Task.Run(() =&amp;gt;
    {
        Console.WriteLine(&quot;Start to create component A&quot;);
        Thread.Sleep(2500);
        Console.WriteLine(&quot;Finished component A&quot;);
    });

    Task taskB = Task.Run(() =&amp;gt;
    {
        Console.WriteLine(&quot;Start to create component B&quot;);
        Thread.Sleep(5000);
        Console.WriteLine(&quot;Finished component B&quot;);
    });

    Console.WriteLine(&quot;Wait for components&quot;);
    Task.WaitAll(new Task[] { taskA, taskB });

    Console.WriteLine(&quot;Assemble the toy&quot;);
    Console.WriteLine(&quot;Toy created&quot;);
}
&lt;/pre&gt;
是不是好懂一百倍? 語意簡單到不用解釋。&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/5224481133615802938/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2018/06/csynchronize-thread.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/5224481133615802938'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/5224481133615802938'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2018/06/csynchronize-thread.html' title='【C#】Synchronize thread'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-6846584230444229925</id><published>2018-06-24T21:04:00.003+08:00</published><updated>2018-06-24T21:04:56.812+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><title type='text'>【C#】Generics</title><content type='html'>從之前寫的文章拆出來&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;
&lt;b&gt;Generics#2 (再談泛型)&lt;/b&gt;&lt;br /&gt;
泛型的功能是傳入 Class T 為類別或方法的參數；這提供我們一個彈性，就是在執行時期才決定某些東西的型別，就以前面的 List 為例，我們來看微軟的 src code&lt;br /&gt;
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;namespace System.Collections.Generic {
 
    using System;
    ...
    
    public class List&amp;lt;T&amp;gt; : IList&amp;lt;T&amp;gt;, ...
    {
        ...
        private T[] _items;
        ...

&lt;/pre&gt;
不重要的地方我截掉了，可以看到它是用普通陣列為底去實作的，對於 List 的操作(比方 Sort();)來說陣列元素是什麼型別完全沒有影響，但如果我們沒有 T，要定義 List 中的普通陣列要嘛就是 object[] _items 操作完出去在轉型，不然就是建構子就要傳具型別的陣列進來，像 List(SomeType[] array)。&lt;br /&gt;
&lt;br /&gt;
但對前者，問題是效能差(因為要轉型)，而且多了不必要的轉型錯誤可能性，後者則是泛用性低落，每多一個型別就被迫要寫一個多載，造成大量的程式碼重覆。所以在需要&lt;span style=&quot;background-color: #fff2cc;&quot;&gt;把型別當參數&lt;/span&gt;的這個背景需求下發展了泛型這個功能。</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/6846584230444229925/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2018/06/cgenerics.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/6846584230444229925'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/6846584230444229925'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2018/06/cgenerics.html' title='【C#】Generics'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7578039559471749169.post-5439407994355305849</id><published>2018-06-21T11:34:00.001+08:00</published><updated>2018-06-21T11:37:15.062+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="SQL"/><title type='text'>【SQL】Close single user mode</title><content type='html'>在 master 強制設 DB 回多使用者&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;
&lt;pre class=&quot;prettyprint linenums:1&quot;&gt;-- Find spid
SELECT&amp;nbsp;sd.[name], sp.spid, sp.login_time, sp.loginame&amp;nbsp;
FROM&amp;nbsp;sysprocesses sp&amp;nbsp;
INNER&amp;nbsp;JOIN&amp;nbsp;sysdatabases sd&amp;nbsp;on&amp;nbsp;sp.dbid = sd.dbid&amp;nbsp;&amp;nbsp;
WHERE&amp;nbsp;sd.[name] =&amp;nbsp;&amp;lt;DB_NAME&amp;gt;

KILL&amp;nbsp;&amp;lt;spid&amp;gt;
GO
SET&amp;nbsp;DEADLOCK_PRIORITY HIGH
GO
ALTER&amp;nbsp;DATABASE&amp;nbsp;&amp;lt;DB_NAME&amp;gt;&amp;nbsp;SET&amp;nbsp;MULTI_USER&amp;nbsp;WITH&amp;nbsp;ROLLBACK&amp;nbsp;IMMEDIATE
GO&amp;nbsp; 
&lt;/pre&gt;
</content><link rel='replies' type='application/atom+xml' href='http://iamshiao.blogspot.com/feeds/5439407994355305849/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://iamshiao.blogspot.com/2018/06/sqlclose-single-user-mode.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/5439407994355305849'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7578039559471749169/posts/default/5439407994355305849'/><link rel='alternate' type='text/html' href='http://iamshiao.blogspot.com/2018/06/sqlclose-single-user-mode.html' title='【SQL】Close single user mode'/><author><name>Circle Hsiao</name><uri>http://www.blogger.com/profile/05258767397298463170</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//1.bp.blogspot.com/-FmoK6KSTFtc/WlODRe0GzuI/AAAAAAAAE0A/FtfCOCir0uc6212trLvHEBhzm8sVA1ReQCK4BGAYYCw/s98/id.jpg'/></author><thr:total>0</thr:total></entry></feed>