问题描述
我读过,人们建议在Xamarin的主UI线程上拨打电话以通过蓝牙连接到设备,如下所示:
Xamarin.Forms.Device.BeginInvokeOnMainThread(async () => {
await adapter.ConnectToDeviceAsync(targetDevice,new Plugin.BLE.Abstractions.ConnectParameters(),token);
});
但是,我想采取主动行动,并使用Polly针对该政策制定重试政策:
retryPolicy = Policy
.Handle<Exception>()
.WaitAndRetryAsync(
new[]{
TimeSpan.FromSeconds(1),TimeSpan.FromSeconds(2),TimeSpan.FromSeconds(5) },(result,timeSpan,retryCount,context) => {
Console.WriteLine("Exception inside polly: " + result.ToString());
});
像这样在BeginInvokeOnMainThread内部执行策略是否更好:
Xamarin.Forms.Device.BeginInvokeOnMainThread(async () => {
await retryPolicy.ExecuteAsync(async () => {
await adapter.ConnectToDeviceAsync(targetDevice,token);
});
});
或者,如果在BeginInvokeOnMainThread周围放置同步重试策略,将会看到更好的结果:
Polly.Policy.Handle<Exception>().Retry(3).Execute(() => {
Xamarin.Forms.Device.BeginInvokeOnMainThread(async () => {
await adapter.ConnectToDeviceAsync(targetDevice,token);
});
});
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)