我想使用Thread Form Send Mail [英] I want to use Thread Form Send Mail

查看:86
本文介绍了我想使用Thread Form Send Mail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个

公共线程邮件线程;



其次我称之为这样的线程



first
public Thread mailthread;

Second I call this thread like this

Mailthread = new Thread(() => sendmail());
            Mailthread.Start();









但是它返回错误:



调用线程无法访问此对象,因为另一个线程拥有它



注意我在表单中以相同的方式使用其他线程并且它正常工作





but it returns error :

"The calling thread cannot access this object because a different thread owns it"

Notice i am useing other thread in same way in the form and it works

推荐答案

如果你在Windows Forms,WPF或Silverlight中,你需要回到用于访问数据的相同线程。我的猜测是你的sendmail()方法尝试访问带有邮件信息的文本块。



你应该做的是将消息信息包装成一个class并将其传递给ParamaterizedThreadStart作为参数。

这样你就不需要调用控件来获取信息了。



但是你必须直接访问控件:

WindowsForms:

Control.Invoke - >这将让你回到主题



WPF

UiElemenet.Dispatcher.Invoke - >让你回到线程



Silverlight

App.Current.RootVisual.Dispatcher.Invoke - >让你回到主线程
If you are in Windows Forms, WPF or Silverlight you need to get back onto the same thread to access the data. My guess is that your sendmail() method tries to access a textblock with the information for the mail message.

What you should do is wrap the message information up into a class and pass it into a ParamaterizedThreadStart as the parameter.
This way you won't need to call controls to get the information later.

However should you have to access the controls directly:
WindowsForms:
Control.Invoke -> this will get you back on the thread

WPF
UiElemenet.Dispatcher.Invoke -> gets you back on the thread

Silverlight
App.Current.RootVisual.Dispatcher.Invoke -> gets you back on the main thread


这篇关于我想使用Thread Form Send Mail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆