解决方案 »
- 怎么将引用的字符作为参数名和参数值
- Image控件怎么设置使图片自动和网页大小一样啊?问个菜菜的问题。
- listview 问题
- 如何将多次查询结果绑定到一个DataGridView中?
- C#基础问题
- 一个自己写的公用函数,很多地方调用,出错的时候不知是哪一个调用出错,该怎么捕捉错误呢?
- C#初学者求助textbox.Invoke(resetMsgTxtCallBack, new object[] { textbox })
- 设置、获取AppDomain数据,然后测试,出现警告
- 自定义Attribute,类名是否必须以"Attribute"结尾?
- 如何使用C#将Excel导入到SQloServer中。谢谢,很着急
- 提取文本,循环操作
- SqlDataAdapter.Fill 总报错啊
这个Lambda 表达式是在方法中调用的,如何在之前声明呢?
嗯,主要是解决办法不知道。
即 request.GetResponse()
然后回调一个 EndGetAddressByGPS
return strAddress;
var start = DateTime.Now;
while(noReturn && DateTime.Now.Subtract(start).Seconds<30){Thread.Sleep(1);}
return strAddress;
void EndGetAddressByGPS(IAsyncResult asynchronousResult)
{
...
}strAddress 如果是给UI赋值,可以直接在里面做。但要用跨线程的Invoke。
比如:
Action<string> action = (str)=>{ this.Label1.Text = str; };
this.Label1.Invoke(action, strAddress);
在异步代码后加等待你这不是同步么?直接request.GetResponse()得了。费那劲
要返回值就必须同步,就不能用BeginGetResponse()。非要用BeginGetResponse(),就不能有返回值。
不知道你UI和这个类啥关系能把表示的控件往这个类里传吗?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace BeginInvoke
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
DataTable data = new DataTable();
data.Columns.Add("col1");
data.Columns.Add("col2");
data.Columns.Add("col3"); data.Rows.Add(new object[] { "11", "12", "" });
data.Rows.Add(new object[] { "21", "22", "" });
data.Rows.Add(new object[] { "31", "32", "" });
data.Rows.Add(new object[] { "41", "42", "" }); this.dataGridView1.DataSource = data;
} private void button1_Click(object sender, EventArgs e)
{
DataTable data = (DataTable)this.dataGridView1.DataSource;
YourSomeClass yourClass = new YourSomeClass();
yourClass.OnGetAddress += yourClass_OnGetAddress;
foreach (DataRow row in data.Rows)
{
//为了知道是哪一行数据获得了,我把行号传进去了。
var index = data.Rows.IndexOf(row);
yourClass.GetAddressByGPS(0, 0, (object)index);
}
} void yourClass_OnGetAddress(object arg1, string arg2)
{
DataTable data = (DataTable)this.dataGridView1.DataSource;
var index = (int)arg1;
data.Rows[index][data.Columns.Count-1] = arg2;
}
} public class YourSomeClass
{
public event Action<object, string> OnGetAddress; public void GetAddressByGPS(double longitude, double latitude, object vehicleInfo)
{
//你原来的逻辑
//我这里模拟你的BeginGetResponse
Action action = SomeWork;
action.BeginInvoke((ar) => {
string strAddress = "";
try
{
//...
strAddress = "GetAddressSuccess";
}
catch (Exception ex)
{
//...
strAddress = "GetAddressFailure";
} //这里我把vehicleInfo作为区分你需要更新数据的Key
if (OnGetAddress != null)
OnGetAddress(vehicleInfo, strAddress);
}, null);
} private void SomeWork()
{
Thread.Sleep(5000);
} }
}
我现在回宿舍了,明早上班我就测试下!看能否实现,不过感觉是可行的。。可能具体的细节到时还要请教你哦。。
调用类代码分别为:
using System;
using System.Threading;namespace ChuFaEvent
{
public class EveClass
{
public event Action<int> OnGetValue;
public void DealValue(int val)
{
Action action = DoSomeWork;
action.BeginInvoke((ar) =>
{ int temp = val * 100;
if (OnGetValue != null)
{
OnGetValue(temp);
}
} , null);
}
private void DoSomeWork()
{
Thread.Sleep(3000);
}
}
}窗体代码:
using System;
using System.Windows.Forms;namespace ChuFaEvent
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); }
private void button1_Click(object sender , EventArgs e)
{
EveClass Ec = new EveClass();
Ec.OnGetValue += new Action<int>(Ec_OnGetValue);
for (int i = 0; i < 10; i++)
{
Ec.DealValue(i);
} }
void Ec_OnGetValue(int obj)
{
this.Invoke((MethodInvoker)(() =>
{
listBox1.Items.Add(obj);
})); }
}
}效果如下:
Form侧在事件中更新自己控件的数据。但,另外一个问题在于怎么区别不同单元格返回的数据上。