SYD8801是一款低功耗高性能蓝牙低功耗SOC,集成了高性能2.4GHz射频收发机、32位ARM Cortex-M0处理器、128kB Flash存储器、以及丰富的数字接口。SYD8801片上集成了Balun无需阻抗匹配网络、高效率DCDC降压转换器,适合用于可穿戴、物联网设备等。具体可咨询:http://www.sydtek.com/
调用线程无法访问此对象,因为另一个线程拥有该对象的问题的解决办法
摘录于:http://blog.csdn.net/u011549779/article/details/53388795
这个问题出现的原因主要是线程间的访问限制导致的,如果想要访问可以通过委托来进行访问
this.userModeControl.Dispatcher.Invoke(
new Action(
delegate
{
this.userModeControl.IsEnabled = true;
}
));
解析:
出现上面的异常是因为多个线程在同时访问一个对象造成的,在网上查看了一些资料,说的是在C#2005后不再支持多线程直接访问界面的控件(界面创建线程与访问线程不是同一个线程),但是可以可以使用delegate来解决。
相应的解决方法如下:
WPF:Dispatcher.Invoke 方法,只有在其上创建 Dispatcher 的线程才可以直接访问DispatcherObject。若要从不同于在其上创建 DispatcherObject 的线程的某个线程访问 DispatcherObject,请对与 DispatcherObject 关联的 Dispatcher 调用 Invoke 或 BeginInvoke。需要强制线程安全的 DispatcherObject 的子类可以通过对所有公共方法调用 VerifyAccess 来强制线程安全。这样可以保证调用线程是在其上创建 DispatcherObject 的线程。
代码: this.lbl.Dispatcher.Invoke(new Action(()={ this.lbl.Text = this is a test!!!; }));
this.lbl.Dispatcher.Invoke(new Action(()={ this.lbl.Text = this is a test!!!; }));
Winfrom:Control.Invoke 方法 (Delegate),在拥有此控件的基础窗口句柄的线程上执行指定的委托。
代码: this.lbl.Invoke(new Action(()={ this.lbl.Text = this is a test!!!; }));
所以可以按照下面这样(修改theout方法的内容)来解决刚才的问题:
publicvoidtheout(objectsource,System.Timers.ElapsedEventArgse){this.gcc1.Dispatcher.Invoke(newAction(delegate{if(this.gcc1.Offset<1){this.gcc1.Offset+=0.1;}else{this.gcc1.Offset=0;}}));this.gcc2.Dispatcher.Invoke(newAction(delegate{if(this.gcc2.Offset<1){this.gcc2.Offset+=0.1;}else{this.gcc2.Offset=0;}}));} 这样就可以解决调用线程无法访问此对象,因为另一个线程拥有该对象的问题
C#读写EXCEL源码提示“office检测到此文件存在一个问题。为帮助保护您的计算机,不能打开此文件”的解决
摘录于:http://blog.csdn.net/chenhao0568/article/details/77676965
我发现excel最新的扩展名.xlsx不用打开文件在计算机里就可以直接搜里面的内容。但老的.xls就搜不到。
1.C#写的操作EXCEL应用 提示“office检测到此文件存在一个问题。为帮助保护您的计算机,不能打开此文件。 之前还正常,换个电脑就报错。
先手工打开Excel,会提示“受保护的视图”,不能编辑。点启用编辑就行了。再打开软件,正常了。
2.找不到引用microsoft.office.core
在项目引用中右击选择添加引用,选择COM里面选择Microft Office 12.0 object Library和Microft Excel 12.0 object Library分别点确定即可!同样如果要引用World选Microft World 12.0 object Library! 2003/2007共通处理方式 分别为11或12版本 添加.net中Microsoft.Office.Interop.excel; 添加.net中Office
3.源码示例
[csharp] view plain copy
- using System;
- using System.Reflection;
- using System.IO;
- using Microsoft.Office.Interop.Excel;
- namespace Excel
- {
- class Excel
- {
- public static int startRow =5;
- public static string jzmc = "";
- public static string id = "";
- public static Double count = 0;
- public static Array zw;
- public static bool WriteXls(string filename, System.Collections.ArrayList rowData, int sheetNum)
- {
- //启动Excel应用程序
- Microsoft.Office.Interop.Excel.Application xls = new Microsoft.Office.Interop.Excel.Application();
- // _Workbook book = xls.Workbooks.Add(Missing.Value); //创建一张表,一张表可以包含多个sheet
- //如果表已经存在,可以用下面的命令打开
- _Workbook book = xls.Workbooks.Open(filename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
- _Worksheet sheet;//定义sheet变量
- xls.Visible = false;//设置Excel后台运行
- xls.DisplayAlerts = false;//设置不显示确认修改提示
- //创建并写入数据到sheet
- try
- {
- sheet = (_Worksheet)book.Worksheets.get_Item(sheetNum);//获得第i个sheet,准备写入
- }
- catch (Exception ex)//不存在就增加一个sheet
- {
- sheet = (_Worksheet)book.Worksheets.Add(Missing.Value, book.Worksheets[book.Sheets.Count], 1, Missing.Value);
- }
- //sheet.Name = "第" + i.ToString() + "页";//设置当前sheet的Name
- sheet.get_Range("C3", Type.Missing).Cells.Value2=jzmc;
- sheet.get_Range("G3", Type.Missing).Cells.Value2=id;
- sheet.get_Range("I3", Type.Missing).Cells.Value2=count;
- for (int offset = 0; offset < 10; offset++)
- {
- string str = rowData[offset].ToString();
- sheet.Cells[startRow, offset + 1] = rowData[offset].ToString();
- }
- startRow++;//行位置向前加
- //将表另存为
- // book.SaveAs(filename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
- //如果表已经存在,直接用下面的命令保存即可
- book.Save();
- book.Close(false, Missing.Value, Missing.Value);//关闭打开的表
- xls.Quit();//Excel程序退出
- //sheet,book,xls设置为null,防止内存泄露
- sheet = null;
- book = null;
- xls = null;
- GC.Collect();//系统回收资源
- return true;
- }
- public static bool CreateFile(string filename)
- {
- try
- {
- //启动Excel应用程序
- Microsoft.Office.Interop.Excel.Application xls = new Microsoft.Office.Interop.Excel.Application();
- _Workbook book = xls.Workbooks.Add(Missing.Value); //创建一张表,一张表可以包含多个sheet
- xls.DisplayAlerts = false;//设置不显示确认修改提示
- //将表另存为
- book.SaveAs(filename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
- book.Close(false, Missing.Value, Missing.Value);//关闭打开的表
- xls.Quit();//Excel程序退出
- //sheet,book,xls设置为null,防止内存泄露
- xls = null;
- GC.Collect();//系统回收资源
- }
- catch (Exception ex)//不存在就退出
- {
- return false;
- }
- return true;
- }
- public static Array ReadXls(string filename, int index)//读取第index个sheet的数据
- {
- //启动Excel应用程序
- Microsoft.Office.Interop.Excel.Application xls = new Microsoft.Office.Interop.Excel.Application();
- //打开filename表
- _Workbook book = xls.Workbooks.Open(filename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
- try
- {
- _Worksheet sheet;//定义sheet变量
- xls.Visible = false;//设置Excel后台运行
- xls.DisplayAlerts = false;//设置不显示确认修改提示
- sheet = (_Worksheet)book.Worksheets.get_Item(index);//获得第index个sheet,准备读取
- Console.WriteLine(sheet.Name);
- int row = sheet.UsedRange.Rows.Count;//获取不为空的行数
- int col = sheet.UsedRange.Columns.Count;//获取不为空的列数
- jzmc = sheet.get_Range("C3", Type.Missing).Cells.Value2;
- id = sheet.get_Range("G3", Type.Missing).Cells.Value2;
- count = sheet.get_Range("I3", Type.Missing).Cells.Value2;
- zw = (Array)sheet.get_Range("B5", "B" + row).Cells.Value2; //获得区域数据赋值给Array数组,方便读取
- Array value = (Array)sheet.get_Range("A5", "F" + row).Cells.Value2; //获得区域数据赋值给Array数组,方便读取
- // Array value = (Array)sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[row, col]).Cells.Value2;
- book.Save();//保存
- book.Close(false, Missing.Value, Missing.Value);//关闭打开的表
- xls.Quit();//Excel程序退出
- //sheet,book,xls设置为null,防止内存泄露
- sheet = null;
- book = null;
- xls = null;
- GC.Collect();//系统回收资源
- return value;
- }
- catch (Exception ex)//不存在就退出
- {
- book.Close(false, Missing.Value, Missing.Value);//关闭打开的表
- xls.Quit();//Excel程序退出
- //sheet,book,xls设置为null,防止内存泄露
- book = null;
- xls = null;
- GC.Collect();//系统回收资源
- return null;
- }
- }
- public static void Copy(string path, string path2)
- {
- //string path = @"c:/temp/MyTest.txt";
- // string path2 = path + "temp";
- try
- {
- // Create the file and clean up handles.
- // using (FileStream fs = File.Create(path)) { }
- // Ensure that the target does not exist.
- File.Delete(path2);
- // Copy the file.
- // File.Copy(path, path2);
- // Console.WriteLine("{0} copied to {1}", path, path2);
- // Try to copy the same file again, which should succeed.
- File.Copy(path, path2, true);
- // Console.WriteLine("The second Copy operation succeeded, which was expected.");
- }
- catch
- {
- // Console.WriteLine("Double copy is not allowed, which was not expected.");
- }
- }
- public static void Test()
- {
- string Current;
- Current = Directory.GetCurrentDirectory();//获取当前根目录
- Array Data = ReadXls(Current + "\\JMO-PCBA-019.xls", 1);//读取test.xls的第一个sheet表
- foreach (string temp in Data)
- Console.WriteLine(temp);
- Console.ReadKey();
- }
- }
- }