我有一个公共库'datalib':
namespace DataLib
{
public class DataLib
{
public async static Task<List<String>> Daten()
{
//here is a http request witch call data from the internet in write it to file within isoStorage
//than the content of this files will be written into var1, var2, var3... to return them at the end
//than I creat an instans of an UserControl witch generate Images to the IsoStore using data from the IsoStore genarated at the beginning...
var TestTile = new Tiles();
//...and the tile will be updated here with the image form the isoStorage
return new List<String> { var1, var2 var3 ... };
}
}
UserControll"瓷砖"看起来像这样:
namespace DataLib
{
public partial class Tiles : UserControl
{
public Tiles()
{
InitializeComponent();
//some stuff to generate the image and write it into isoStorage
}
}
}
在应用程序中,我正在使用var1,var2,var3 ...像这样:
private async void Hauptfunktion()
{
List<String> DatenPacket = await DataLib.DataLib.Daten();
string var1 = DatenPacket[0].ToString();
string var2 = DatenPacket[0].ToString();
string var3 = DatenPacket[0].ToString();
...
}
所有这些都可以正常工作,但是现在我必须在背景代理中运行此漏洞这个代理" cwtaskagent"看起来像这样:
protected override async void OnInvoke(ScheduledTask task)
{
await DataLib.DataLib.Daten();
if (Debugger.IsAttached)
{
ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(30));
}
NotifyComplete();
}
遵循教程后,我在app.xaml.cs ...中具有此代码...
private void Application_Launching(object sender, LaunchingEventArgs e)
{
string taskName = "CWTaskAgent";
PeriodicTask oldTask = ScheduledActionService.Find(taskName) as PeriodicTask;
if (oldTask != null)
{
ScheduledActionService.Remove(taskName);
}
PeriodicTask task = new PeriodicTask(taskName);
task.Description = "Change Livetile";
ScheduledActionService.Add(task);
if (Debugger.IsAttached)
{
ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(10));
}
}
...以及wmappmanifest.xml中的此代码:
<ExtendedTask Name="CWTaskAgent">
<BackgroundServiceAgent Specifier="ScheduledTaskAgent" Name="CWTaskAgent" Source="CWTaskAgent" Type="CWTaskAgent.ScheduledAgent"/>
</ExtendedTask>
so,现在如果我在模拟器中运行它,我会在10秒钟左右后会出现两个错误,当代理人启动...
时'system..io.filenotfoundexception'类型的例外(第一次机会)发生在mscorlib.ni.dll中。
'system.unauthorizedAccessexceptio'类型的例外(第一次机会)发生在mscorlib.ni.dll中。
标记的线是'var testtile = new tiles();'在datalib.cs
中更新:用于阅读和编写文件的代码:
using (Mutex mutex1 = new Mutex())
{
mutex1.WaitOne();
try
{
IsolatedStorageFileStream WritingStream = new IsolatedStorageFileStream("TagMonatJahr.txt", FileMode.Create, DatenDateien);
StreamWriter writer = new StreamWriter(WritingStream);
writer.Write(DateTime.Now.ToString("ddMMyyyy"));
writer.Close();
}
finally
{
mutex1.ReleaseMutex();
}
}
using (Mutex mutex2 = new Mutex())
{
mutex2.WaitOne();
try
{
IsolatedStorageFileStream ReadingStream = new IsolatedStorageFileStream("TagMonatJahr.txt", FileMode.Open, DatenDateien);
StreamReader reader = new StreamReader(ReadingStream);
TagMonatJahr = reader.ReadToEnd();
reader.Close();
}
finally
{
mutex2.ReleaseMutex();
}
}
检查此线程。看起来您正在尝试使用UI代码而无需进行调度器。因此,尝试此代码:
protected override void OnInvoke(ScheduledTask task)
{
Deployment.Current.Dispatcher.BeginInvoke( async () =>
{
await DataLib.DataLib.Daten();
NotifyComplete();
});
}