posted @ 2012-02-20 17:44 张荣华 阅读(157) 评论(0) 编辑
posted @ 2012-04-11 11:28 张荣华 阅读(173) 评论(0) 编辑
今天在做一个Winform的项目时遇到了一个问题需要跨线程更新GUI,Winform默认是不允许跨线程更新GUI控件的,如果你这样做会报错,所以需要做一下变通,在我的解决方法中借鉴了Updating Your Form from Another Thread without Creating Delegates for Every Type of Update的代码,代码如下:
// 新建一个放扩展方法的类
public static class ExtensionMethod
{
public static TResult SafeInvoke<T, TResult>(this T isi, Func<T, TResult> call) where T : ISynchronizeInvoke
{
if (isi.InvokeRequired) {
IAsyncResult result = isi.BeginInvoke(call, new object[] { isi });
object endResult = isi.EndInvoke(result); return (TResult)endResult;
}
else
{
return call(isi);
}
}
public static void SafeInvoke<T>(this T isi, Action<T> call) where T : ISynchronizeInvoke
{
if (isi.InvokeRequired)
{
isi.BeginInvoke(call, new object[] { isi });
}
else
{
call(isi);
}
}
}
// 在类里这样应用
private void button1_Click(object sender, EventArgs e)
{
Thread thread = new Thread(new ThreadStart(startCalculation));
thread.Start();
}
private void startCalculation()
{
button1.SafeInvoke(d => d.Enabled = false);
for (double i = 0; i <= 10000000000; i++)
{
string textForLabel = (i) + "%";
lblProcent.SafeInvoke(d => d.Text = textForLabel);
var i1 = i;
progressBar1.SafeInvoke(d => d.Value = 10);
string labelText = lblProcent.SafeInvoke(d => d.Text);
}
this.SafeInvoke(d => d.refreshTree());
button1.SafeInvoke(d => d.Enabled = true);
}
private void refreshTree()
{
Thread.Sleep(10000);
this.treeView1.ExpandAll();
this.treeView1.Nodes[0].Nodes[1].Remove();
}
需要注意的startCalculation方法是在新线程里执行的,但SafeInvoke里调用的方法(如refreshTree)仍是在主线程里执行的,如果你在这些方法里有耗费资源的代码(如这里的Thread.Sleep(10000))时程序还是会有停止反应的假死状态的。
posted @ 2012-03-29 14:43 张荣华 阅读(96) 评论(1) 编辑
今天在听iOS开发讲座时,照着讲座的demo输入代码,尝试运行时遇到了" this class is not key value coding-compliant for the key digitPressed.' "的错误,经过一番搜索和排查发现是自己在设置IBAction和IBOutlet时有多余的连线,按下ctrl键检查控件的连接将多余的连续删除后程序正常运行。
posted @ 2012-03-16 23:12 张荣华 阅读(192) 评论(0) 编辑
今天在练习Learning Objective-c on Mac上的代码时输入了以下的代码,
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
//omitted
[pool drain];
return 0;
程序报错为"NSAutoreleasePool is unavailable: not available in automatic reference counting mode".
经过搜索得知Learning Objective-c on Mac使用的是比例老Xcode版本,而在Xcode4.2之后的版本中引入了ARC特性来自动管理内存,所以应该将下面的代码发为下面的样子。
@autoreleasepool{
//omitted.
}
PS:网上还有人建议在设置中关闭ARC来继续使用NSSAutoreleasePool,想不明白为什么有这个需求,难道是老代码用新Xcode来编译?如果是新项目的话还是尽量使用ARC吧,毕竟能自动管理内存是多美好的一件事啊。
posted @ 2012-03-15 10:03 张荣华 阅读(132) 评论(0) 编辑
正在做的一个Winform程序中引用资源文件中的图片时遇到错误如下:
未能找到任何适合于指定的区域性或非特定区域性的资源。请确保在编译时已将“EDSS.GEDSection.Properties.Resources”正确嵌入或链接到程序集“GEDSection”,或者确保所有需要的附属程序集都可加载并已进行了完全签名。
但是资源文件和资源文件中的图片明明是正确嵌入的,没有问题。最后经过搜索发现问题的根源在项目的属性文件中项目的默认命名空间还是原先的“ExploreDevelpe.GEDSection”,而不是现在该程序集下所有文件正在使用的默认命名空间的"EDSS.GEDSection"。修改默认命名空间后问解决。
PS:网上还有说如果有时不慎将bin/obj目录包含在项目文件中也会导致该bug。
posted @ 2012-03-09 12:07 张荣华 阅读(89) 评论(0) 编辑
posted @ 2012-02-29 13:52 张荣华 阅读(251) 评论(0) 编辑
posted @ 2012-02-27 17:39 张荣华 阅读(34) 评论(0) 编辑
posted @ 2012-02-23 15:24 张荣华 阅读(55) 评论(0) 编辑
posted @ 2012-02-20 17:44 张荣华 阅读(157) 评论(0) 编辑
posted @ 2012-02-14 15:30 张荣华 阅读(180) 评论(1) 编辑
posted @ 2012-02-07 13:43 张荣华 阅读(46) 评论(0) 编辑
posted @ 2012-01-07 12:40 张荣华 阅读(2196) 评论(11) 编辑
posted @ 2011-12-31 11:38 张荣华 阅读(223) 评论(3) 编辑
