2020年10月15日 星期四

VS 2017 C# change the name of output dll

 

change the name of output dll


Depending on which version of Visual Studio.NET you are running, try going to Project Properties -> Application, and then change the Assembly Name field. On earlier versions it might be in a different place but I think the Assembly Name field is still the one you are looking for.



2020年10月13日 星期二

在Html中的table選擇某一column (轉貼)

 

Select only certain text in an HTML table

Updated: 04/01/2018 by Computer Hope
select text in html table

In Mozilla Firefox, users may highlight only certain rows, columns, or cells of an HTML table by holding down the Ctrl key on the keyboard and click each cell individually. Once you have selected the appropriate cells, you can copy and paste their text into another program, such as a text editor or word processor. Additionally, clicking and dragging the mouse up or down a column, or left or right through a row achieves a similar effect.

Note

Unfortunately, this tip only works in Firefox. Other browsers, such as Chrome and Internet Explorer, do not support this feature.

2020年10月12日 星期一

Regular Expression 與 C# Regex

11 . 匹配除「\n」之外的任何單個字元。要匹配包括「\n」在內的任何字元,請使用像「[.\n]」的範式。
17 [xyz] 字符集合。匹配所包含的任意一個字元。
18 [^xyz] 負值字符集合。匹配未包含的任意字元。

注意:\d是只有匹配1個數字,並非多個。

參:https://jimmy0222.pixnet.net/blog/post/36958669

Do I need to explicitly close the StreamReader in C# when using it to load a file into a string variable?(轉貼)

 Example:

variable = new StreamReader( file ).ReadToEnd();

Is that acceptable?

47

No, this will not close the StreamReader. You need to close it. Using does this for you (and disposes it so it's GC'd sooner):

using (StreamReader r = new StreamReader("file.txt"))
{
  allFileText = r.ReadToEnd();
}

Or alternatively in .Net 2 you can use the new File. static members, then you don't need to close anything:

variable = File.ReadAllText("file.txt");

2020年10月6日 星期二

自動修正換行字元For windows and Linux to the same with windows

 C:\Users\<使用者帳戶>\.gitconfig

  [core]

        autocrlf = false

2020年9月25日 星期五

跨執行緒作業無效: 存取控制項 ... 時所使用的執行緒與建立控制項的執行緒不同(轉貼)

 

[C#] 控制項跨執行緒作業無效解決方法

  • 24801
  •  0

通常在開發winForm程式時,最容易碰到執行緒(thread)問題,而控制項(controls)的跨續問題也十分讓人頭痛,而通常出現的錯誤訊息,莫過於「跨執行緒作業無效: 存取控制項 ... 時所使用的執行緒與建立控制項的執行緒不同」。

通常在開發winForm程式時,最容易碰到執行緒(thread)問題,而控制項(controls)的跨續問題也十分讓人頭痛,而通常出現的錯誤訊息,莫過於「跨執行緒作業無效: 存取控制項 ... 時所使用的執行緒與建立控制項的執行緒不同」。

因為控制項(control)主要是存在UI Thread(通常是Main Thread)內,所以在非相同執行緒下要更新UI就會產生跨執行緒的錯誤。而一般常見的解法是用delegate搭配Invoke,缺點是不夠彈性,並且有時可能會不如預期的還是會發生跨執行序錯誤。google關鍵字「C# dalegate invoke」會找到很多類似例子。

然後我們從MSDN中 How to: Make Thread-Safe Calls to Windows Forms Controls 這篇文章中可以看到很多相關寫法,其中強調是thread-safe寫法的關鍵字在Controls.InvokeRequired 。

MSDN上對這個值的說明「這個值會指示是否由於呼叫端是在建立控制項之執行緒以外的執行緒,因此在進行控制項的方法呼叫時,應呼叫叫用 (Invoke) 方法。」

而為了更有效便利的使用Invoke跟InvokeRequired去執行跨執行緒控制項操作,我通常會把這個方法撰寫class的擴充方法,並且加入MethodInvoker 委派/lambda的應用。

使用方法與程式碼如下

public class main
{
    //參考用controls
    TextBox tb = new TextBox();

    void Func()
    {
        //使用lambda
        tb.InvokeIfRequired(() =>
        {
            tb.Text = "hello";
        });

        //使用Action
        tb.InvokeIfRequired(helloAction);
    }

    void helloAction()
    {
        tb.Text = "hello";
    }
}

//擴充方法
public static class Extension
{
    //非同步委派更新UI
    public static void InvokeIfRequired(
        this Control control, MethodInvoker action)
    {
        if (control.InvokeRequired)//在非當前執行緒內 使用委派
        {
            control.Invoke(action);
        }
        else
        {
            action();
        }
    }
}

 

MSDN參考資料:

How to: Make Thread-Safe Calls to Windows Forms Controls
https://msdn.microsoft.com/en-us/library/ms171728%28v=vs.80%29.aspx

Control.InvokeRequired 屬性
https://msdn.microsoft.com/zh-tw/library/system.windows.forms.control.invokerequired%28v=vs.110%29.aspx

Lambda 運算式
https://msdn.microsoft.com/zh-tw/library/vstudio/bb397687%28v=vs.110%29.aspx

MethodInvoker 委派
https://msdn.microsoft.com/zh-tw/library/system.windows.forms.methodinvoker%28v=vs.110%29.aspx