說明:BackgroundWorker是.NET framewrok 2.0時就有的控制項,方便用於UI thread外,另開thread背景執行耗時的作業,如下載網路上的資料等。
用法:
using System.ComponentModel;
//declare BackgroundWorker object in Field
private readonly BackgroundWorker worker = new BackgroundWorker();
//subscribe events & assign property
public MainWindow()
{
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.ProgressChanged += worker_ProgressChanged;
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
}
//下載button
private void btnDownload_Click(object sender, RoutedEventArgs e)
{
if (!worker.IsBusy)
{
//trigger DoWork event
worker.RunWorkerAsync();
}
else
{
MessageBox.Show("背景下載作業執行中,請稍候!");
return;
}
}
//取消下載button
private void btnCancelDownload_Click(object sender, RoutedEventArgs e)
{
worker.CancelAsync();
}
#region event handler method
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
//背景耗時作業
Download(sender as BackgroundWorker);
if (worker.CancellationPending)
{
e.Cancel = true;
return;
}
else
{
e.Result = "Download Completed";
}
}
//DoWork結束後觸發 屬UI Thread
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//從DoWorkEventArgs object取得傳遞引數e
//label binding
this.lblDownloadProgress.Content = (e.Cancelled) ? "Canceled" : e.Result.ToString();
}
//屬UI Thread
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//label binding
this.lblDownloadProgress.Content = e.UserState.ToString();
//progressbar binding
this.pbDownload.Value = e.ProgressPercentage;
}
#endregion
private void Download(BackgroundWorker worker)
{
for (int i = 0; i < data.Length; i++)
{
if (worker.CancellationPending)
{
return;
}
else
{
//觸發ProgressChanged event
worker.ReportProgress(i * 100 / data.Length, string.Format("{0} files download", i));
}
Thread.Sleep(100);
}
}
參考資料:
http://stackoverflow.com/questions/5483565/how-to-use-wpf-background-worker
https://msdn.microsoft.com/zh-tw/library/cc221403(VS.95).aspx
https://dotblogs.com.tw/billchung/2009/05/29/8592
2016年5月1日 星期日
2016年4月19日 星期二
[Visual Studio] 使用Visual Studio 2015從程式碼逆向工程產出類別圖(class diagram)
說明:
從Visual Studio 2010後,可從架構總管(architecture explorer),提供從程式碼逆向產出類別圖,但VS 2015拿掉了架構總管,那該怎麼產出類別圖?
作法:
使用類別檢視(VS -> 檢視 -> 類別檢視)。
切換至類別檢視,點開你的專案,會列出專案全部namespce,
再點開namespace,會列出包含的class,在cs檔上右鍵,選擇類別圖檢視,
VS就會新增ClassDiagram1.cd,上面就有該class的圖,
再將其他class拖曳至ClassDiagram1.cd,即可產出想要的類別圖。
參考資料:
https://blogs.msdn.microsoft.com/visualstudioalm/2014/10/24/upcoming-changes-in-visual-studio-architecture-and-design-tools/
從Visual Studio 2010後,可從架構總管(architecture explorer),提供從程式碼逆向產出類別圖,但VS 2015拿掉了架構總管,那該怎麼產出類別圖?
作法:
使用類別檢視(VS -> 檢視 -> 類別檢視)。
切換至類別檢視,點開你的專案,會列出專案全部namespce,
再點開namespace,會列出包含的class,在cs檔上右鍵,選擇類別圖檢視,
VS就會新增ClassDiagram1.cd,上面就有該class的圖,
再將其他class拖曳至ClassDiagram1.cd,即可產出想要的類別圖。
參考資料:
https://blogs.msdn.microsoft.com/visualstudioalm/2014/10/24/upcoming-changes-in-visual-studio-architecture-and-design-tools/
2016年2月22日 星期一
[.NET/WPF] 屬性'Content'設定了一次以上。
說明:
編輯WPF的xaml,使用GroupBox編輯控制項出現該錯誤。
原因:
GroupBox內只能有一個element,因此無論Panel或Grid都只能放一個,可以在Panel或Grid內放入子element即可。例:
<GroupBox>
<StackPanel>
<UniformGrid></UniformGrid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
</Grid>
</StackPanel>
</GroupBox>
參考資料:
http://stackoverflow.com/questions/1356036/groupbox-in-wpf-can-only-contain-one-element
編輯WPF的xaml,使用GroupBox編輯控制項出現該錯誤。
原因:
GroupBox內只能有一個element,因此無論Panel或Grid都只能放一個,可以在Panel或Grid內放入子element即可。例:
<GroupBox>
<StackPanel>
<UniformGrid></UniformGrid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
</Grid>
</StackPanel>
</GroupBox>
參考資料:
http://stackoverflow.com/questions/1356036/groupbox-in-wpf-can-only-contain-one-element
2016年2月16日 星期二
[Visual Studio] 如何自動同步程式發行版本與最小必要版本? (強制更新發行程式) / minimum required version auto-increment in the clickonce deployment
說明:
使用clickonce發行程式安裝檔,想強制更新client程式,需指定最小必要版本,因發行版本可自動遞增,如何讓最小必要版本同步發行版本? 讓client程式可自動更新。
做法:
1. 方案總管中的專案圖示 -> 右鍵 -> 卸載專案
2. 一樣專案圖示 -> 右鍵 -> 編輯xxx.csproj
3. 在<Project>與</Project>的tag中插入以下設定:
4. 儲存並關閉csproj -> 發行後檢視部署資訊清單(xxx.application) -> 檢視minimum required version是否有值且正確。
參考資料:
http://stackoverflow.com/questions/202491/automatically-increment-minimum-required-version-in-a-clickonce-deployment
http://stackoverflow.com/questions/202491/automatically-increment-minimum-required-version-in-a-clickonce-deployment
使用clickonce發行程式安裝檔,想強制更新client程式,需指定最小必要版本,因發行版本可自動遞增,如何讓最小必要版本同步發行版本? 讓client程式可自動更新。
做法:
1. 方案總管中的專案圖示 -> 右鍵 -> 卸載專案
2. 一樣專案圖示 -> 右鍵 -> 編輯xxx.csproj
3. 在<Project>與</Project>的tag中插入以下設定:
<Target Name="AutoSetMinimumRequiredVersion" BeforeTargets="GenerateDeploymentManifest">
<FormatVersion Version="$(ApplicationVersion)" Revision="$(ApplicationRevision)">
<Output PropertyName="MinimumRequiredVersion" TaskParameter="OutputVersion" />
</FormatVersion>
<FormatVersion Version="$(ApplicationVersion)" Revision="$(ApplicationRevision)">
<Output PropertyName="_DeploymentBuiltMinimumRequiredVersion" TaskParameter="OutputVersion" />
</FormatVersion>
</Target>
設定是將ApplicationVersion(如: 1.0.0.%2a)與ApplicationRevision(修訂號,如: 2)合併成最終發行版號,寫入最小必要版本,再寫入部署資訊清單內的最小必要版本屬性。4. 儲存並關閉csproj -> 發行後檢視部署資訊清單(xxx.application) -> 檢視minimum required version是否有值且正確。
參考資料:
http://stackoverflow.com/questions/202491/automatically-increment-minimum-required-version-in-a-clickonce-deployment
http://stackoverflow.com/questions/202491/automatically-increment-minimum-required-version-in-a-clickonce-deployment
2016年2月4日 星期四
[.NET] DateTimeOffset與DateTime的差異/ What's the difference between DateTimeOffset and DateTime
說明:在.NET framework中用來處理日期與時間的class有DateTimeOffset跟DateTime,究竟差在哪?
說明:
DateTime:好像最直覺用來處理時間的class,但有下列缺點:
● 轉UTC或LocalTime時,可能會與預期不符,如:
DateTime dt = new DateTime(2016,2,4,22,0,0);
dt.ToLocalTime().ToString("yyyy/MM/dd HH:mm:ss"); //顯示為2016/2/5 06:00:00
這是因為DateTime內只儲存Ticks(從0001/1/1起的時間單位)跟Kind,此時Kind為Unspecified,呼叫ToLocalTime()將dt視為UTC時間,依據本地電腦時區+8,因此出現該情形。若改為:
DateTime dt = DateTime.Now; //Kind為Local,代表本地時間,ToLocalTime()就ok。
● DateTime.CompareTo(),不會考慮時區,單純比較Ticks。
DateTimeOffset:特定時間需指定時區offset,如:
DateTimeOffset dt = new DateTimeOffset(2016,2,4, 22, 0, 0, new TimeSpan(8,0,0));
若有跨時區轉換,用DateTimeOffset是最保險的做法。
參考資料:
http://blogs.msdn.com/b/davidrickard/archive/2012/04/07/system-datetime-good-practices-and-common-pitfalls.aspx
說明:
DateTime:好像最直覺用來處理時間的class,但有下列缺點:
● 轉UTC或LocalTime時,可能會與預期不符,如:
DateTime dt = new DateTime(2016,2,4,22,0,0);
dt.ToLocalTime().ToString("yyyy/MM/dd HH:mm:ss"); //顯示為2016/2/5 06:00:00
這是因為DateTime內只儲存Ticks(從0001/1/1起的時間單位)跟Kind,此時Kind為Unspecified,呼叫ToLocalTime()將dt視為UTC時間,依據本地電腦時區+8,因此出現該情形。若改為:
DateTime dt = DateTime.Now; //Kind為Local,代表本地時間,ToLocalTime()就ok。
● DateTime.CompareTo(),不會考慮時區,單純比較Ticks。
DateTimeOffset:特定時間需指定時區offset,如:
DateTimeOffset dt = new DateTimeOffset(2016,2,4, 22, 0, 0, new TimeSpan(8,0,0));
若有跨時區轉換,用DateTimeOffset是最保險的做法。
參考資料:
http://blogs.msdn.com/b/davidrickard/archive/2012/04/07/system-datetime-good-practices-and-common-pitfalls.aspx
2015年12月30日 星期三
[VS/TFS] 連接TFS出現無法切換伺服器,忙碌中訊息/unable to switch server at this time, team explorer is busy
說明:
使用VS 2008安裝Team Explorer 2008與向上相容TFS 2010套件連接TFS 2010,在新增伺服器時,輸入TFS URL後,出現該錯誤訊息。
解法:
將輸入的URL最後的反斜線拿掉即可。例:http://[ServerName]:8080/tfs/,移除/。
因為Team Explorer 2008比較舊,無法處理/,造成無法連線。
可參閱[1]:
參考資料:
使用VS 2008安裝Team Explorer 2008與向上相容TFS 2010套件連接TFS 2010,在新增伺服器時,輸入TFS URL後,出現該錯誤訊息。
解法:
將輸入的URL最後的反斜線拿掉即可。例:http://[ServerName]:8080/tfs/,移除/。
因為Team Explorer 2008比較舊,無法處理/,造成無法連線。
可參閱[1]:
Warning: Although the Admin Console displays the URL with a final slash, "/", the older clients do not take handle the slash and will not connect.
It is possible to connect to the default Team Project Collection by providing the server name only. This method is limited, as it only allows connection to one of the Team Project Collections in a TFS Instance.
參考資料:
- http://blogs.msdn.com/b/team_foundation/archive/2010/04/13/compat-matrix-for-2010-rtm-team-foundation-server-to-team-explorer-2008-and-2005.aspx
- https://social.msdn.microsoft.com/Forums/zh-TW/7b912531-efee-470e-bcd1-0a3d44b814b5/unable-to-switch-servers-at-this-time-team-explorer-is-busy?forum=tfssetup
- http://blogs.msdn.com/b/jasonba/archive/2009/08/10/how-to-connect-to-a-tfs-2010-server-from-a-2008-team-explorer-client.aspx
[.NET/IIS] runtime修改ASP.NET專案的web.config,對request與session的影響?
說明:在Server上修改web.config或更改bin下的dll檔,對於request與session會有什麼影響?
參考如下:
會起新的Application Domain,Application Pool不動(working process不變)。
● app domain在卸除前,會處理完佇列中的request,新的request由新的app domain起來處理。
● in-process session會不見,因此建議將session存放於out-of-process,如session state server或DB。
參考資料:
https://blogs.msdn.microsoft.com/tess/2006/08/02/asp-net-case-study-lost-session-variables-and-appdomain-recycles/
http://huan-lin.blogspot.com/2010/11/app-pool-vs-app-domain.html
http://stackoverflow.com/questions/27705527/does-editing-a-web-config-file-trigger-an-overlapping-recycle-or-a-startstop-of
參考如下:
會起新的Application Domain,Application Pool不動(working process不變)。
● app domain在卸除前,會處理完佇列中的request,新的request由新的app domain起來處理。
● in-process session會不見,因此建議將session存放於out-of-process,如session state server或DB。
參考資料:
https://blogs.msdn.microsoft.com/tess/2006/08/02/asp-net-case-study-lost-session-variables-and-appdomain-recycles/
http://huan-lin.blogspot.com/2010/11/app-pool-vs-app-domain.html
http://stackoverflow.com/questions/27705527/does-editing-a-web-config-file-trigger-an-overlapping-recycle-or-a-startstop-of
訂閱:
文章 (Atom)