Click here to Skip to main content
15,912,204 members
Home / Discussions / C#
   

C#

 
GeneralControl Scaling Pin
netJP12L26-Mar-08 10:26
netJP12L26-Mar-08 10:26 
GeneralRe: Control Scaling Pin
Christian Graus26-Mar-08 10:40
protectorChristian Graus26-Mar-08 10:40 
GeneralRe: Control Scaling Pin
netJP12L26-Mar-08 10:50
netJP12L26-Mar-08 10:50 
GeneralRe: Control Scaling Pin
Christian Graus26-Mar-08 10:56
protectorChristian Graus26-Mar-08 10:56 
GeneralRe: Control Scaling Pin
netJP12L26-Mar-08 11:21
netJP12L26-Mar-08 11:21 
GeneralRe: Control Scaling Pin
Christian Graus26-Mar-08 11:39
protectorChristian Graus26-Mar-08 11:39 
GeneralRe: Control Scaling Pin
netJP12L26-Mar-08 17:38
netJP12L26-Mar-08 17:38 
GeneralCrossing threads problem (editing labels from another thread) Pin
oscarderooij26-Mar-08 10:19
oscarderooij26-Mar-08 10:19 
Hi there,

In my project I need to create a control in a form and edit it in a diferent thread, but as you all know, that is not possible because it is not allowed to edit something from another thread.

This way (i'm still learning how to use threads), I'm testing a simple project that will have 3 threads

1º- The Main Form
2º- A Count label that will be counting until 100
3º- A Count label that will be counting until 200

What am I trying to do?
- Create a Command that will receive the MainForm as a parameter (this way I can suspend, resume layouts and add Controls to it)

- Create in the Command constructor the label manually (exacly like the generated code from Designer) and the MethodInvoker. What I tried to do by doing so is to create the label, that I'm going to modify, IN the thread that is going to modify it.

- In the main form event of SHOWN call the constructor of the command and then the execute.

The code is at the end of this post.. i commented the line that crossthreads.
I tried creating the label Counter in the Designer but didn't work, then I tried to work around and do as shown above... but again.. I didn't think about the frmMain.Controls.Add(lblCounter) line that modifies the MainForm (in another Thread).

Well basecaly I'm having this problem for 2 reasons that differ from this article:
- The thread modify a Form/Control or something visual.
- The thread is a background process that counts until 100 (but I could let it count forever and it would never end... thats the problem...)

Anyone can help?

The Complete Command Code:

class UpdateCounterCmd: ICommand
{
   private System.Windows.Forms.Label lblCount; //Label to be Modified.
   private Form1 frmMain; //Reference to the main form.

   private MethodInvoker delInvokeMe; // The MethodInvoker.


   public UpdateCounterCmd(Form1 formMain)
   {
      lblCount = new System.Windows.Forms.Label();
      frmMain = formMain;

      delInvokeMe = new MethodInvoker(Count);
   }
   
   public void Execute()
   {
      delInvokeMe.BeginInvoke(CallBack, null);
   }
   
   private void CallBack(IAsyncResult ar)
   {
      delInvokeMe.EndInvoke(ar);
   }
   
   private void Count()
   {
      frmMain.SuspendLayout();
      lblCounter.AutoSize = true;
      lblCounter.Location = new System.Drawing.Point(333, 96);
      lblCounter.Name = "LabelCount";
      lblCounter.Size = new System.Drawing.Size(35, 13);
      lblCounter.TabIndex = 0;
      lblCounter.Text = "Counting: 0";
      lblCounter.Visible = true;
      frmMain.Controls.Add(lblCounter); //PROBLEM CROSSING THREADS!
      
      int i = 0;
      for (i = 0; i <= 100; i++)
      {
         lblCounter.Text = "Counting: " + i.ToString();
         frmMain.ResumeLayout(true);
         frmMain.PerformLayout();
         frmMain.SuspendLayout();
      }
   }
   
}

GeneralRe: Crossing threads problem (editing labels from another thread) Pin
led mike26-Mar-08 10:30
led mike26-Mar-08 10:30 
GeneralRe: Crossing threads problem (editing labels from another thread) Pin
oscarderooij27-Mar-08 5:16
oscarderooij27-Mar-08 5:16 
GeneralRe: Crossing threads problem (editing labels from another thread) Pin
led mike27-Mar-08 5:27
led mike27-Mar-08 5:27 
GeneraldataContext.storedProcedure(params).Skip(4).Take(3) - where is it executed Pin
Seishin#26-Mar-08 10:04
Seishin#26-Mar-08 10:04 
GeneralRe: dataContext.storedProcedure(params).Skip(4).Take(3) - where is it executed Pin
led mike26-Mar-08 10:10
led mike26-Mar-08 10:10 
GeneralRe: dataContext.storedProcedure(params).Skip(4).Take(3) - where is it executed Pin
Seishin#26-Mar-08 10:19
Seishin#26-Mar-08 10:19 
GeneralRe: dataContext.storedProcedure(params).Skip(4).Take(3) - where is it executed Pin
Not Active26-Mar-08 10:50
mentorNot Active26-Mar-08 10:50 
GeneralRe: dataContext.storedProcedure(params).Skip(4).Take(3) - where is it executed Pin
Seishin#26-Mar-08 10:51
Seishin#26-Mar-08 10:51 
GeneralForm position relative to form control Pin
dannytsang26-Mar-08 9:40
dannytsang26-Mar-08 9:40 
GeneralRe: Form position relative to form control Pin
Gareth H26-Mar-08 10:04
Gareth H26-Mar-08 10:04 
GeneralRe: Form position relative to form control Pin
dannytsang27-Mar-08 13:37
dannytsang27-Mar-08 13:37 
Questionc# code request Pin
jadtls26-Mar-08 9:27
jadtls26-Mar-08 9:27 
GeneralRe: c# code request Pin
Giorgi Dalakishvili26-Mar-08 9:45
mentorGiorgi Dalakishvili26-Mar-08 9:45 
QuestionHow to create a progressbar for a web browser control???? Pin
Aditya Baraya26-Mar-08 7:41
Aditya Baraya26-Mar-08 7:41 
AnswerRe: How to create a progressbar for a web browser control???? Pin
Gareth H26-Mar-08 8:53
Gareth H26-Mar-08 8:53 
QuestionHow to extract Ethernet type? [modified] Pin
merh26-Mar-08 6:46
merh26-Mar-08 6:46 
GeneralMessage Closed Pin
26-Mar-08 6:33
dshyryayev26-Mar-08 6:33 

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.