Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加新的Demo #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions ijw.Demo.AsyncThread/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

namespace ijw.Demo.AsyncThread {
class Program {
static int count = 0;
private static string LargeString = null;
private static int count = 0;
private static string LargeString = string.Empty;
private static Random r = new Random();

static void Main(string[] args) {
Console.WriteLine("Main Start...");
while(count < 100) {
while (count < 100) {
count++;
dosomeIO(count);
}
Expand All @@ -37,25 +38,27 @@ private async static void dosomething(int i) {
}

private async static void dosomeIO(int i) {
Console.WriteLine("Thread " + Thread.CurrentThread.ManagedThreadId.ToString() + " start async work " + count.ToString());
int oldThread = Thread.CurrentThread.ManagedThreadId;
Console.WriteLine("Thread " + oldThread.ToString() + " start async work " + count.ToString() + " with string of " + LargeString.Length + "...");

using (StreamWriter sw = new StreamWriter("file"+i.ToString()+".txt", false)) {
await sw.WriteLineAsync(buildLargeString());
using (StreamWriter sw = new StreamWriter("file" + i.ToString() + ".txt", false)) {
await sw.WriteLineAsync(buildString());
}

//下面的代码,根据完成task的时间,有可能是在另外的线程中进行的
//即,await 前后的代码被分成了两个部分,所等待的操作如果没有马上完成就立即返回,待完成后抓一个线程继续进行后面的代码
//await内部在有必要的时候使用线程池,对异步开发者屏蔽了线程调用的细节
Console.WriteLine(" Thread " + Thread.CurrentThread.ManagedThreadId.ToString() + " finish async work " + i.ToString());

string buildLargeString() {
if (LargeString == null) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < 40000; j++) {
sb.Append(j.ToString());
}
LargeString = sb.ToString();
int newThread = Thread.CurrentThread.ManagedThreadId;
Console.WriteLine(" Thread " + newThread.ToString() + " finish async work " + i.ToString() + ". " + ((oldThread == newThread) ? String.Empty : "(Different thread)"));

string buildString()
{
StringBuilder sb = new StringBuilder();
var length = r.Next(4000);
for (int j = 0; j < length; j++) {
sb.Append(j.ToString());
}
LargeString = sb.ToString();
return LargeString;
}
}
Expand Down
6 changes: 6 additions & 0 deletions ijw.Demo.CoreConfigInWinform/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
</startup>
</configuration>
20 changes: 20 additions & 0 deletions ijw.Demo.CoreConfigInWinform/App.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ijw.Demo.CoreConfigInWinform {
public class App {
private readonly IMainForm _mainForm;

public App(IMainForm mainForm) {
this._mainForm = mainForm;
}
public void Run() {
Application.Run(this._mainForm as Form);
}
}
}
10 changes: 10 additions & 0 deletions ijw.Demo.CoreConfigInWinform/IMainForm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ijw.Demo.CoreConfigInWinform {
public interface IMainForm {
}
}
43 changes: 43 additions & 0 deletions ijw.Demo.CoreConfigInWinform/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions ijw.Demo.CoreConfigInWinform/MainForm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ijw.Demo.CoreConfigInWinform {
public partial class MainForm : Form, IMainForm {
private MainFormSettings _settings;

public MainFormSettings Settings { get => _settings; set => _settings = value; }

public MainForm() {
InitializeComponent();
}

public MainForm(MainFormSettings settings) : this() {
this.Settings = settings;
}

private void MainForm_Load(object sender, EventArgs e) {
this.Text = this.Settings.Name;
}
}
}
120 changes: 120 additions & 0 deletions ijw.Demo.CoreConfigInWinform/MainForm.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
11 changes: 11 additions & 0 deletions ijw.Demo.CoreConfigInWinform/MainFormSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ijw.Demo.CoreConfigInWinform {
public class MainFormSettings {
public string Name { get; set; }
}
}
55 changes: 55 additions & 0 deletions ijw.Demo.CoreConfigInWinform/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ijw.Demo.CoreConfigInWinform {
static class Program {
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() {
//创建窗体前必须先执行
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

// create service collection
var serviceCollection = new ServiceCollection();

//配置服务
ConfigureServices(serviceCollection);

// create service provider
var serviceProvider = serviceCollection.BuildServiceProvider();

// 启动App
serviceProvider.GetService<App>().Run();
}

private static void ConfigureServices(IServiceCollection serviceCollection) {
//设定配置文件
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("app-settings.json", false, true)
.Build();
//使用Options
serviceCollection.AddOptions();

//从配置文件的一个section生成一个强类型配置MainFormSettings
serviceCollection.Configure<MainFormSettings>(configuration.GetSection("MainForm"));

//把所有需要MainFormSettings实例的地方,都用下面的方式获取
serviceCollection.AddTransient(srv => srv.GetService<IOptionsSnapshot<MainFormSettings>>().Value);

// add app
serviceCollection.AddTransient<App>();
serviceCollection.AddTransient<IMainForm, MainForm>();
}
}
}
Loading