C#크로스 스레드 작업이 잘못 되었습니다.(delegate,Invoke 사용하기)

C# 윈도우 프로그램에서 쓰레드를 사용하였는데 쓰레드에서 폼의 컨트롤을 사용할 경우
크로스 스레드 작업이 잘못되었습니다. ‘…’컨트롤이 자신이 만들어진 스레드가 아닌 스레드에서 액세스되었습니다.’
에러가 발생한다.


스레드로 동작할 경우 하나의 컨트롤과 또 다른 하나의 컨트롤이 동시에 같은 메모리 주소를 참조 하는 경우…
쉽게 말해서 작업자에게 동시에 두가지 작업을 시키게 되면 작업우선순위를 결정하지 못해서 작업을 포기하거나 무엇부터 할 것인지 논의해야 하는 상황이 발생한다고 보면 된다.
스레드 동작 경우는 특정작업을 동시에 진행하므로 이러한 현상이 발생할 확률이 상당히 높다.
CrossThreadSafetyAccess.zip
C++ 의 경우는 Critical Section 등의 처리로 이런간섭을 번거롭게 처리해 주어한다.
때에 따라서는 Critical Section으로의 진입과 탈출을 정확하게 컨트롤 해야 하는 경우도 있다.
C#의 경우는 InvokeRequired를 확인하여 delegate,Invoke로 처리하는 방법이 있다.
delegate를 정의하고
delegate void CrossThreadSafetySetText(Control ctl, String text, String text2);
실행 부분에서 InvokeRequired를 Check하여 Invoke를 실행한다

private void CSafeSetText(Control ctl, String text, String text2)
{
 if ( ctl.InvokeRequired )
	ctl.Invoke(new CrossThreadSafetySetText(CSafeSetText), ctl, text, text2);
 else
  ctl.Text = text;
}

Control혹은 Form에서 Invoke를 실행하는 다른 방법

private void CSafeSetText2(String text, String text2)
{
 if (button3.InvokeRequired)
 { //Argument를 생성하는 방법
   Invoke(new CrossThreadSafetyFormSetText(CSafeSetText2),new object[]{text,text2});
 }
 else
 button3.Text = text;
}

“C#크로스 스레드 작업이 잘못 되었습니다.(delegate,Invoke 사용하기)”의 36개의 댓글

  1. Hello my family member! I want to say that this post
    is amazing, nice written and come with almost all vital infos.
    I would like to look extra posts like this
    .

  2. Generally I don’t read post on blogs, but I wish to say that this write-up very compelled
    me to try and do it! Your writing taste has been amazed me.
    Thanks, quite great post.

  3. Aw, this was an incredibly good post. Spending some time and actual effort to make a top
    notch article… but what can I say… I procrastinate a lot and
    don’t manage to get nearly anything done.

  4. Hey There. I found your blog using msn. This is a very well written article.

    I will be sure to bookmark it and come back to read
    more of your useful information. Thanks for the post.
    I’ll certainly return.

  5. You’re so awesome! I don’t think I’ve truly read a single thing like that before.
    So great to discover someone with a few unique thoughts on this subject.
    Seriously.. thanks for starting this up. This website is something that’s needed
    on the web, someone with a little originality!

  6. You really make it seem so easy with your presentation but
    I find this matter to be really something that I think I would
    never understand. It seems too complicated and extremely broad for me.
    I am looking forward for your next post, I
    will try to get the hang of it!

  7. I just want to say I am newbie to blogging and site-building and certainly enjoyed this web blog. Almost certainly I’m planning to bookmark your blog post . You really come with very good posts. Bless you for sharing with us your web-site.

  8. After research study a few of the post on your website now, and also I genuinely like your means of blogging. I bookmarked it to my book mark internet site listing and also will be checking back soon. Pls check out my web site also and let me recognize what you think.

  9. Oh my goodness! an amazing article dude. Thank you Nevertheless I am experiencing problem with ur rss. Don?t understand why Incapable to register for it. Exists any individual obtaining the same rss issue? Anybody who recognizes kindly react. Thnkx

  10. I was extremely delighted to find this web-site. I intended to many thanks for your time for this fantastic read!! I certainly delighting in every little of it and I have you bookmarked to take a look at new stuff you blog post.

  11. There are certainly a great deal of information like that to take into consideration. That is a fantastic indicate bring up. I offer the ideas above as general ideas however plainly there are concerns like the one you raise where the most essential thing will be operating in straightforward good faith. I don?t understand if ideal practices have emerged around things like that, yet I am sure that your work is clearly recognized as an up for grabs. Both boys and ladies feel the influence of just a moment?s satisfaction, for the remainder of their lives.

  12. Aw, this was a really wonderful article. In suggestion I wish to place in creating like this additionally? requiring time and also actual effort to make a great write-up? however what can I state? I hesitate alot as well as never seem to get something done.

  13. Can I just state what a relief to find a person that in fact understands what theyre talking about on the web. You absolutely recognize exactly how to bring an issue to light and also make it vital. Even more individuals need to read this and understand this side of the story. I cant think youre not extra prominent because you definitely have the gift.

  14. Youre so trendy! I do not intend Ive review anything like this before. So good to locate somebody with some original ideas on this subject. realy thank you for starting this up. this web site is something that is required on the internet, a person with a little creativity. valuable job for bringing something brand-new to the internet!

  15. Youre so amazing! I don’t intend Ive review anything like this before. So wonderful to discover someone with some original thoughts on this subject. realy thank you for starting this up. this web site is something that is required on the internet, a person with a little creativity. beneficial job for bringing something new to the internet!

  16. I found your blog site on google and inspect a few of your very early articles. Remain to maintain the excellent operate. I simply added up your RSS feed to my MSN News Viewers. Seeking ahead to finding out more from you in the future!?

  17. An outstanding share, I just given this onto a colleague that was doing a little analysis on this. And he in fact bought me breakfast because I located it for him. smile. So let me rephrase that: Thnx for the treat! Yet yeah Thnkx for spending the moment to review this, I feel highly concerning it as well as like reading more on this topic. If possible, as you become competence, would you mind upgrading your blog site with even more information? It is very valuable for me. Large thumb up for this article!

  18. Can I just say what an alleviation to discover somebody who in fact understands what theyre speaking about on the internet. You definitely recognize just how to bring a concern to light as well as make it essential. Even more people require to read this as well as understand this side of the tale. I angle believe youre not much more popular due to the fact that you absolutely have the gift.

댓글 남기기