1、WPF中线程定义与调度优先级设置
Thread thread = new Thread(new ThreadStart(Thread1));//使用自定义方法Thread声明线程
thread1.Priority = ThreadPriority.Lowest; //设置线程的调度优先级
thread1.Start(); //开启线程thread
static void Thread1()
{
Console.WriteLine("线程一");
}
C#的线程类设置线程优先级:https://blog.csdn.net/u010792238/article/details/30239299
- Highest 在具有任何其他优先级的线程之前(5,最高)
- AboveNormal 可以将Thread安排在具有highest优先级线程之后,在Normal之前(4)
- Normal 在AboveNormal之后,BelowNormal之前。默认值。(3)
- BelowNormal 在Normal之后,Lowest之前(2)
- Lowest 在具有其他任何优先级的线程之后(1,最低)
2、WPF线程中对UI控件赋值的方法:
this.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate()
{
lblHello.Content = "欢迎你光临WPF的世界,Dispatche 同步方法 !!";
});
Dispatcher介绍:https://blog.csdn.net/albert528108/article/details/51503955
3、WPF写日志
static string strAppDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);//获取exe程序所在位置
string filename = strAppDir + @"\log\HMI.txt";
WriteLog("CraneAlarmShow_Tick(Ice)" + ex.Message, filename);
private static void WriteLog(string strMsg, string strLogPath)
{
string strInfo = "[" + DateTime.Now.ToString() + "] " + strMsg + "\r\r\n";
try
{
if (!File.Exists(strLogPath))
{
using (StreamWriter sw = File.CreateText(strLogPath))
{
sw.Write(strInfo);
}
}
else
{
FileInfo fi = new FileInfo(strLogPath);
//文件超过20M
if (fi.Length > 20 * 1024 * 1024)
{
using (StreamWriter sw = File.CreateText(strLogPath))
{
sw.Write(strInfo);
}
}
else
{
using (StreamWriter sw = File.AppendText(strLogPath))
{
sw.Write(strInfo);
}
}
}
}
catch (Exception ex)
{
//WriteLog("WriteLog:" + ex.Message, fileLogPath);
}
}
4、WPF错误定位
try
{
string curStockNo="";
curStockNo = curStockNo.Substring(0, 22);
}
catch (System.Exception ex)
{
string a= System.Text.RegularExpressions.Regex.Match(ex.StackTrace, @"行号\D+\d+").ToString();//定位错误行
MessageBox.Show(a);
WriteLog(string.Format("TaskScheduler_UnobservedTaskException Error encountered:{0}, 报错行:{1}", ex.Message, System.Text.RegularExpressions.Regex.Match(ex.StackTrace, @"行号\D+\d+").Value), filename);
}
5、VS 2017 打包C#窗体程序成可安装程序
参考链接:https://blog.csdn.net/sinat_33692536/article/details/108042487
6、常用十六进制颜色对照表代码查询
参考链接:https://yusi123.com/1090.html
7、WPF编程,后台设置字体、颜色的一种写法
参考链接:https://blog.csdn.net/qq_43307934/article/details/107180678?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-4.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-4.channel_param
8、WPF中在exe中打开另一个exe的方法
string baseDir = "E:\\jx\\AutoUpdate\\AutoUpdate\\UpdateApp\\bin\\Debug";
Process startProc = new Process();
startProc.StartInfo.FileName = System.IO.Path.Combine(baseDir, "UpdateApp.exe");
//就是你要打开的文件的详细路径
startProc.StartInfo.UseShellExecute = true;
startProc.StartInfo.WorkingDirectory = baseDir;
//就是如APGIS.Tools.exe 执行文件是在那个文件夹下。
startProc.Start();
9、WPF操作数据库的方法
-
增删改
OracleConnection cn = new OracleConnection(str); if (cn.State != ConnectionState.Open) cn.Open(); OracleCommand cmd = new OracleCommand(SqlStr, cn); int res = cmd.ExecuteNonQuery();//res为返回值,1成功,0失败
-
查
OracleConnection cn = new OracleConnection(str); OracleCommand cmd = new OracleCommand(sqlTxt, cn); if (cn.State != ConnectionState.Open) cn.Open(); OracleDataReader dr = cmd.ExecuteReader();
10、WPF定时器定义与定时退出任务
-
定义
private System.Windows.Threading.DispatcherTimer Timer1; Timer1 = new System.Windows.Threading.DispatcherTimer(); Timer1.Tick += new EventHandler(Timer1_Tick); Timer1.Interval = new TimeSpan(0, 0, 1); Timer1.Start(); //Timer1.Stop();//根据逻辑,可随时停止定时器
-
定时退出任务
private void Timer1_Tick(object sender, EventArgs e) { string SystemTime= DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); string SystemTime_HMS= SystemTime.Substring(SystemTime.Length - 8, 8); if (SystemTime_HMS == "16:47:00" || SystemTime_HMS== "20:00:00") { Logout_Click(null, null);//退出任务的按钮触发事件 } }
11、WPF程序单开的两种方式
-
在Window_Loaded进行处理
#region//防止重复点击exe,在Window_Loaded里面添加 System.Diagnostics.Process[] processes1 = System.Diagnostics.Process.GetProcessesByName("CarouselSamples.4"); //CarouselSamples.4是exe程序名 if (processes1.Length == 2)//出现两个名称相同的进程时,第二次的杀死自己 { foreach (Process ps in processes1) { ps.Kill(); return; } } #endregion
-
App.xaml.cs 里面添加
protected override void OnStartup(StartupEventArgs e) { mutex1 = new System.Threading.Mutex(true, "CarouselSamples.4"); //CarouselSamples.4是exe程序名 if (mutex1.WaitOne(0, false)) { base.OnStartup(e); } else { MessageBox.Show("程序已经在运行!", "提示"); this.Shutdown(); } }
12、Visual Studio2017 远程调试 Remote Debugger
- 从主机VS的安装目录中拷贝Remote Debugger文件夹至你要远程的服务器。可以放到任意位置,不影响程序运行,路径为 Program Files\Microsoft Visual Studio 14.0\Common7\IDE\Remote Debugger
- 远程的服务器中, Remote Debugger文件夹里面会有x86、x64两个文件夹对应相应版本的工具,根据服务器的系统环境进入相应的文件夹并以管理员身份运行里面的远程调试器msvsmon.exe。
- 工具>选项 中配置 验证模式(身份验证,两种模式都行)
- 运行本地项目,打开 调试>附加到进行 界面如下,我们只需要在连接和目标填上服务器IP和远程调试器选项中的端口号,然后点击查找。例子:133.3.3.71:4022
- 选择Windows身份验证和无身份验证的区别就多了一个身份验证,验证通过后我们会看到服务器的全部进程,选择显示所有用户的进程找到w3wp.exe,然后附加到进程,开始调试
- 参考链接:https://blog.csdn.net/a54654132/article/details/77914462
13、根据多个字符 分割字符串(split)
var str="USA|8888|ABC";
string[] array= str.Split(new string[] { "|" }, StringSplitOptions.None);
结果:array={"USA","8888","ABC"}