Click here to Skip to main content
15,917,320 members
Home / Discussions / C#
   

C#

 
AnswerRe: Datagridview Binding Pin
Henry Minute2-Jul-09 11:17
Henry Minute2-Jul-09 11:17 
GeneralRe: Datagridview Binding Pin
ziwez02-Jul-09 21:53
ziwez02-Jul-09 21:53 
QuestionOutOfMemoryException Pin
qwerty532-Jul-09 8:39
qwerty532-Jul-09 8:39 
AnswerRe: OutOfMemoryException Pin
Christian Graus2-Jul-09 8:42
protectorChristian Graus2-Jul-09 8:42 
GeneralRe: OutOfMemoryException Pin
qwerty532-Jul-09 11:34
qwerty532-Jul-09 11:34 
AnswerRe: OutOfMemoryException Pin
Luc Pattyn2-Jul-09 8:44
sitebuilderLuc Pattyn2-Jul-09 8:44 
GeneralRe: OutOfMemoryException Pin
qwerty532-Jul-09 8:46
qwerty532-Jul-09 8:46 
AnswerRe: OutOfMemoryException Pin
Dave Kreskowiak2-Jul-09 8:47
mveDave Kreskowiak2-Jul-09 8:47 
GeneralRe: OutOfMemoryException Pin
qwerty532-Jul-09 9:15
qwerty532-Jul-09 9:15 
GeneralRe: OutOfMemoryException Pin
Henry Minute2-Jul-09 11:12
Henry Minute2-Jul-09 11:12 
GeneralRe: OutOfMemoryException Pin
Luc Pattyn2-Jul-09 11:18
sitebuilderLuc Pattyn2-Jul-09 11:18 
GeneralRe: OutOfMemoryException Pin
Henry Minute2-Jul-09 12:03
Henry Minute2-Jul-09 12:03 
GeneralRe: OutOfMemoryException Pin
Luc Pattyn2-Jul-09 12:17
sitebuilderLuc Pattyn2-Jul-09 12:17 
GeneralRe: OutOfMemoryException Pin
Henry Minute2-Jul-09 12:23
Henry Minute2-Jul-09 12:23 
AnswerRe: OutOfMemoryException Pin
harold aptroot2-Jul-09 11:38
harold aptroot2-Jul-09 11:38 
QuestionMSChart RangeColumn type chart [modified] Pin
JohnQuar12-Jul-09 7:23
JohnQuar12-Jul-09 7:23 
Questionhi friends Pin
jeyaluxmy2-Jul-09 6:17
jeyaluxmy2-Jul-09 6:17 
AnswerRe: hi friends Pin
Rajesh R Subramanian2-Jul-09 6:27
professionalRajesh R Subramanian2-Jul-09 6:27 
AnswerRe: hi friends Pin
led mike2-Jul-09 6:37
led mike2-Jul-09 6:37 
AnswerRe: hi friends Pin
Henry Minute2-Jul-09 6:40
Henry Minute2-Jul-09 6:40 
JokeRe: hi friends Pin
musefan2-Jul-09 6:48
musefan2-Jul-09 6:48 
QuestionC# threads and invoking Pin
Blue3652-Jul-09 6:15
Blue3652-Jul-09 6:15 
Hi all,

I am trying to invoke the forms main thread from a worker thred, but I am desperately trying to avoid Control.InvokeRequired. To better explain, here is the code:

Simple class: (MyClass.cs)
public class MyClass
{
   public delegate void LiveStatsLoginRequest(object sender, LiveStatsLoginEventArgs e);
   public event LiveStatsLoginRequest OnLSLoginRequest;

   public MyClass()
   {

   }

   public void OpenConnection(string postData)
   { 
      var args = new object[] {postData, Operation.Check, LiveStatsAction.Authenticate};
      var t = new Thread(dowork);
      t.Start(args);
   }

   private void dowork(object args)
   {
      DoSomethingThatTakesALongTime();
      OnLSLoginRequest.Invoke(this, new EventArgs());
   }
}


Now the main form (MainForm.cs)
public partial class MainForm : Form
{
   private MyClass myClass;

   public MainForm()
   {
      InitializeComponent();
      myClass = new MyClass();
      MyClass.OnLSLoginRequest += LiveStats_OnLSLoginRequest;
   }

   private void Form_Load(object sender, EventArgs e)
   {
      myClass.OpenConnection(new object[]{"Something"};
   }

   private void LiveStats_OnLSLoginRequest(object sender, LiveStatsLoginEventArgs e)
   {
       this.TextBox1.Text = "Logged in!";
   }
}


The problem is that I get an error due to cross threading. I know that I can fix this my doing the following:

MainForm.cs
private void LiveStats_OnLSLoginRequest(object sender, LiveStatsLoginEventArgs e)
{
    if (InvokeRequired)
    {
        //do invoke
    }
    else
    {
        this.TextBox1.Text = "Logged in!";
    }
}


But I think this is messy and I would like to do the invoking through the class itself. I will eventually be releasing this code open source and don't want the user to have to worry/need to invoke the main form.

How can I solve this?

many thanks

(Sorry for the crude code!)


AnswerRe: C# threads and invoking Pin
Henry Minute2-Jul-09 6:44
Henry Minute2-Jul-09 6:44 
GeneralRe: C# threads and invoking Pin
Luc Pattyn2-Jul-09 6:50
sitebuilderLuc Pattyn2-Jul-09 6:50 
GeneralRe: C# threads and invoking Pin
Henry Minute2-Jul-09 6:56
Henry Minute2-Jul-09 6:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.