赞
踩
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
this.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate()
{
lblHello.Content = "欢迎你光临WPF的世界,Dispatche 同步方法 !!";
});
Dispatcher介绍:https://blog.csdn.net/albert528108/article/details/51503955
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); } }
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);
}
参考链接:https://blog.csdn.net/sinat_33692536/article/details/108042487
参考链接:https://yusi123.com/1090.html
参考链接: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
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();
增删改
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();
定义
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);//退出任务的按钮触发事件
}
}
在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();
}
}
var str="USA|8888|ABC";
string[] array= str.Split(new string[] { "|" }, StringSplitOptions.None);
结果:array={"USA","8888","ABC"}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。